-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FI-1236 feat: add "full mocks" functionality
tests: add tests for "full mocks" functionality fix: pack compilation errors `is not under 'rootDir'`
- Loading branch information
Showing
69 changed files
with
1,064 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {readFile, writeFile} from 'node:fs/promises'; | ||
import {join} from 'node:path'; | ||
|
||
import {BAD_REQUEST_STATUS_CODE, READ_FILE_OPTIONS} from 'e2ed/constants'; | ||
|
||
import type {FullMocks} from 'autotests/configurator'; | ||
import type {FilePathFromRoot, FullMocksTestId, RequestKind, TestFullMocks} from 'e2ed/types'; | ||
|
||
const fullMocksStoragePath = join('autotests', 'fixtures', 'fullMocks'); | ||
|
||
const getTestFullMocksPath = (testId: FullMocksTestId): FilePathFromRoot => | ||
join(fullMocksStoragePath, `${testId}.json`) as FilePathFromRoot; | ||
|
||
export const fullMocks: FullMocks = { | ||
filterTests: ({options: {meta}}) => meta.testId === '18', | ||
getRequestKind: (method, {pathname}) => pathname as RequestKind, | ||
getResponseFromFullMocks: ({responseWithRequest}) => | ||
responseWithRequest ?? {statusCode: BAD_REQUEST_STATUS_CODE}, | ||
getResponseToWriteToFullMocks: (requestKind, responseWithRequest) => responseWithRequest, | ||
readTestFullMocks: async (testId) => { | ||
const testFullMocksJson = await readFile(getTestFullMocksPath(testId), READ_FILE_OPTIONS).catch( | ||
() => undefined, | ||
); | ||
|
||
if (testFullMocksJson === undefined) { | ||
return undefined; | ||
} | ||
|
||
return JSON.parse(testFullMocksJson) as TestFullMocks; | ||
}, | ||
writeTestFullMocks: async (testId, testFullMocks) => { | ||
const testFullMocksJson = JSON.stringify(testFullMocks); | ||
|
||
await writeFile(getTestFullMocksPath(testId), testFullMocksJson); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export {createDevice} from './device'; | ||
export {addProduct} from './product'; | ||
export {createUser} from './user'; | ||
export {addUser, getUsers} from './worker'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {createClientFunction} from 'e2ed'; | ||
|
||
import type {ApiProduct, Product} from 'autotests/types'; | ||
|
||
/** | ||
* Adds product. | ||
*/ | ||
export const addProduct = createClientFunction( | ||
(product: Product) => | ||
fetch(`https://reqres.in/api/product/${product.id}?size=${product.size}`, { | ||
body: JSON.stringify({ | ||
cookies: [], | ||
input: product.input, | ||
model: product.model, | ||
version: product.version, | ||
}), | ||
headers: {'Content-Type': 'application/json; charset=UTF-8'}, | ||
method: 'POST', | ||
}).then((res) => res.json() as Promise<ApiProduct>), | ||
{name: 'addProduct', timeout: 2_000}, | ||
); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"/api/product/135865":[{"completionTimeInMs":1716205312921,"duration":"4ms","request":{"method":"POST","query":{"size":"13"},"requestBody":{"cookies":[],"input":17,"model":"samsung","version":"12"},"requestHeaders":{"accept":"*/*","content-type":"application/json; charset=UTF-8","referer":"https://joomcode.github.io/","user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.35 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.35"},"url":"https://reqres.in/api/product/135865?size=13","utcTimeInMs":1716205312917},"responseBody":{"id":135865,"method":"POST","output":"17","payload":{"id":"135865","cookies":[],"input":17,"model":"samsung","version":"12"},"query":{"size":"13"},"url":"https://reqres.in/api/product/135865?size=13"},"responseHeaders":{"content-length":"201","content-type":"application/json; charset=UTF-8"},"statusCode":200},{"completionTimeInMs":1716205313198,"duration":"255ms","request":{"method":"POST","query":{"size":"13"},"requestBody":{"cookies":[],"input":17,"model":"samsung","version":"12"},"requestHeaders":{"accept":"*/*","content-type":"application/json; charset=UTF-8","referer":"https://joomcode.github.io/","user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.35 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.35"},"url":"https://reqres.in/api/product/135865?size=13","utcTimeInMs":1716205312943},"responseBody":{"cookies":[],"input":17,"model":"samsung","version":"12","id":"713","createdAt":"2024-05-20T11:41:53.144Z"},"responseHeaders":{"date":"Mon, 20 May 2024 11:41:53 GMT","content-type":"application/json; charset=utf-8","content-length":"108","report-to":"{\"group\":\"heroku-nel\",\"max_age\":3600,\"endpoints\":[{\"url\":\"https://nel.heroku.com/reports?ts=1716205313&sid=c4c9725f-1ab0-44d8-820f-430df2718e11&s=mbk9hukht3cdEhvMVZlrntLk5LPKjfRJrh277niKK2Q%3D\"}]}","reporting-endpoints":"heroku-nel=https://nel.heroku.com/reports?ts=1716205313&sid=c4c9725f-1ab0-44d8-820f-430df2718e11&s=mbk9hukht3cdEhvMVZlrntLk5LPKjfRJrh277niKK2Q%3D","nel":"{\"report_to\":\"heroku-nel\",\"max_age\":3600,\"success_fraction\":0.005,\"failure_fraction\":0.05,\"response_headers\":[\"Via\"]}","x-powered-by":"Express","access-control-allow-origin":"*","etag":"W/\"6c-640Odu4IHdHX5uCM17XShtUIvGU\"","via":"1.1 vegur","cf-cache-status":"DYNAMIC","server":"cloudflare","cf-ray":"886c0f66bc3265cc-FRA"},"statusCode":201}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import {test} from 'autotests'; | ||
import {addProduct} from 'autotests/entities'; | ||
import {E2edReportExample} from 'autotests/pageObjects/pages'; | ||
import {CreateProduct as CreateProductRoute} from 'autotests/routes/apiRoutes'; | ||
import {expect} from 'e2ed'; | ||
import {mockApiRoute, navigateToPage, unmockApiRoute} from 'e2ed/actions'; | ||
|
||
import type {DeviceId, Product, ProductId} from 'autotests/types'; | ||
import type {Url} from 'e2ed/types'; | ||
|
||
test('full mocks works correctly', {meta: {testId: '18'}}, async () => { | ||
await navigateToPage(E2edReportExample); | ||
|
||
await mockApiRoute(CreateProductRoute, (routeParams, {method, query, requestBody, url}) => { | ||
const responseBody = { | ||
id: routeParams.id, | ||
method, | ||
output: String(requestBody.input), | ||
payload: {id: String(routeParams.id) as DeviceId, ...requestBody}, | ||
query, | ||
url, | ||
}; | ||
|
||
return {responseBody}; | ||
}); | ||
|
||
const productId = Number('135865') as ProductId; | ||
const product: Product = { | ||
id: productId, | ||
input: 17, | ||
model: 'samsung', | ||
size: '13', | ||
version: '12', | ||
}; | ||
|
||
const mockedProduct = await addProduct(product); | ||
|
||
const fetchUrl = `https://reqres.in/api/product/${productId}?size=${product.size}` as Url; | ||
|
||
await expect(mockedProduct, 'mocked API returns correct result').eql({ | ||
id: productId, | ||
method: 'POST', | ||
output: String(product.input), | ||
payload: { | ||
cookies: [], | ||
id: String(productId) as DeviceId, | ||
input: product.input, | ||
model: product.model, | ||
version: product.version, | ||
}, | ||
query: {size: product.size}, | ||
url: fetchUrl, | ||
}); | ||
|
||
await unmockApiRoute(CreateProductRoute); | ||
|
||
const newMockedProduct = await addProduct(product); | ||
|
||
await expect('createdAt' in newMockedProduct, 'API mock on CreateProductRoute was umocked').ok(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.