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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import { expect, spy } from 'chai';
import { it, describe, beforeEach } from 'mocha';
import History from '../src/renderer/lib/history';
import { RoutePath } from '../src/renderer/lib/routes';
const BASE_PATH = RoutePath.launch;
const FIRST_PATH = RoutePath.main;
const SECOND_PATH = RoutePath.settings;
const THIRD_PATH = RoutePath.advancedSettings;
const FOURTH_PATH = RoutePath.wireguardKeys;
const FIFTH_PATH = RoutePath.splitTunneling;
describe('History', () => {
let history: History;
beforeEach(() => {
history = new History(BASE_PATH);
history.push(FIRST_PATH);
history.push(SECOND_PATH);
history.push(THIRD_PATH);
history.push(FOURTH_PATH);
});
it('should start at the correct location', () => {
const history2 = new History(BASE_PATH);
expect(history2.location.pathname).to.equal(BASE_PATH);
expect(history2.length).to.equal(1);
expect(history.location.pathname).to.equal(FOURTH_PATH);
expect(history.length).to.equal(5);
});
it('should pop', () => {
history.pop();
expect(history.location.pathname).to.equal(THIRD_PATH);
expect(history.length).to.equal(4);
});
it('should fail to pop', () => {
history.pop();
history.pop();
history.pop();
history.pop();
expect(history.location.pathname).to.equal(BASE_PATH);
expect(history.length).to.equal(1);
history.pop();
expect(history.location.pathname).to.equal(BASE_PATH);
expect(history.length).to.equal(1);
});
it('should push', () => {
history.push(FIFTH_PATH);
expect(history.location.pathname).to.equal(FIFTH_PATH);
expect(history.length).to.equal(6);
});
it('should go backward to base path', () => {
history.dismiss(true);
expect(history.location.pathname).to.equal(BASE_PATH);
expect(history.length).to.equal(1);
});
it('should reset entries with path', () => {
history.reset(THIRD_PATH);
expect(history.location.pathname).to.equal(THIRD_PATH);
expect(history.length).to.equal(1);
});
it('should add a listener', () => {
const listenerA = spy();
history.listen(listenerA);
history.pop();
history.push(FIFTH_PATH);
const listenerB = spy();
history.listen(listenerB);
history.dismiss(true);
history.push(FIRST_PATH);
expect(listenerA).to.have.been.called.exactly(4);
expect(listenerB).to.have.been.called.exactly(2);
});
it('should remove a listener', () => {
const listenerA = spy();
const removeListenerA = history.listen(listenerA);
history.pop();
history.push(FIFTH_PATH);
const listenerB = spy();
history.listen(listenerB);
history.dismiss(true);
removeListenerA();
history.push(FIRST_PATH);
history.reset(SECOND_PATH);
expect(listenerA).to.have.been.called.exactly(3);
expect(listenerB).to.have.been.called.exactly(3);
});
});
|