-
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-1557 fix: ingore all re-requests when
waitForAllRequestsComplete
tests: add tests of types for `mockWebSocketRoute` tests: add tests for `mockWebSocketRoute` fix: protocol in `WebSocketRoute.getUrl` method fix: types of request and response for `mockWebSocketRoute` fix: context in `onMessage` handler for `mockWebSocketRoute`
- Loading branch information
Showing
18 changed files
with
263 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export {createDevice} from './device'; | ||
export {addProduct} from './product'; | ||
export {sendScore} from './score'; | ||
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,25 @@ | ||
import {createClientFunction} from 'e2ed'; | ||
|
||
import type {ClientFunction, Url} from 'e2ed/types'; | ||
|
||
/** | ||
* Sends page score. | ||
*/ | ||
export const sendScore: ClientFunction<[string, Url], Promise<string>> = createClientFunction( | ||
(pageState, url) => { | ||
const socket = new WebSocket(url); | ||
const data = JSON.stringify({pageState}); | ||
const promise = new Promise<string>((resolve) => { | ||
socket.onmessage = (event) => { | ||
resolve(event.data as string); | ||
}; | ||
}); | ||
|
||
socket.onopen = () => { | ||
socket.send(data); | ||
}; | ||
|
||
return promise; | ||
}, | ||
{name: 'sendScore', timeout: 1_000}, | ||
); |
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 {WebSocketRoute} from 'e2ed'; | ||
|
||
import type {WebSocketBaseRequest, WebSocketBaseResponse} from 'autotests/types'; | ||
|
||
/** | ||
* Base WebSocket. | ||
*/ | ||
export class Base extends WebSocketRoute<undefined, WebSocketBaseRequest, WebSocketBaseResponse> { | ||
getPath(): string { | ||
return '/base'; | ||
} | ||
} |
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,33 @@ | ||
import {stringify} from 'node:querystring'; | ||
import {URL} from 'node:url'; | ||
|
||
import {WebSocketRoute} from 'e2ed'; | ||
import {assertValueIsTrue} from 'e2ed/utils'; | ||
|
||
import type {WebSocketScoreRequest, WebSocketScoreResponse} from 'autotests/types'; | ||
import type {Url} from 'e2ed/types'; | ||
|
||
type Params = Readonly<{size: number}>; | ||
|
||
const pathname = '/score'; | ||
|
||
/** | ||
* Score WebSocket. | ||
*/ | ||
export class Score extends WebSocketRoute<Params, WebSocketScoreRequest, WebSocketScoreResponse> { | ||
static override getParamsFromUrlOrThrow(url: Url): Params { | ||
const urlObject = new URL(url); | ||
const size = urlObject.searchParams.get('size'); | ||
|
||
assertValueIsTrue(urlObject.pathname === pathname, 'pathname is correct', {urlObject}); | ||
|
||
return {size: Number(size)}; | ||
} | ||
|
||
getPath(): string { | ||
const {size} = this.routeParams; | ||
const query = stringify({size}); | ||
|
||
return `${pathname}?${query}`; | ||
} | ||
} |
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,2 @@ | ||
export {Base} from './Base'; | ||
export {Score} from './Score'; |
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,45 @@ | ||
import {test} from 'autotests'; | ||
import {sendScore} from 'autotests/entities'; | ||
import {E2edReportExample} from 'autotests/pageObjects/pages'; | ||
import {Score as ScoreRoute} from 'autotests/routes/webSocketRoutes'; | ||
import {expect} from 'e2ed'; | ||
import {mockWebSocketRoute, navigateToPage, unmockWebSocketRoute} from 'e2ed/actions'; | ||
import {assertFunctionThrows} from 'e2ed/utils'; | ||
|
||
import type {Url} from 'e2ed/types'; | ||
|
||
test( | ||
'mockWebSocketRoute correctly intercepts requests, and unmockWebSocketRoute cancels the interception', | ||
{meta: {testId: '19'}, testIdleTimeout: 3_000}, | ||
async () => { | ||
await mockWebSocketRoute(ScoreRoute, ({size}, {pageState}) => { | ||
const stateScore = Number(pageState); | ||
|
||
return {score: size * stateScore}; | ||
}); | ||
|
||
await navigateToPage(E2edReportExample); | ||
|
||
const pageState = '5'; | ||
const size = 3; | ||
const webSocketUrl = `wss://localhost/score?size=${size}` as Url; | ||
|
||
const result = await sendScore(pageState, webSocketUrl); | ||
|
||
const scoreRouteParams = ScoreRoute.getParamsFromUrlOrThrow(webSocketUrl); | ||
|
||
const scoreRouteFromUrl = new ScoreRoute(scoreRouteParams); | ||
|
||
await expect(scoreRouteFromUrl.routeParams.size, 'route has correct params').eql(size); | ||
|
||
await expect(JSON.parse(result), 'mocked WebSocket returns correct result').eql({ | ||
score: size * Number(pageState), | ||
}); | ||
|
||
await unmockWebSocketRoute(ScoreRoute); | ||
|
||
await assertFunctionThrows(async () => { | ||
await sendScore(pageState, webSocketUrl); | ||
}, 'throws an error after unmocking WebSocket route'); | ||
}, | ||
); |
55 changes: 55 additions & 0 deletions
55
autotests/tests/internalTypeTests/mockWebSocketRoute.skip.ts
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,55 @@ | ||
import {Main} from 'autotests/routes/pageRoutes'; | ||
import {Base, Score} from 'autotests/routes/webSocketRoutes'; | ||
import {mockWebSocketRoute, unmockWebSocketRoute} from 'e2ed/actions'; | ||
|
||
import type {WebSocketScoreRequest, WebSocketScoreResponse} from 'autotests/types'; | ||
import type {Any} from 'e2ed/types'; | ||
|
||
const anyMockFunction = (..._args: Any[]): Any => {}; | ||
|
||
const webSocketMockFunction = ( | ||
{size}: {size: number}, | ||
{pageState}: WebSocketScoreRequest, | ||
): WebSocketScoreResponse => { | ||
if (pageState !== '') { | ||
return {score: 8}; | ||
} | ||
|
||
return {score: size > 2 ? size : 2}; | ||
}; | ||
|
||
// @ts-expect-error: mockWebSocketRoute require WebSocket route as first argument | ||
void mockWebSocketRoute(Main, anyMockFunction); | ||
|
||
// @ts-expect-error: unmockWebSocketRoute require WebSocket route as first argument | ||
void unmockWebSocketRoute(Main); | ||
|
||
// @ts-expect-error: mockWebSocketRoute require WebSocket route with static method getParamsFromUrlOrThrow | ||
void mockWebSocketRoute(Base, anyMockFunction); | ||
|
||
// ok | ||
void mockWebSocketRoute(Score, anyMockFunction); | ||
|
||
// @ts-expect-error: unmockWebSocketRoute require WebSocket route with static method getParamsFromUrlOrThrow | ||
void unmockWebSocketRoute(Base); | ||
|
||
// ok | ||
void mockWebSocketRoute(Score, webSocketMockFunction); | ||
|
||
// ok | ||
void unmockWebSocketRoute(Score); | ||
|
||
// ok | ||
void mockWebSocketRoute( | ||
Score, | ||
async ( | ||
{size}, | ||
{pageState}, // eslint-disable-next-line @typescript-eslint/require-await | ||
) => { | ||
if (pageState !== '') { | ||
return {score: 10}; | ||
} | ||
|
||
return {score: size > 1 ? size : 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
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,9 @@ | ||
/** | ||
* Request for base WebSocket. | ||
*/ | ||
export type WebSocketBaseRequest = Readonly<{pageState: string}>; | ||
|
||
/** | ||
* Response for base WebSocket. | ||
*/ | ||
export type WebSocketBaseResponse = Readonly<{tags: readonly string[]}>; |
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,9 @@ | ||
/** | ||
* Request for score WebSocket. | ||
*/ | ||
export type WebSocketScoreRequest = Readonly<{pageState: string}>; | ||
|
||
/** | ||
* Response for score WebSocket. | ||
*/ | ||
export type WebSocketScoreResponse = Readonly<{score: number}>; |
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,6 +1,8 @@ | ||
export type {ApiAddUserRequest, ApiAddUserResponse} from './AddUser'; | ||
export type {WebSocketBaseRequest, WebSocketBaseResponse} from './Base'; | ||
export type {ApiCreateDeviceRequest, ApiCreateDeviceResponse} from './CreateDevice'; | ||
export type {ApiCreateProductRequest, ApiCreateProductResponse} from './CreateProduct'; | ||
export type {ApiGetUserRequest, ApiGetUserResponse} from './GetUser'; | ||
export type {ApiGetUsersRequest, ApiGetUsersResponse} from './GetUsers'; | ||
export type {WebSocketScoreRequest, WebSocketScoreResponse} from './Score'; | ||
export type {ApiUserSignUpRequest, ApiUserSignUpResponse} from './UserSignUp'; |
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