forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait-for-route.ts
51 lines (40 loc) · 1.48 KB
/
wait-for-route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type {
Interception,
InterceptionState,
} from './types'
import { getAliasedRequests } from './aliasing'
import type { StateFunc } from '../../cypress/state'
const RESPONSE_WAITED_STATES: InterceptionState[] = ['Complete', 'Errored']
export function waitForRoute (alias: string, state: StateFunc, specifier: 'request' | 'response' | string): Interception | null {
// 1. Create an array of known requests that have this alias.
const candidateRequests = getAliasedRequests(alias, state)
// 2. Find the requests without responseWaited/requestWaited
// We should not find only the first interception here,
// because there can be a list of interceptions with meaningful result
// when user calls xhr.abort();
// @see https://github.com/cypress-io/cypress/issues/9549
const requests = candidateRequests.filter((r) => {
if (specifier === 'request') {
return r.requestWaited === false
}
return r.responseWaited === false
})
if (requests.length === 0) {
return null
}
// 3. Determine if it's ready based on the specifier
// When request, return the first request.
if (specifier === 'request') {
requests[0].requestWaited = true
return requests[0]
}
// When response, return the first request that has the wanted state.
for (let i = 0; i < requests.length; i++) {
const request = requests[i]
if (RESPONSE_WAITED_STATES.includes(request.state)) {
request.responseWaited = true
return request
}
}
return null
}