blob: 08d031a4fa7a95023603f14483335743bb2a77bf (
plain)
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
|
import CaptchaModal from '~/captcha/captcha_modal.vue';
import { waitForCaptchaToBeSolved } from '~/captcha/wait_for_captcha_to_be_solved';
jest.mock('~/captcha/captcha_modal.vue', () => ({
mounted: jest.fn(),
render(h) {
return h('div', { attrs: { id: 'mock-modal' } });
},
}));
describe('waitForCaptchaToBeSolved', () => {
const response = 'CAPTCHA_RESPONSE';
const findModal = () => document.querySelector('#mock-modal');
it('opens a modal, resolves with captcha response on success', async () => {
CaptchaModal.mounted.mockImplementationOnce(function mounted() {
requestAnimationFrame(() => {
this.$emit('receivedCaptchaResponse', response);
this.$emit('hidden');
});
});
expect(findModal()).toBeNull();
const promise = waitForCaptchaToBeSolved('FOO');
expect(findModal()).not.toBeNull();
const result = await promise;
expect(result).toEqual(response);
expect(findModal()).toBeNull();
expect(document.body.innerHTML).toEqual('');
});
it("opens a modal, rejects with error in case the captcha isn't solved", async () => {
CaptchaModal.mounted.mockImplementationOnce(function mounted() {
requestAnimationFrame(() => {
this.$emit('receivedCaptchaResponse', null);
this.$emit('hidden');
});
});
expect(findModal()).toBeNull();
const promise = waitForCaptchaToBeSolved('FOO');
expect(findModal()).not.toBeNull();
await expect(promise).rejects.toThrow(/You must solve the CAPTCHA in order to submit/);
expect(findModal()).toBeNull();
expect(document.body.innerHTML).toEqual('');
});
});
|