-
Notifications
You must be signed in to change notification settings - Fork 187
/
alert-service.test.ts
97 lines (77 loc) · 2.72 KB
/
alert-service.test.ts
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
import * as Sinon from 'sinon';
import {render} from '@testing-library/react';
import {ReactElement} from 'react';
import Alert from '../alert/alert';
import alert from './alert-service';
const SMALL_TICK = 1000;
const BIG_TICK = 2000;
const ALERT_SHOW_TIME = 100;
describe('Alert Service', () => {
let alertKey: string | number;
let clock: Sinon.SinonFakeTimers;
beforeEach(() => {
clock = sandbox.useFakeTimers({toFake: ['setTimeout']});
let rerender: (element: ReactElement) => void;
sandbox.stub(alert.reactRoot, 'render').callsFake(node => {
const element = node as ReactElement;
if (rerender == null) {
rerender = render(element).rerender;
} else {
rerender(element);
}
});
});
afterEach(() => {
alert.setDefaultTimeout(0);
alert.removeWithoutAnimation(alertKey);
});
it('Should show alert', () => {
alertKey = alert.message('foo');
const alertItem = alert._getShowingAlerts()[0];
'foo'.should.equal(alertItem.message);
Alert.Type.MESSAGE.should.equal(alertItem.type);
(0).should.equal(alertItem.timeout);
false.should.equal(alertItem.isClosing);
});
it('Should show message', () => {
alertKey = alert.message('foo');
Alert.Type.MESSAGE.should.equal(alert._getShowingAlerts()[0].type);
});
it('Should show error', () => {
alertKey = alert.error('foo');
Alert.Type.ERROR.should.equal(alert._getShowingAlerts()[0].type);
});
it('Should show warning', () => {
alertKey = alert.warning('foo');
Alert.Type.WARNING.should.equal(alert._getShowingAlerts()[0].type);
});
it('Should join same alerts and shake', () => {
alertKey = alert.message('foo');
alertKey = alert.message('foo');
alert._getShowingAlerts().length.should.equal(1);
true.should.equal(alert._getShowingAlerts()[0].isShaking);
});
it('Should remove alert after timeout', () => {
alertKey = alert.message('foo', ALERT_SHOW_TIME);
clock.tick(SMALL_TICK);
alert._getShowingAlerts().length.should.equal(0);
});
it('should remove alert without animation', () => {
alertKey = alert.message('foo');
alert.removeWithoutAnimation(alertKey);
alert._getShowingAlerts().length.should.equal(0);
});
it('should remove alert after animation', () => {
alertKey = alert.message('foo');
alert.remove(alertKey);
true.should.equal(alert._getShowingAlerts().filter(it => it.key === alertKey)[0].isClosing);
clock.tick(SMALL_TICK);
alert._getShowingAlerts().length.should.equal(0);
});
it('should allow configuring default timeout', () => {
alert.setDefaultTimeout(SMALL_TICK);
alertKey = alert.message('foo');
clock.tick(BIG_TICK);
alert._getShowingAlerts().length.should.equal(0);
});
});