-
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-714 feat: add functions
waitForRequestToRoute
/`waitForResponseTo…
…Route` feat: add `RequestWithUtcTimeInMs` public type fix: correct definition of `ResponseWithRequest` type fix: use `ResponseWithRequest` for `waitForResponse` fix: use `RequestWithUtcTimeInMs` for `waitForRequest` fix: add `skipLogs` option to `waitForRequest`/`waitForResponse` fix: tests for timeout in `expect` functions fix: add check that methods `isMatchUrl` and `getParamsFromUrl` are consistent fix: printing of `E2edError` after `replaceFields` applying chore: add eslint plugin `typescript-sort-keys/recommended` tests: add tests for `waitForResponseToRoute` function
- Loading branch information
Showing
87 changed files
with
850 additions
and
300 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
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,2 +1,3 @@ | ||
export {createDevice} from './device'; | ||
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,32 @@ | ||
import {createClientFunction} from 'e2ed'; | ||
import {log} from 'e2ed/utils'; | ||
|
||
import type {UserWorker} from 'autotests/types'; | ||
|
||
const clientGetUsers = createClientFunction( | ||
(delay: number) => | ||
fetch(`https://reqres.in/api/users?delay=${delay}`, {method: 'GET'}).then((res) => res.json()), | ||
{name: 'getUsers', timeout: 6_000}, | ||
); | ||
|
||
/** | ||
* Adds user-worker. | ||
*/ | ||
export const addUser = createClientFunction( | ||
(user: UserWorker, delay?: number) => | ||
fetch(`https://reqres.in/api/users${delay ? `?delay=${delay}` : ''}`, { | ||
body: JSON.stringify(user), | ||
headers: {'Content-Type': 'application/json; charset=UTF-8'}, | ||
method: 'POST', | ||
}).then((res) => res.json()), | ||
{name: 'addUser', timeout: 2_000}, | ||
); | ||
|
||
/** | ||
* Get list of user-workers. | ||
*/ | ||
export const getUsers = (delay: number): Promise<unknown> => { | ||
log(`Send API request with delay = ${delay}s`); | ||
|
||
return clientGetUsers(delay); | ||
}; |
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,48 @@ | ||
import {ApiRoute} from 'autotests/routes'; | ||
import {assertValueIsTrue} from 'e2ed/utils'; | ||
|
||
import type {ApiAddUserRequest, ApiAddUserResponse} from 'autotests/types'; | ||
import type {Url} from 'e2ed/types'; | ||
|
||
type Params = Readonly<{delay?: number}>; | ||
|
||
const pathStart = '/api/users'; | ||
|
||
/** | ||
* Client API route for adding user-worker. | ||
*/ | ||
export class AddUser extends ApiRoute<Params, ApiAddUserRequest, ApiAddUserResponse> { | ||
static override getParamsFromUrl(url: Url): Params { | ||
const urlObject = new URL(url); | ||
|
||
assertValueIsTrue( | ||
urlObject.pathname.startsWith(pathStart), | ||
'url pathname starts with correct path', | ||
{urlObject}, | ||
); | ||
|
||
const delay: number | undefined = Number(urlObject.searchParams.get('delay')); | ||
|
||
if (!Number.isNaN(delay)) { | ||
assertValueIsTrue(Number.isInteger(delay), 'url has correct delay', {delay, urlObject}); | ||
|
||
return {delay}; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
getMethod(): 'POST' { | ||
return 'POST'; | ||
} | ||
|
||
override getOrigin(): Url { | ||
return 'https://reqres.in' as Url; | ||
} | ||
|
||
getPath(): string { | ||
const {delay} = this.routeParams; | ||
|
||
return delay === undefined ? pathStart : `${pathStart}?delay=${delay}`; | ||
} | ||
} |
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
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,39 +1,42 @@ | ||
import {test} from 'autotests'; | ||
import {createClientFunction, expect} from 'e2ed'; | ||
import {waitForRequest} from 'e2ed/actions'; | ||
import {addUser} from 'autotests/entities'; | ||
import {AddUser} from 'autotests/routes/apiRoutes'; | ||
import {expect} from 'e2ed'; | ||
import {waitForRequest, waitForRequestToRoute} from 'e2ed/actions'; | ||
import {assertFunctionThrows} from 'e2ed/utils'; | ||
|
||
import type {UserWorker} from 'autotests/types'; | ||
import type {Request} from 'e2ed/types'; | ||
|
||
type Body = Readonly<{job: string; name: string}> | undefined; | ||
type Body = UserWorker | undefined; | ||
|
||
const worker: UserWorker = {job: 'leader', name: 'John'}; | ||
|
||
test( | ||
'waitForRequest gets correct request body and rejects on timeout', | ||
{meta: {testId: '2'}, testIdleTimeout: 3_000}, | ||
async () => { | ||
const addUser = createClientFunction( | ||
() => | ||
fetch('https://reqres.in/api/users', { | ||
body: JSON.stringify({job: 'leader', name: 'John'}), | ||
headers: {'Content-Type': 'application/json; charset=UTF-8'}, | ||
method: 'POST', | ||
}).then((res) => res.json()), | ||
{name: 'addUser', timeout: 2_000}, | ||
); | ||
|
||
void addUser(); | ||
void addUser(worker); | ||
|
||
const request = await waitForRequest( | ||
({requestBody}: Request<Body>) => requestBody?.name === 'John', | ||
); | ||
|
||
await expect(request.requestBody, 'request has correct body').eql({ | ||
job: 'leader', | ||
name: 'John', | ||
}); | ||
await expect(request.requestBody, 'request has correct body').eql(worker); | ||
|
||
await assertFunctionThrows(async () => { | ||
await waitForRequest(() => false, {timeout: 100}); | ||
}, '`waitForRequest` throws an error on timeout'); | ||
|
||
void addUser(worker, 1); | ||
|
||
const {request: secondRequest, routeParams} = await waitForRequestToRoute(AddUser); | ||
|
||
await expect( | ||
secondRequest.requestBody, | ||
'request from waitForRequestToRoute has correct body', | ||
).eql(worker); | ||
|
||
await expect(routeParams, 'routeParams from waitForRequestToRoute is correct').eql({delay: 1}); | ||
}, | ||
); |
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,50 +1,38 @@ | ||
import {test} from 'autotests'; | ||
import {createClientFunction, expect} from 'e2ed'; | ||
import {addUser} from 'autotests/entities'; | ||
import {expect} from 'e2ed'; | ||
import {waitForResponse} from 'e2ed/actions'; | ||
import {assertFunctionThrows} from 'e2ed/utils'; | ||
|
||
import type {UserWorker} from 'autotests/types'; | ||
import type {Response} from 'e2ed/types'; | ||
|
||
type Body = Readonly<{job: string; name: string}> | undefined; | ||
type Body = UserWorker | undefined; | ||
|
||
const worker: UserWorker = {job: 'leader', name: 'John'}; | ||
|
||
test( | ||
'waitForResponse gets correct response body and rejects on timeout', | ||
{meta: {testId: '3'}, testIdleTimeout: 3_000}, | ||
async () => { | ||
const addUser = createClientFunction( | ||
() => | ||
fetch('https://reqres.in/api/users', { | ||
body: JSON.stringify({job: 'leader', name: 'John'}), | ||
headers: {'Content-Type': 'application/json; charset=UTF-8'}, | ||
method: 'POST', | ||
}).then((res) => res.json()), | ||
{name: 'addUser'}, | ||
); | ||
|
||
void addUser(); | ||
void addUser(worker); | ||
|
||
let response = await waitForResponse( | ||
({responseBody}: Response<Body>) => responseBody?.name === 'John', | ||
let response = await waitForResponse<Response<Body>>( | ||
({responseBody}) => responseBody?.name === 'John', | ||
); | ||
|
||
await expect(response.responseBody, 'response has correct body').contains({ | ||
job: 'leader', | ||
name: 'John', | ||
}); | ||
await expect(response.responseBody, 'response has correct body').contains(worker); | ||
|
||
await assertFunctionThrows(async () => { | ||
await waitForResponse(() => false, {timeout: 100}); | ||
}, '`waitForResponse` throws an error on timeout'); | ||
|
||
void addUser(); | ||
void addUser(worker); | ||
|
||
response = await waitForResponse( | ||
({request}: Response<Body>) => request?.url === 'https://reqres.in/api/users', | ||
response = await waitForResponse<Response<Body>>( | ||
({request}) => request.url === 'https://reqres.in/api/users', | ||
); | ||
|
||
await expect(response.responseBody, 'second response has correct body').contains({ | ||
job: 'leader', | ||
name: 'John', | ||
}); | ||
await expect(response.responseBody, 'second response has correct body').contains(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,12 @@ | ||
import type {ApiGetUsersResponse, UserWorker} from 'autotests/types'; | ||
import type {Request} from 'e2ed/types'; | ||
|
||
/** | ||
* API request for adding user-worker endpoint. | ||
*/ | ||
export type ApiAddUserRequest = Request<UserWorker>; | ||
|
||
/** | ||
* API response for adding user-worker endpoint. | ||
*/ | ||
export type ApiAddUserResponse = ApiGetUsersResponse; |
Oops, something went wrong.