-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathErrorBar.test.tsx
More file actions
196 lines (169 loc) · 6.68 KB
/
ErrorBar.test.tsx
File metadata and controls
196 lines (169 loc) · 6.68 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import React from 'react';
import { act } from 'react-dom/test-utils';
import { initializeIcons, MessageBar } from '@fluentui/react';
import { ActiveErrorMessage, ErrorBar, ErrorBarProps } from './ErrorBar';
import Enzyme, { ReactWrapper, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
const ONE_DAY_MILLISECONDS = 24 * 3600 * 1000;
describe('ErrorBar self-clearing error', () => {
beforeAll(() => {
Enzyme.configure({ adapter: new Adapter() });
initializeIcons();
});
test('error bar is hidden when an error with timestamp is cleared', () => {
const root = mountErrorBarWithDefaults();
setAccessDeniedErrorAt(root, new Date(Date.now()));
expect(messageBarCount(root)).toBe(1);
setNoActiveError(root);
expect(messageBarCount(root)).toBe(0);
setAccessDeniedErrorAt(root, new Date(Date.now()));
expect(messageBarCount(root)).toBe(1);
});
test('error bar is hidden when an error without timestamp is cleared', () => {
const root = mountErrorBarWithDefaults();
setAccessDeniedErrorWithoutTimestamp(root);
expect(messageBarCount(root)).toBe(1);
setNoActiveError(root);
expect(messageBarCount(root)).toBe(0);
setAccessDeniedErrorWithoutTimestamp(root);
expect(messageBarCount(root)).toBe(1);
});
});
describe('ErrorBar dismissal for errors with timestamp', () => {
beforeAll(() => {
Enzyme.configure({ adapter: new Adapter() });
initializeIcons();
});
it('error can be dimissed', () => {
const root = mountErrorBarWithDefaults();
setAccessDeniedErrorAt(root, new Date(Date.now()));
expect(messageBarCount(root)).toBe(1);
simulateDismissOneError(root);
expect(messageBarCount(root)).toBe(0);
});
it('new error after dismissal is shown', () => {
const root = mountErrorBarWithDefaults();
setAccessDeniedErrorAt(root, new Date(Date.now()));
simulateDismissOneError(root);
setAccessDeniedErrorAt(root, new Date(Date.now() + ONE_DAY_MILLISECONDS));
expect(messageBarCount(root)).toBe(1);
});
it('old error after dismissal is not shown', () => {
const root = mountErrorBarWithDefaults();
setAccessDeniedErrorAt(root, new Date(Date.now()));
simulateDismissOneError(root);
setAccessDeniedErrorAt(root, new Date(Date.now() - ONE_DAY_MILLISECONDS));
expect(messageBarCount(root)).toBe(0);
});
it('old error after dismissal and intervening non-error is not shown', () => {
const root = mountErrorBarWithDefaults();
setAccessDeniedErrorAt(root, new Date(Date.now()));
simulateDismissOneError(root);
setNoActiveError(root);
setAccessDeniedErrorAt(root, new Date(Date.now() - ONE_DAY_MILLISECONDS));
expect(messageBarCount(root)).toBe(0);
});
});
describe('ErrorBar dismissal for errors without timestamp', () => {
beforeAll(() => {
Enzyme.configure({ adapter: new Adapter() });
initializeIcons();
});
it('error can be dimissed', () => {
const root = mountErrorBarWithDefaults();
setAccessDeniedErrorWithoutTimestamp(root);
expect(messageBarCount(root)).toBe(1);
simulateDismissOneError(root);
expect(messageBarCount(root)).toBe(0);
});
it('new error after dismissal is not shown', () => {
const root = mountErrorBarWithDefaults();
setAccessDeniedErrorWithoutTimestamp(root);
simulateDismissOneError(root);
setAccessDeniedErrorWithoutTimestamp(root);
expect(messageBarCount(root)).toBe(0);
});
it('new error after dismissal and intervening non-error is shown', () => {
const root = mountErrorBarWithDefaults();
setAccessDeniedErrorWithoutTimestamp(root);
simulateDismissOneError(root);
setNoActiveError(root);
setAccessDeniedErrorWithoutTimestamp(root);
expect(messageBarCount(root)).toBe(1);
});
});
describe('ErrorBar dismissal with multiple errors', () => {
beforeAll(() => {
Enzyme.configure({ adapter: new Adapter() });
initializeIcons();
});
it('clearing an error with multiple errors leaves other errors untouched', () => {
const root = mountErrorBarWithDefaults();
setActiveErrors(root, [{ type: 'accessDenied', timestamp: new Date(Date.now()) }, { type: 'muteGeneric' }]);
expect(messageBarCount(root)).toBe(2);
simulateDismissOneError(root);
expect(messageBarCount(root)).toBe(1);
});
});
describe('ErrorBar handling of errors from previous call or chat', () => {
beforeAll(() => {
Enzyme.configure({ adapter: new Adapter() });
initializeIcons();
});
it('shows all old errors by default', () => {
const oldErrors: ActiveErrorMessage[] = [
// Make sure old error is in the past.
{ type: 'accessDenied', timestamp: new Date(Date.now() - 10) },
{ type: 'muteGeneric' }
];
const root = mountErrorBarWithDefaults();
setActiveErrors(root, oldErrors);
expect(messageBarCount(root)).toBe(2);
});
it('does not show old errors with timestamp when ignorePremountErrors is set', () => {
// Make sure old error is in the past.
const oldErrors: ActiveErrorMessage[] = [{ type: 'accessDenied', timestamp: new Date(Date.now() - 10) }];
const root = mountErrorBarWithDefaults({ ignorePremountErrors: true });
setActiveErrors(root, oldErrors);
expect(messageBarCount(root)).toBe(0);
});
it('shows old errors without timestamp when ignorePremountErrors is set', () => {
const oldErrors: ActiveErrorMessage[] = [{ type: 'muteGeneric' }];
const root = mountErrorBarWithDefaults({ ignorePremountErrors: true });
setActiveErrors(root, oldErrors);
expect(messageBarCount(root)).toBe(1);
});
});
const mountErrorBarWithDefaults = (props?: Partial<ErrorBarProps>): ReactWrapper => {
const mergedProps: ErrorBarProps = {
activeErrorMessages: [],
...(props ?? {})
};
let root;
act(() => {
root = mount(<ErrorBar {...mergedProps} />);
});
return root;
};
const messageBarCount = (root: ReactWrapper): number => root.find(MessageBar).length;
const simulateDismissOneError = (root: ReactWrapper): void => {
const messageBar = root.find(MessageBar).at(0);
const button = messageBar.find('button').at(0);
button.simulate('click');
};
const setAccessDeniedErrorAt = (root: ReactWrapper, timestamp: Date): void =>
setActiveErrors(root, [{ type: 'accessDenied', timestamp }]);
const setAccessDeniedErrorWithoutTimestamp = (root: ReactWrapper): void =>
setActiveErrors(root, [{ type: 'accessDenied' }]);
const setActiveErrors = (root: ReactWrapper, activeErrorMessages: ActiveErrorMessage[]): void => {
act(() => {
root.setProps({ activeErrorMessages: activeErrorMessages });
});
};
const setNoActiveError = (root: ReactWrapper): void => {
act(() => {
root.setProps({ activeErrorMessages: [] });
});
};