debian-mirror-gitlab/spec/frontend/captcha/captcha_modal_spec.js

160 lines
5.1 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { GlModal } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { stubComponent } from 'helpers/stub_component';
import CaptchaModal from '~/captcha/captcha_modal.vue';
import { initRecaptchaScript } from '~/captcha/init_recaptcha_script';
jest.mock('~/captcha/init_recaptcha_script');
describe('Captcha Modal', () => {
let wrapper;
let grecaptcha;
const captchaSiteKey = 'abc123';
2023-05-27 22:25:52 +05:30
const showSpy = jest.fn();
const hideSpy = jest.fn();
2021-03-11 19:13:27 +05:30
function createComponent({ props = {} } = {}) {
wrapper = shallowMount(CaptchaModal, {
propsData: {
captchaSiteKey,
...props,
},
stubs: {
2023-05-27 22:25:52 +05:30
GlModal: stubComponent(GlModal, {
methods: {
show: showSpy,
hide: hideSpy,
},
}),
2021-03-11 19:13:27 +05:30
},
});
}
2023-05-27 22:25:52 +05:30
const findGlModal = () => wrapper.findComponent(GlModal);
2021-03-11 19:13:27 +05:30
beforeEach(() => {
grecaptcha = {
render: jest.fn(),
};
initRecaptchaScript.mockResolvedValue(grecaptcha);
});
describe('rendering', () => {
2023-05-27 22:25:52 +05:30
beforeEach(() => {
createComponent();
});
2021-03-11 19:13:27 +05:30
it('renders', () => {
2023-05-27 22:25:52 +05:30
expect(findGlModal().exists()).toBe(true);
2021-03-11 19:13:27 +05:30
});
it('assigns the modal a unique ID', () => {
2023-05-27 22:25:52 +05:30
const firstInstanceModalId = findGlModal().props('modalId');
2021-03-11 19:13:27 +05:30
createComponent();
const secondInstanceModalId = findGlModal().props('modalId');
expect(firstInstanceModalId).not.toEqual(secondInstanceModalId);
});
});
describe('functionality', () => {
describe('when modal is shown', () => {
describe('when initRecaptchaScript promise resolves successfully', () => {
2023-06-20 00:43:36 +05:30
beforeEach(() => {
2023-05-27 22:25:52 +05:30
createComponent({ props: { needsCaptchaResponse: true } });
findGlModal().vm.$emit('shown');
2021-03-11 19:13:27 +05:30
});
2023-06-20 00:43:36 +05:30
it('shows modal', () => {
2023-05-27 22:25:52 +05:30
expect(showSpy).toHaveBeenCalled();
2021-03-11 19:13:27 +05:30
});
it('renders window.grecaptcha', () => {
expect(grecaptcha.render).toHaveBeenCalledWith(wrapper.vm.$refs.captcha, {
sitekey: captchaSiteKey,
callback: expect.any(Function),
});
});
describe('then the user solves the captcha', () => {
const captchaResponse = 'a captcha response';
beforeEach(() => {
// simulate the grecaptcha library invoking the callback
const { callback } = grecaptcha.render.mock.calls[0][1];
callback(captchaResponse);
});
it('emits receivedCaptchaResponse exactly once with the captcha response', () => {
expect(wrapper.emitted('receivedCaptchaResponse')).toEqual([[captchaResponse]]);
});
2023-06-20 00:43:36 +05:30
it('hides modal with null trigger', () => {
2021-03-11 19:13:27 +05:30
// Assert that hide is called with zero args, so that we don't trigger the logic
// for hiding the modal via cancel, esc, headerclose, etc, without a captcha response
2023-05-27 22:25:52 +05:30
expect(hideSpy).toHaveBeenCalledWith();
2021-03-11 19:13:27 +05:30
});
});
describe('then the user hides the modal without solving the captcha', () => {
// Even though we don't explicitly check for these trigger values, these are the
// currently supported ones which can be emitted.
// See https://bootstrap-vue.org/docs/components/modal#prevent-closing
describe.each`
trigger | expected
${'cancel'} | ${[[null]]}
${'esc'} | ${[[null]]}
${'backdrop'} | ${[[null]]}
${'headerclose'} | ${[[null]]}
`('using the $trigger trigger', ({ trigger, expected }) => {
beforeEach(() => {
const bvModalEvent = {
trigger,
};
2023-05-27 22:25:52 +05:30
findGlModal().vm.$emit('hide', bvModalEvent);
2021-03-11 19:13:27 +05:30
});
it(`emits receivedCaptchaResponse with ${JSON.stringify(expected)}`, () => {
expect(wrapper.emitted('receivedCaptchaResponse')).toEqual(expected);
});
});
});
});
describe('when initRecaptchaScript promise rejects', () => {
const fakeError = {};
beforeEach(() => {
2023-05-27 22:25:52 +05:30
createComponent({
props: { needsCaptchaResponse: true },
});
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
initRecaptchaScript.mockImplementation(() => Promise.reject(fakeError));
2021-03-11 19:13:27 +05:30
jest.spyOn(console, 'error').mockImplementation();
2023-05-27 22:25:52 +05:30
findGlModal().vm.$emit('shown');
2021-03-11 19:13:27 +05:30
});
it('emits receivedCaptchaResponse exactly once with null', () => {
expect(wrapper.emitted('receivedCaptchaResponse')).toEqual([[null]]);
});
2023-05-27 22:25:52 +05:30
it('hides modal with null trigger', () => {
2021-03-11 19:13:27 +05:30
// Assert that hide is called with zero args, so that we don't trigger the logic
// for hiding the modal via cancel, esc, headerclose, etc, without a captcha response
2023-05-27 22:25:52 +05:30
expect(hideSpy).toHaveBeenCalledWith();
2021-03-11 19:13:27 +05:30
});
it('calls console.error with a message and the exception', () => {
// eslint-disable-next-line no-console
expect(console.error).toHaveBeenCalledWith(
expect.stringMatching(/exception.*captcha/),
fakeError,
);
});
});
});
});
});