-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathamp-document-status-notification.js
More file actions
187 lines (153 loc) · 4.96 KB
/
amp-document-status-notification.js
File metadata and controls
187 lines (153 loc) · 4.96 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
/**
* External dependencies
*/
import { render, fireEvent } from '@testing-library/react';
import { beforeAll, describe, expect, it, jest } from '@jest/globals';
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
jest.mock('@wordpress/edit-post', () => ({
PluginSidebar: ({ children }) => children,
PluginSidebarMoreMenuItem: ({ children }) => children,
}));
jest.mock('@wordpress/block-editor', () => ({
BlockIcon: ({ icon }) => icon,
}));
/**
* Internal dependencies
*/
import AMPDocumentStatusNotification from '../index';
import { useAMPDocumentToggle } from '../../../hooks/use-amp-document-toggle';
import { useErrorsFetchingStateChanges } from '../../../hooks/use-errors-fetching-state-changes';
jest.mock('@wordpress/data', () => ({
useSelect: jest.fn(),
useDispatch: jest.fn(),
combineReducers: jest.fn(),
createSelector: jest.fn(),
createReduxStore: jest.fn(),
register: jest.fn(),
}));
jest.mock('../../../hooks/use-amp-document-toggle', () => ({
useAMPDocumentToggle: jest.fn(),
}));
jest.mock('../../../hooks/use-errors-fetching-state-changes', () => ({
useErrorsFetchingStateChanges: jest.fn(),
}));
describe('AMPDocumentStatusNotification', () => {
const openGeneralSidebar = jest.fn();
const closePublishSidebar = jest.fn();
function setupHooks(
useSelectOverrides = {},
useErrorsFetchingStateChangesOverrides = {},
useAMPDocumentToggleOverrides = {}
) {
useSelect.mockImplementation(() => ({
isPostDirty: false,
maybeIsPostDirty: false,
keptMarkupValidationErrorCount: 0,
unreviewedValidationErrorCount: 0,
...useSelectOverrides,
}));
useErrorsFetchingStateChanges.mockImplementation(() => ({
isFetchingErrors: false,
fetchingErrorsMessage: '',
...useErrorsFetchingStateChangesOverrides,
}));
useAMPDocumentToggle.mockImplementation(() => ({
isAMPEnabled: true,
...useAMPDocumentToggleOverrides,
}));
}
beforeAll(() => {
useDispatch.mockImplementation(() => ({
openGeneralSidebar,
closePublishSidebar,
}));
});
it('renders only a toggle if AMP is disabled', () => {
setupHooks(
{},
{},
{
isAMPEnabled: false,
}
);
const { container } = render(<AMPDocumentStatusNotification />);
expect(container.children).toHaveLength(1);
expect(container.innerHTML).toContain('Enable AMP');
});
it('renders a loading spinner when errors are being fetched', () => {
setupHooks(
{},
{
isFetchingErrors: true,
fetchingErrorsMessage: 'Loading',
}
);
const { container } = render(<AMPDocumentStatusNotification />);
expect(container.innerHTML).toContain('Enable AMP');
expect(
container.querySelector('.amp-spinner-container')
).not.toBeNull();
expect(container.innerHTML).toContain('Loading');
});
it('renders a correct message if a post content is or may be dirty', () => {
setupHooks({
isPostDirty: true,
});
let { container } = render(<AMPDocumentStatusNotification />);
expect(container.innerHTML).toContain('Enable AMP');
expect(container.innerHTML).toContain('Content has changed.');
expect(container.querySelector('svg')).not.toBeNull();
expect(container.querySelector('button').textContent).toContain('Open');
// Post may be dirty case.
setupHooks({
maybeIsPostDirty: true,
});
({ container } = render(<AMPDocumentStatusNotification />));
expect(container.innerHTML).toContain('Content may have changed.');
// Simulate button click.
fireEvent.click(container.querySelector('button'));
expect(openGeneralSidebar).toHaveBeenCalledTimes(1);
expect(closePublishSidebar).toHaveBeenCalledTimes(1);
});
it('renders a correct message if there are kept markup errors', () => {
setupHooks({
keptMarkupValidationErrorCount: 3,
});
const { container } = render(<AMPDocumentStatusNotification />);
expect(container.innerHTML).toContain('Enable AMP');
expect(container.innerHTML).toContain(
'AMP is blocked due to 3 validation issues marked as kept.'
);
expect(container.querySelector('svg')).not.toBeNull();
expect(container.querySelector('button').textContent).toContain(
'Review'
);
});
it('renders a correct message if there are unreviewed validation errors', () => {
setupHooks({
unreviewedValidationErrorCount: 1,
});
const { container } = render(<AMPDocumentStatusNotification />);
expect(container.innerHTML).toContain('Enable AMP');
expect(container.innerHTML).toContain(
'AMP is valid, but 1 issue needs review.'
);
expect(container.querySelector('svg')).not.toBeNull();
expect(container.querySelector('button').textContent).toContain(
'Review'
);
});
it('renders a correct message if there are no errors', () => {
setupHooks();
const { container } = render(<AMPDocumentStatusNotification />);
expect(container.innerHTML).toContain('Enable AMP');
expect(container.innerHTML).toContain(
'No AMP validation issues detected.'
);
expect(container.querySelector('svg')).not.toBeNull();
expect(container.querySelector('button')).toBeNull();
});
});