2021-11-11 11:23:49 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
|
|
|
import ReviewBar from '~/batch_comments/components/review_bar.vue';
|
|
|
|
import { REVIEW_BAR_VISIBLE_CLASS_NAME } from '~/batch_comments/constants';
|
|
|
|
import createStore from '../create_batch_comments_store';
|
|
|
|
|
|
|
|
describe('Batch comments review bar component', () => {
|
|
|
|
let store;
|
|
|
|
let wrapper;
|
2022-08-27 11:52:29 +05:30
|
|
|
let addEventListenerSpy;
|
|
|
|
let removeEventListenerSpy;
|
2021-11-11 11:23:49 +05:30
|
|
|
|
|
|
|
const createComponent = (propsData = {}) => {
|
|
|
|
store = createStore();
|
|
|
|
|
|
|
|
wrapper = shallowMount(ReviewBar, {
|
|
|
|
store,
|
|
|
|
propsData,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
document.body.className = '';
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
addEventListenerSpy = jest.spyOn(window, 'addEventListener');
|
|
|
|
removeEventListenerSpy = jest.spyOn(window, 'removeEventListener');
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-08-27 11:52:29 +05:30
|
|
|
addEventListenerSpy.mockRestore();
|
|
|
|
removeEventListenerSpy.mockRestore();
|
2021-11-11 11:23:49 +05:30
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
describe('when mounted', () => {
|
|
|
|
it('it adds review-bar-visible class to body', async () => {
|
|
|
|
expect(document.body.classList.contains(REVIEW_BAR_VISIBLE_CLASS_NAME)).toBe(false);
|
|
|
|
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
expect(document.body.classList.contains(REVIEW_BAR_VISIBLE_CLASS_NAME)).toBe(true);
|
|
|
|
});
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
it('it adds a blocking handler to the `beforeunload` window event', () => {
|
|
|
|
expect(addEventListenerSpy).not.toBeCalled();
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
createComponent();
|
|
|
|
|
|
|
|
expect(addEventListenerSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(addEventListenerSpy).toBeCalledWith('beforeunload', expect.any(Function), {
|
|
|
|
capture: true,
|
|
|
|
});
|
|
|
|
});
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
describe('before destroyed', () => {
|
|
|
|
it('it removes review-bar-visible class to body', async () => {
|
|
|
|
createComponent();
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
wrapper.destroy();
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
expect(document.body.classList.contains(REVIEW_BAR_VISIBLE_CLASS_NAME)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('it removes the blocking handler from the `beforeunload` window event', () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
expect(removeEventListenerSpy).not.toBeCalled();
|
|
|
|
|
|
|
|
wrapper.destroy();
|
|
|
|
|
|
|
|
expect(removeEventListenerSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(removeEventListenerSpy).toBeCalledWith('beforeunload', expect.any(Function), {
|
|
|
|
capture: true,
|
|
|
|
});
|
|
|
|
});
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
});
|