-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock-scope.ts
56 lines (48 loc) · 1.25 KB
/
mock-scope.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
52
53
54
55
56
import { InvalidArgumentError } from "./mock-fetch.error.ts";
import { MockRequest } from "./mock-fetch.type.ts";
/**
* Defines the scope API for an interceptor response
*/
export class MockScope {
#mockRequest: MockRequest;
constructor(mockRequest: MockRequest) {
this.#mockRequest = mockRequest;
}
/**
* Delay a response by a set amount in ms.
*/
delay(waitInMs: number) {
if (!Number.isInteger(waitInMs) || waitInMs <= 0) {
throw new InvalidArgumentError(
"Invalid delay: waitInMs must be a valid integer > 0",
);
}
this.#mockRequest.delay = waitInMs;
return this;
}
/**
* For a defined response, never mark as consumed.
*/
persist() {
this.#mockRequest.persist = true;
return this;
}
/**
* Allow one to define a response for a set amount of matching requests.
*/
times(repeatTimes: number) {
if (!Number.isInteger(repeatTimes) || repeatTimes <= 0) {
throw new InvalidArgumentError(
"Invalid times input: repeatTimes must be a valid integer > 0",
);
}
this.#mockRequest.times = repeatTimes;
return this;
}
/**
* Mock Request Metadata for the Mock Scope.
*/
get metadata(): Readonly<MockRequest> {
return this.#mockRequest;
}
}