-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathalert.spec.js
More file actions
210 lines (185 loc) · 6.69 KB
/
alert.spec.js
File metadata and controls
210 lines (185 loc) · 6.69 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
import sinon from 'sinon';
import { T } from 'ramda';
import { nextTick, ref } from 'vue';
import Home from '../src/components/home.vue';
import createAlerts from '../src/container/alerts';
import { useAlert } from '../src/alert';
import { noop } from '../src/util/util';
import { block, wait } from './util/util';
import { load } from './util/http';
import { mockLogin } from './util/session';
import { withSetup } from './util/lifecycle';
describe('createAlert()', () => {
for (const type of ['success', 'info', 'danger']) {
describe(`${type}()`, () => {
it('updates the data', () => {
const { alert } = createAlerts();
alert[type]('Something happened!');
alert.type.should.equal(type);
alert.message.should.equal('Something happened!');
alert.state.should.be.true;
});
});
}
describe('CTA', () => {
it('updates the data via show().cta()', () => {
const { alert } = createAlerts();
should.not.exist(alert.cta);
alert.info('Something happened!');
should.not.exist(alert.cta);
const fake = sinon.fake();
alert.info('Something else happened!').cta('Click here', fake);
alert.cta.text.should.equal('Click here');
alert.cta.handler();
fake.called.should.be.true;
});
it('hides the alert if the CTA handler returns true', async () => {
const { alert } = createAlerts();
// Handler returns `undefined`; don't hide.
alert.info('Something happened!').cta('Click here', noop);
alert.cta.handler();
await nextTick();
alert.state.should.be.true;
// Handler returns `true`; hide.
alert.info('Something happened!').cta('Click here', T);
alert.state.should.be.true;
alert.cta.handler();
await nextTick();
alert.state.should.be.false;
});
it('supports an async CTA handler', async () => {
const { alert } = createAlerts();
const fromLock = (lock) => () => lock.then(T);
// Async handler that resolves
const [lock1, unlock1] = block();
alert.info('Something happened!').cta('Click here', fromLock(lock1));
alert.cta.handler();
alert.state.should.be.true;
alert.cta.pending.should.be.true;
unlock1();
await wait();
alert.state.should.be.false;
should.not.exist(alert.cta);
// Async handler that rejects
const [lock2, , fail2] = block();
alert.info('Something happened!').cta('Click here', fromLock(lock2));
alert.cta.handler();
alert.state.should.be.true;
alert.cta.pending.should.be.true;
fail2();
await wait();
alert.state.should.be.true;
alert.cta.pending.should.be.false;
alert.last.hide();
// First handler resolves during second handler
const [lock3, unlock3] = block();
alert.info('Something happened!').cta('Click here', fromLock(lock3));
alert.cta.handler();
const [lock4, unlock4] = block();
alert.info('Something happened!').cta('Click here', fromLock(lock4));
alert.cta.handler();
unlock3();
await wait();
alert.state.should.be.true;
alert.cta.pending.should.be.true;
unlock4();
await wait();
alert.state.should.be.false;
should.not.exist(alert.cta);
// First handler rejects during second handler
const [lock5, , fail5] = block();
alert.info('Something happened!').cta('Click here', fromLock(lock5));
alert.cta.handler();
const [lock6, unlock6] = block();
alert.info('Something happened!').cta('Click here', fromLock(lock6));
alert.cta.handler();
fail5();
await wait();
alert.state.should.be.true;
alert.cta.pending.should.be.true;
unlock6();
await wait();
alert.state.should.be.false;
should.not.exist(alert.cta);
});
});
describe('hide()', () => {
it('updates the data', () => {
const { alert } = createAlerts();
alert.info('Something happened!').cta('Click here', noop);
alert.last.hide();
alert.state.should.be.false;
should.not.exist(alert.message);
should.not.exist(alert.cta);
});
});
});
describe('useAlert()', () => {
describe('hiding alert after 7 seconds', () => {
it('hides a success alert after 7 seconds', async () => {
const clock = sinon.useFakeTimers();
const { alert, toast } = createAlerts();
withSetup(() => useAlert(toast, ref(null)));
alert.success('Something good!');
await clock.tickAsync(6999);
alert.state.should.be.true;
await clock.tickAsync(1);
alert.state.should.be.false;
});
it('does not hide a non-success alert', async () => {
const clock = sinon.useFakeTimers();
const { alert, toast } = createAlerts();
withSetup(() => useAlert(toast, ref(null)));
alert.info('Something happened!');
await clock.tickAsync(7000);
alert.state.should.be.true;
});
it('restarts the clock for each new alert', async () => {
const clock = sinon.useFakeTimers();
const { alert, toast } = createAlerts();
withSetup(() => useAlert(toast, ref(null)));
alert.success('Something good!');
await clock.tickAsync(6999);
alert.state.should.be.true;
alert.success('Something else good!');
await clock.tickAsync(1);
alert.state.should.be.true;
await clock.tickAsync(6998);
alert.state.should.be.true;
await clock.tickAsync(1);
alert.state.should.be.false;
alert.success('Something good again!');
await clock.tickAsync(6999);
alert.info('Something happened!');
await clock.tickAsync(1);
alert.state.should.be.true;
await clock.tickAsync(6999);
alert.state.should.be.true;
});
});
describe('hiding alert after user clicks an a[target="_blank"]', () => {
beforeEach(mockLogin);
const preventDefault = (event) => { event.preventDefault(); };
beforeAll(() => {
document.addEventListener('click', preventDefault);
});
afterAll(() => {
document.removeEventListener('click', preventDefault);
});
it('hides the alert', async () => {
const app = await load('/', { attachTo: document.body });
app.vm.$container.alert.info('Something happened!');
await app.getComponent(Home).get('a[target="_blank"]').trigger('click');
app.should.not.alert();
});
it('does not hide the alert if it was shown after the click', async () => {
const app = await load('/', { attachTo: document.body });
const a = app.getComponent(Home).get('a[target="_blank"]');
a.element.addEventListener('click', () => {
app.vm.$container.alert.info('Something happened!');
});
a.trigger('click');
app.should.alert();
});
});
});