-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathDocumentationTopicStore.spec.js
152 lines (134 loc) · 4.74 KB
/
DocumentationTopicStore.spec.js
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
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import { filterInactiveReferences } from 'docc-render/utils/references';
import DocumentationTopicStore from 'docc-render/stores/DocumentationTopicStore';
import ApiChangesStoreBase from 'docc-render/stores/ApiChangesStoreBase';
import OnThisPageSectionsStoreBase from 'docc-render/stores/OnThisPageSectionsStoreBase';
const references = {
foo: { identifier: 'foo' },
bar: { identifier: 'bar' },
};
describe('DocumentationTopicStore', () => {
const defaultState = {
preferredLanguage: null,
contentWidth: 0,
...ApiChangesStoreBase.state,
...OnThisPageSectionsStoreBase.state,
references: {},
};
beforeEach(() => {
localStorage.clear();
DocumentationTopicStore.reset();
// force reset the api changes
DocumentationTopicStore.setAPIChanges(null);
DocumentationTopicStore.state.contentWidth = 0;
});
it('has a default state', () => {
expect(DocumentationTopicStore.state).toEqual(defaultState);
});
describe('reset', () => {
it('restores the default state', () => {
DocumentationTopicStore.state.apiChanges = {};
DocumentationTopicStore.state.references = references;
expect(DocumentationTopicStore.state).not.toEqual(defaultState);
DocumentationTopicStore.reset();
// assert all the state is reset
expect(DocumentationTopicStore.state).toEqual(defaultState);
});
});
describe('addOnThisPageSection', () => {
it('adds an item to the `onThisPageSections` state', () => {
DocumentationTopicStore.addOnThisPageSection({
anchor: 'foo',
title: 'Foo',
});
expect(DocumentationTopicStore.state).toEqual({
...defaultState,
onThisPageSections: [
{
anchor: 'foo',
i18n: true,
title: 'Foo',
},
],
});
DocumentationTopicStore.addOnThisPageSection({
anchor: 'bar',
title: 'Bar',
});
expect(DocumentationTopicStore.state).toEqual({
...defaultState,
onThisPageSections: [
{
anchor: 'foo',
i18n: true,
title: 'Foo',
},
{
anchor: 'bar',
i18n: true,
title: 'Bar',
},
],
});
});
});
describe('setPreferredLanguage', () => {
it('sets the `preferredLanguage` state', () => {
DocumentationTopicStore.setPreferredLanguage('objc');
expect(DocumentationTopicStore.state.preferredLanguage).toBe('objc');
});
});
it('sets the content width', () => {
DocumentationTopicStore.setContentWidth(99);
expect(DocumentationTopicStore.state.contentWidth).toBe(99);
});
it('sets `references`', () => {
DocumentationTopicStore.setReferences(references);
expect(DocumentationTopicStore.state.references)
.toEqual(filterInactiveReferences(references));
});
it('updates `references`', () => {
const prevState = DocumentationTopicStore.state;
DocumentationTopicStore.updateReferences();
expect(DocumentationTopicStore.state.references)
.toEqual(filterInactiveReferences(prevState.references));
});
describe('APIChanges', () => {
it('sets the API changes', () => {
const apiChanges = { foo: 'bar' };
DocumentationTopicStore.setAPIChanges(apiChanges);
expect(DocumentationTopicStore.state).toEqual({
...defaultState,
apiChanges,
});
});
it('updates API changes counts', async () => {
const spy = jest.spyOn(document, 'querySelectorAll');
await DocumentationTopicStore.updateApiChangesCounts();
expect(spy).toHaveBeenCalledTimes(3);
expect(spy).toHaveBeenNthCalledWith(1, '.changed-modified:not(.changed-total)');
expect(spy).toHaveBeenNthCalledWith(2, '.changed-added:not(.changed-total)');
expect(spy).toHaveBeenNthCalledWith(3, '.changed-deprecated:not(.changed-total)');
jest.restoreAllMocks();
});
it('resets API changes counts', () => {
DocumentationTopicStore.state.apiChangesCounts.modified = 5;
DocumentationTopicStore.state.apiChangesCounts.added = 5;
expect(DocumentationTopicStore.state.apiChangesCounts)
.not.toEqual(defaultState.apiChangesCounts);
DocumentationTopicStore.resetApiChanges();
expect(DocumentationTopicStore.state.apiChangesCounts)
.toEqual(defaultState.apiChangesCounts);
expect(DocumentationTopicStore.state.apiChanges)
.toEqual(null);
});
});
});