/
GoForward.ts
72 lines (67 loc) · 1.67 KB
/
GoForward.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type { History } from "../../history";
export default async function GoForward(history: History) {
let spy: jest.SpyInstance = jest.fn();
expect(history.location).toMatchObject({
pathname: "/",
});
history.push("/home");
expect(history.action).toEqual("PUSH");
expect(history.location).toMatchObject({
pathname: "/home",
});
expect(spy).not.toHaveBeenCalled();
// JSDom doesn't fire the listener synchronously :(
let promise1 = new Promise((resolve) => {
let unlisten = history.listen((...args) => {
//@ts-expect-error
spy(...args);
unlisten();
resolve(null);
});
});
history.go(-1);
await promise1;
expect(history.action).toEqual("POP");
expect(history.location).toMatchObject({
pathname: "/",
});
expect(spy).toHaveBeenCalledWith({
action: "POP",
delta: expect.any(Number),
location: {
hash: "",
key: expect.any(String),
pathname: "/",
search: "",
state: null,
},
});
expect(spy.mock.calls.length).toBe(1);
// JSDom doesn't fire the listener synchronously :(
let promise2 = new Promise((resolve) => {
let unlisten = history.listen((...args) => {
//@ts-expect-error
spy(...args);
unlisten();
resolve(null);
});
});
history.go(1);
await promise2;
expect(history.action).toEqual("POP");
expect(history.location).toMatchObject({
pathname: "/home",
});
expect(spy).toHaveBeenCalledWith({
action: "POP",
delta: expect.any(Number),
location: {
hash: "",
key: expect.any(String),
pathname: "/home",
search: "",
state: null,
},
});
expect(spy.mock.calls.length).toBe(2);
}