|
| 1 | +import { When } from '@cucumber/cucumber'; |
| 2 | +import { getValue } from './transformers'; |
| 3 | +import memory from '@qavajs/memory'; |
| 4 | +import { Route } from '@playwright/test'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Create simple mock instance |
| 8 | + * @param {string} urlTemplate - minimatch url template to mock |
| 9 | + * @param {string} memoryKey - memory key to store mock instance |
| 10 | + * @example When I create mock for '/yourservice/**' as 'mock1' |
| 11 | + * @example When I create mock for '$mockUrlTemplate' as 'mock1' |
| 12 | + */ |
| 13 | +When('I create mock for {string} as {string}', async function (urlTemplate: string, memoryKey: string) { |
| 14 | + const url = await getValue(urlTemplate); |
| 15 | + memory.setValue(memoryKey, url); |
| 16 | +}); |
| 17 | + |
| 18 | +async function respondWith(mockKey: string, statusCode: string, body: string): Promise<void> { |
| 19 | + const mockUrl: string = await getValue(mockKey); |
| 20 | + const responseStatusCode: number = parseInt(await getValue(statusCode)); |
| 21 | + const responseBody = await getValue(body); |
| 22 | + await page.route(mockUrl, async (route: Route) => { |
| 23 | + await route.fulfill({ |
| 24 | + body: responseBody, |
| 25 | + status: responseStatusCode |
| 26 | + }); |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Add mocking rule to respond with desired status code and payload |
| 32 | + * @param {string} mockKey - memory key to get mock instance |
| 33 | + * @param {string} statusCode - status code |
| 34 | + * @param {string} body - response body |
| 35 | + * @example |
| 36 | + * When I create mock for '/yourservice/**' with filter options as 'myServiceMock' |
| 37 | + * And I set '$myServiceMock' mock to respond '200' with: |
| 38 | + * """ |
| 39 | + * { |
| 40 | + * "status": "success" |
| 41 | + * } |
| 42 | + * """ |
| 43 | + */ |
| 44 | +When('I set {string} mock to respond {string} with:', respondWith); |
| 45 | + |
| 46 | +/** |
| 47 | + * Add mocking rule to respond with desired status code and payload |
| 48 | + * @param {string} mockKey - memory key to get mock instance |
| 49 | + * @param {string} statusCode - status code |
| 50 | + * @param {string} body - response body |
| 51 | + * @example |
| 52 | + * When I create mock for '/yourservice/**' with filter options as 'myServiceMock' |
| 53 | + * And I set '$myServiceMock' mock to respond '200' with '$response' |
| 54 | + */ |
| 55 | +When('I set {string} mock to respond {string} with {string}', respondWith); |
| 56 | + |
| 57 | +/** |
| 58 | + * Add mocking rule to abort request with certain reason |
| 59 | + * @param {string} mockKey - memory key to get mock instance |
| 60 | + * @param {string} reason - reason string see https://playwright.dev/docs/api/class-route#route-abort |
| 61 | + * @example |
| 62 | + * When I create mock for '/yourservice/**' with filter options as 'myServiceMock' |
| 63 | + * And I set '$myServiceMock' mock to abort with 'Failed' reason |
| 64 | + */ |
| 65 | +When('I set {string} mock to abort with {string} reason', async function (mockKey: string, reason: string) { |
| 66 | + const mockUrl: string = await getValue(mockKey); |
| 67 | + const errorCode: string = await getValue(reason); |
| 68 | + await page.route(mockUrl, async (route: Route) => { |
| 69 | + await route.abort(errorCode); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +/** |
| 74 | + * Restore mock |
| 75 | + * @param {string} mockKey - memory key to get mock instance |
| 76 | + * @example When I restore '$myServiceMock' |
| 77 | + */ |
| 78 | +When('I restore {string} mock', async function (mockKey: string) { |
| 79 | + const mockUrl: string = await getValue(mockKey); |
| 80 | + await page.unroute(mockUrl); |
| 81 | +}); |
| 82 | + |
| 83 | +/** |
| 84 | + * Restore all mocks |
| 85 | + * @example When I restore all mocks |
| 86 | + */ |
| 87 | +When('I restore all mocks', async function () { |
| 88 | + //@ts-ignore |
| 89 | + page._routes = []; |
| 90 | +}); |
0 commit comments