-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathSidebarTabs-test.js
More file actions
408 lines (351 loc) · 11.7 KB
/
SidebarTabs-test.js
File metadata and controls
408 lines (351 loc) · 11.7 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import {
checkAccessibility,
mockImportedComponents,
} from '@hypothesis/frontend-testing';
import { mount } from '@hypothesis/frontend-testing';
import sinon from 'sinon';
import SidebarTabs, { $imports } from '../SidebarTabs';
describe('SidebarTabs', () => {
// mock services
let fakeAnnotationsService;
let fakeSettings;
let fakeFrameSync;
let fakeStore;
let fakeUseRootThread;
function createComponent(props = {}) {
return mount(
<SidebarTabs
annotationsService={fakeAnnotationsService}
settings={fakeSettings}
frameSync={fakeFrameSync}
isLoading={false}
{...props}
/>,
);
}
function stubTabCounts(tabCounts = {}) {
fakeUseRootThread.returns({
rootThread: {
children: [],
},
tabCounts: {
annotation: 123,
note: 456,
orphan: 0,
...tabCounts,
},
});
}
beforeEach(() => {
fakeAnnotationsService = {
createPageNote: sinon.stub(),
};
fakeSettings = {
enableExperimentalNewNoteButton: false,
};
fakeFrameSync = {
getDocumentInfo: sinon.stub().resolves({ metadata: {} }),
};
fakeStore = {
selectTab: sinon.stub(),
isWaitingToAnchorAnnotations: sinon.stub().returns(false),
selectedTab: sinon.stub().returns('annotation'),
};
fakeUseRootThread = sinon.stub();
stubTabCounts();
$imports.$mock(mockImportedComponents());
$imports.$mock({
'./hooks/use-root-thread': { useRootThread: fakeUseRootThread },
'../store': { useSidebarStore: () => fakeStore },
});
});
afterEach(() => {
$imports.$restore();
});
it('should display the tabs and counts of annotations and notes', () => {
const wrapper = createComponent();
const annotationTab = wrapper.find('Tab[label="Annotations"]');
const noteTab = wrapper.find('Tab[label="Page notes"]');
assert.include(annotationTab.text(), 'Annotations');
assert.include(annotationTab.text(), '123');
assert.include(noteTab.text(), 'Page Notes');
assert.include(noteTab.text(), '456');
});
describe('Annotations tab', () => {
it('should display annotations tab as selected when it is active', () => {
const wrapper = createComponent();
const annotationTab = wrapper.find('Tab[label="Annotations"]');
const noteTab = wrapper.find('Tab[label="Page notes"]');
assert.isTrue(annotationTab.find('LinkButton').props().pressed);
assert.isFalse(noteTab.find('LinkButton').props().pressed);
});
it('should not display the add-page-note button when the annotations tab is active', () => {
fakeSettings.enableExperimentalNewNoteButton = true;
const wrapper = createComponent();
assert.equal(
wrapper.find('Button[data-testid="new-note-button"]').length,
0,
);
});
});
describe('Notes tab', () => {
it('should display notes tab as selected when it is active', () => {
fakeStore.selectedTab.returns('note');
const wrapper = createComponent();
const annotationTabButton = wrapper
.find('Tab[label="Annotations"]')
.find('LinkButton');
const noteTabButton = wrapper
.find('Tab[label="Page notes"]')
.find('LinkButton');
assert.isTrue(noteTabButton.prop('pressed'));
assert.isFalse(annotationTabButton.prop('pressed'));
});
describe('Add Page Note button', () => {
it('should not display the add-page-note button if the associated setting is not enabled', () => {
fakeSettings.enableExperimentalNewNoteButton = false;
fakeStore.selectedTab.returns('note');
const wrapper = createComponent();
assert.isFalse(
wrapper.find('LabeledButton[data-testid="new-note-button"]').exists(),
);
});
it('should display the add-page-note button when the associated setting is enabled', () => {
fakeSettings.enableExperimentalNewNoteButton = true;
fakeStore.selectedTab.returns('note');
const wrapper = createComponent();
assert.isTrue(
wrapper.find('Button[data-testid="new-note-button"]').exists(),
);
});
it('should apply background-color styling from settings', () => {
fakeSettings = {
branding: {
ctaBackgroundColor: '#00f',
},
enableExperimentalNewNoteButton: true,
};
fakeStore.selectedTab.returns('note');
const wrapper = createComponent();
const button = wrapper.find('Button[data-testid="new-note-button"]');
assert.deepEqual(button.prop('style'), { backgroundColor: '#00f' });
});
it('should add a new page note on click', async () => {
fakeSettings.enableExperimentalNewNoteButton = true;
fakeStore.selectedTab.returns('note');
const wrapper = createComponent();
await wrapper
.find('Button[data-testid="new-note-button"]')
.props()
.onClick();
// assert.calledOnce(fakeFrameSync.getDocumentInfo);
assert.calledOnce(fakeAnnotationsService.createPageNote);
});
});
});
describe('unanchored tab', () => {
it('should display unanchored tab if there is 1 or more orphans', () => {
stubTabCounts({ orphan: 1 });
const wrapper = createComponent();
const unanchoredTab = wrapper.find('Tab[label="Unanchored"]');
assert.isTrue(unanchoredTab.exists());
});
it('should display unanchored tab as selected when it is active', () => {
fakeStore.selectedTab.returns('orphan');
stubTabCounts({ orphan: 1 });
const wrapper = createComponent();
const unanchoredTab = wrapper.find('Tab[label="Unanchored"]');
assert.isTrue(unanchoredTab.find('LinkButton').prop('pressed'));
});
it('should not display unanchored tab if there are 0 orphans', () => {
const wrapper = createComponent();
assert.isFalse(wrapper.exists('Tab[label="Unanchored"]'));
});
});
describe('tab display and counts', () => {
it('should not render count if there are no page notes', () => {
stubTabCounts({ note: 0 });
const wrapper = createComponent();
const noteTab = wrapper.find('Tab[label="Page notes"]');
assert.equal(noteTab.text(), 'Page Notes');
});
it('should not display a message when its loading annotation count is 0', () => {
stubTabCounts({ annotation: 0 });
const wrapper = createComponent({ isLoading: true });
assert.isFalse(
wrapper.exists('[data-testid="annotations-unavailable-message"]'),
);
});
it('should not display a message when its loading notes count is 0', () => {
stubTabCounts({ note: 0 });
fakeStore.selectedTab.returns('note');
const wrapper = createComponent({ isLoading: true });
assert.isFalse(
wrapper.exists('[data-testid="notes-unavailable-message"]'),
);
});
it('should not display the longer version of the no annotations message when there are no annotations and isWaitingToAnchorAnnotations is true', () => {
stubTabCounts({ annotation: 0 });
fakeStore.isWaitingToAnchorAnnotations.returns(true);
const wrapper = createComponent({ isLoading: false });
assert.isFalse(
wrapper.exists('[data-testid="annotations-unavailable-message"]'),
);
});
it('should display the longer version of the no notes message when there are no notes', () => {
stubTabCounts({ note: 0 });
fakeStore.selectedTab.returns('note');
const wrapper = createComponent();
assert.include(
wrapper.find('Card[data-testid="notes-unavailable-message"]').text(),
'There are no page notes in this group',
);
});
it('should display the longer version of the no annotations message when there are no annotations', () => {
stubTabCounts({ annotation: 0 });
const wrapper = createComponent();
assert.include(
wrapper
.find('Card[data-testid="annotations-unavailable-message"]')
.text(),
'There are no annotations in this group',
);
});
});
const findButton = (wrapper, label) =>
wrapper.findWhere(
el => el.type() === 'button' && el.text().includes(label),
);
[
{ label: 'Annotations', tab: 'annotation' },
{ label: 'Page Notes', tab: 'note' },
{ label: 'Unanchored', tab: 'orphan' },
].forEach(({ label, tab }) => {
it(`should change the selected tab when "${label}" tab is clicked`, () => {
// Pre-select a different tab than the one we are about to click.
fakeStore.selectedTab.returns('other-tab');
// Make the "Unanchored" tab appear.
stubTabCounts({ orphan: 1 });
const wrapper = createComponent();
findButton(wrapper, label).simulate('click');
assert.calledWith(fakeStore.selectTab, tab);
});
});
it('does not change the selected tab if it is already selected', () => {
fakeStore.selectedTab.returns('note');
const wrapper = createComponent();
findButton(wrapper, 'Page Notes').simulate('click');
assert.notCalled(fakeStore.selectTab);
});
[
{
tabCounts: {
annotation: 2,
note: 0,
orphan: 0,
},
message: '2 annotations',
},
{
tabCounts: {
annotation: 0,
note: 3,
orphan: 0,
},
message: '3 notes',
},
{
tabCounts: {
annotation: 0,
note: 0,
orphan: 4,
},
message: '4 unanchored',
},
{
tabCounts: {
annotation: 2,
note: 3,
orphan: 4,
},
message: '2 annotations, 3 notes, 4 unanchored',
},
{
tabCounts: {
annotation: 1,
note: 1,
orphan: 1,
},
message: '1 annotation, 1 note, 1 unanchored',
},
].forEach(({ tabCounts, message }) => {
it('reports annotation count to screen readers', () => {
stubTabCounts(tabCounts);
const wrapper = createComponent();
const status = wrapper.find('[role="status"]');
assert.equal(status.text(), message);
});
});
describe('comments mode', () => {
it.each([
{
commentsMode: false,
shouldShowAnnosTab: true,
expectedNoteText: 'Page notes',
expectedButtonText: 'New note',
expectedFallbackMessage: 'There are no page notes in this group.',
},
{
commentsMode: true,
shouldShowAnnosTab: false,
expectedNoteText: 'Comments',
expectedButtonText: 'Add comment',
expectedFallbackMessage: 'There are no comments in this group.',
},
])(
'shows different contents if comments mode is enabled',
({
commentsMode,
shouldShowAnnosTab,
expectedNoteText,
expectedButtonText,
expectedFallbackMessage,
}) => {
fakeStore.selectedTab.returns('note');
stubTabCounts({ note: 0 });
fakeSettings.enableExperimentalNewNoteButton = true;
fakeSettings.commentsMode = commentsMode;
const wrapper = createComponent();
assert.equal(
wrapper.exists('Tab[name="annotation"]'),
shouldShowAnnosTab,
);
assert.equal(
wrapper.find('Tab[name="note"]').prop('label'),
expectedNoteText,
);
assert.equal(
wrapper.find('Button[data-testid="new-note-button"]').text(),
expectedButtonText,
);
assert.equal(
wrapper.find('Card[data-testid="notes-unavailable-message"]').text(),
expectedFallbackMessage,
);
},
);
});
it(
'should pass a11y checks',
checkAccessibility({
content: () => {
stubTabCounts({
annotation: 1,
note: 2,
orphan: 3,
});
return createComponent();
},
}),
);
});