forked from adamlaska/circleci-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoc.test.js
More file actions
201 lines (171 loc) · 5.92 KB
/
toc.test.js
File metadata and controls
201 lines (171 loc) · 5.92 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import '../../../jest/global';
import { highlightTocOnScroll, highlightTocOnScrollOnce } from './toc';
describe('Table of Contents', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('highlightTocOnScroll', () => {
let h2, tocItem;
beforeEach(() => {
jest.resetAllMocks();
h2 = document.createElement('h2');
tocItem = document.createElement('a');
jest
.spyOn(document, 'querySelectorAll')
.mockImplementation((selector) => {
let items = document.createDocumentFragment();
switch (selector) {
case '.toc-entry a':
items.appendChild(tocItem);
break;
case 'h2, h3, h4, h5, h6':
items.appendChild(h2);
break;
}
return items.childNodes;
});
});
it('Calls observe', () => {
// because InteractionObserver is a class on the window we need to actually
// spy on the prototype of the method.
const spy = jest.spyOn(IntersectionObserver.prototype, 'observe');
highlightTocOnScroll();
expect(spy).toHaveBeenCalled();
});
it('Does not call observe if there are no titles.', () => {
const spy = jest.spyOn(IntersectionObserver.prototype, 'observe');
let tocItem = document.createElement('a');
// we override the jest query selector here because
// we need to mock the case where there are not titles.
jest
.spyOn(document, 'querySelectorAll')
.mockImplementation((selector) => {
let items = document.createDocumentFragment();
switch (selector) {
case '.toc-entry a':
items.appendChild(tocItem);
break;
}
return items.childNodes;
});
highlightTocOnScroll();
expect(spy).not.toHaveBeenCalled();
});
it("A document with one h2 is in the TOC and is 'active'", () => {
highlightTocOnScroll();
expect([...tocItem.classList]).toEqual(
expect.arrayContaining(['active']),
);
});
it('Does not add an active class to TOC item when a ignored headline is encountered', () => {
let h2 = document.createElement('h2');
h2.classList.add('help-improve-header');
jest
.spyOn(document, 'querySelectorAll')
.mockImplementation((selector) => {
let items = document.createDocumentFragment();
switch (selector) {
case '.toc-entry a':
items.appendChild(tocItem);
break;
case 'h2, h3, h4, h5, h6':
items.appendChild(h2);
break;
}
return items.childNodes;
});
highlightTocOnScroll();
expect([...tocItem.classList]).toEqual(
expect.not.arrayContaining(['active']),
);
});
});
describe('highlightTocOnScrollOnce', () => {
let h2, tocItem;
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
h2 = document.createElement('h2');
tocItem = document.createElement('a');
jest
.spyOn(document, 'querySelectorAll')
.mockImplementation((selector) => {
let items = document.createDocumentFragment();
switch (selector) {
case '.toc-entry a':
items.appendChild(tocItem);
break;
case 'h2, h3, h4, h5, h6':
items.appendChild(h2);
break;
}
return items.childNodes;
});
});
it('Calls observe', () => {
const toc = require('./toc');
// because InteractionObserver is a class on the window we need to actually
// spy on the prototype of the method.
const spy = jest.spyOn(IntersectionObserver.prototype, 'observe');
toc.highlightTocOnScrollOnce();
expect(spy).toHaveBeenCalled();
});
it('Does not call observe if there are no titles.', () => {
const toc = require('./toc');
const spy = jest.spyOn(IntersectionObserver.prototype, 'observe');
let tocItem = document.createElement('a');
// we override the jest query selector here because
// we need to mock the case where there are not titles.
jest
.spyOn(document, 'querySelectorAll')
.mockImplementation((selector) => {
let items = document.createDocumentFragment();
switch (selector) {
case '.toc-entry a':
items.appendChild(tocItem);
break;
}
return items.childNodes;
});
toc.highlightTocOnScrollOnce();
expect(spy).not.toHaveBeenCalled();
});
it("A document with one h2 is in the TOC and is 'active'", () => {
const toc = require('./toc');
toc.highlightTocOnScrollOnce();
expect([...tocItem.classList]).toEqual(
expect.arrayContaining(['active']),
);
});
it('Does not add an active class to TOC item when a ignored headline is encountered', () => {
const toc = require('./toc');
let h2 = document.createElement('h2');
h2.classList.add('help-improve-header');
jest
.spyOn(document, 'querySelectorAll')
.mockImplementation((selector) => {
let items = document.createDocumentFragment();
switch (selector) {
case '.toc-entry a':
items.appendChild(tocItem);
break;
case 'h2, h3, h4, h5, h6':
items.appendChild(h2);
break;
}
return items.childNodes;
});
toc.highlightTocOnScrollOnce();
expect([...tocItem.classList]).toEqual(
expect.not.arrayContaining(['active']),
);
});
});
it('only calls highlightTocOnScroll once when called multiple times', () => {
const spy = jest.spyOn(document, 'querySelectorAll');
highlightTocOnScrollOnce();
expect(spy).toHaveBeenCalledTimes(2);
highlightTocOnScrollOnce();
expect(spy).toHaveBeenCalledTimes(2);
});
});