primevue-mirror/components/lib/sidebar/Sidebar.spec.js

123 lines
3.9 KiB
JavaScript
Raw Normal View History

2022-09-06 12:03:37 +00:00
import { mount } from '@vue/test-utils';
2022-12-08 11:04:25 +00:00
import PrimeVue from 'primevue/config';
2022-12-27 14:17:26 +00:00
import { describe, expect, it } from 'vitest';
2023-01-16 08:48:16 +00:00
import DomHandler from '../utils/DomHandler';
2022-09-06 12:03:37 +00:00
import Sidebar from './Sidebar.vue';
describe('Sidebar.vue', () => {
let wrapper;
2023-01-16 08:48:16 +00:00
beforeEach(async () => {
2022-09-06 12:03:37 +00:00
wrapper = mount(Sidebar, {
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
},
props: {
2023-01-16 08:48:16 +00:00
visible: true,
2022-09-06 12:03:37 +00:00
bazeZIndex: 1000
},
slots: {
2022-12-27 14:17:26 +00:00
default: `<h3>Left Sidebar</h3>`,
header: `<span class="header">Header Template</span>`
2022-09-06 12:03:37 +00:00
}
});
2023-01-03 23:14:50 +00:00
});
afterEach(() => {
vi.clearAllMocks();
2022-09-06 12:03:37 +00:00
});
2023-01-03 23:14:50 +00:00
it('When mask element triggered, sidebar should be hide', async () => {
2023-01-16 10:24:50 +00:00
const hideSpy = vi.spyOn(wrapper.vm, 'hide');
2022-09-06 12:03:37 +00:00
2023-01-03 23:14:50 +00:00
await wrapper.find('.p-sidebar-mask').trigger('mousedown');
2022-09-06 12:03:37 +00:00
2023-01-03 23:14:50 +00:00
expect(wrapper.emitted()['update:visible'].length).toBe(1);
2023-01-16 10:24:50 +00:00
expect(hideSpy).toHaveBeenCalled();
2022-09-06 12:03:37 +00:00
});
2023-01-03 23:14:50 +00:00
it('When transition trigger to onEnter, sidebar should be visible', async () => {
const focusSpy = vi.spyOn(wrapper.vm, 'focus');
await wrapper.vm.onEnter();
2022-09-06 12:03:37 +00:00
2023-01-03 23:14:50 +00:00
expect(wrapper.emitted().show.length).toBe(1);
2023-01-16 08:48:16 +00:00
expect(wrapper.vm.containerVisible).toBeTruthy();
2023-01-03 23:14:50 +00:00
expect(focusSpy).toHaveBeenCalled();
2022-09-06 12:03:37 +00:00
});
2023-01-16 10:24:50 +00:00
it('When transition trigger to onLeave, hide should be triggered', async () => {
2023-01-03 23:14:50 +00:00
await wrapper.vm.onLeave();
2022-09-06 12:03:37 +00:00
2023-01-03 23:14:50 +00:00
expect(wrapper.emitted().hide.length).toBe(1);
2022-09-06 12:03:37 +00:00
});
2022-12-08 11:04:25 +00:00
2023-01-16 10:24:50 +00:00
it('When transition trigger to onAfterEnter, enableDocumentSettings should be triggered', async () => {
const enableDocumentSettingsSpy = vi.spyOn(wrapper.vm, 'enableDocumentSettings');
2022-12-08 11:04:25 +00:00
2023-01-03 23:14:50 +00:00
await wrapper.vm.onAfterEnter();
2022-12-27 14:17:26 +00:00
2023-01-16 10:24:50 +00:00
expect(enableDocumentSettingsSpy).toHaveBeenCalled();
2022-12-27 14:17:26 +00:00
});
2023-01-03 23:14:50 +00:00
it('When keydown is triggered , hide method should be triggered', async () => {
const hideSpy = vi.spyOn(wrapper.vm, 'hide');
2022-12-27 14:17:26 +00:00
2023-01-03 23:14:50 +00:00
await wrapper.find('.p-sidebar').trigger('keydown', { code: 'Escape' });
2022-12-27 14:17:26 +00:00
2023-01-03 23:14:50 +00:00
expect(hideSpy).toHaveBeenCalled();
2022-12-27 14:17:26 +00:00
});
2023-01-03 23:14:50 +00:00
it('When keydown is triggered , hide method should be triggered', async () => {
const hideSpy = vi.spyOn(wrapper.vm, 'hide');
2022-12-27 14:17:26 +00:00
2023-01-03 23:14:50 +00:00
await wrapper.find('.p-sidebar-close').trigger('click');
2022-12-27 14:17:26 +00:00
2023-01-03 23:14:50 +00:00
expect(hideSpy).toHaveBeenCalled();
2022-12-27 14:17:26 +00:00
});
2023-01-03 23:14:50 +00:00
it('When component is unmount , unbindOutsideClickListenerSpy method should be triggered', async () => {
const unbindOutsideClickListenerSpy = vi.spyOn(wrapper.vm, 'unbindOutsideClickListener');
await wrapper.unmount();
expect(unbindOutsideClickListenerSpy).toHaveBeenCalled();
expect(Sidebar.container).toBe(null);
2022-12-27 14:17:26 +00:00
});
2023-01-16 08:48:16 +00:00
it('When hide is triggered , removeClass util should be called', async () => {
const removeClassSpy = vi.spyOn(DomHandler, 'removeClass');
await wrapper.setProps({ blockScroll: true });
2023-01-16 10:24:50 +00:00
wrapper.vm.disableDocumentSettings();
2023-01-16 08:48:16 +00:00
expect(removeClassSpy).toHaveBeenCalled();
});
it('When onEnter is triggered , addClass util should be called', async () => {
const addClassSpy = vi.spyOn(DomHandler, 'addClass');
await wrapper.setProps({ blockScroll: true });
2023-01-16 10:24:50 +00:00
wrapper.vm.enableDocumentSettings();
2023-01-16 08:48:16 +00:00
expect(addClassSpy).toHaveBeenCalled();
});
it('When onBeforeLeave is triggered , addClass util should be called', async () => {
const addClassSpy = vi.spyOn(DomHandler, 'addClass');
await wrapper.setProps({ modal: true });
wrapper.vm.onBeforeLeave();
expect(addClassSpy).toHaveBeenCalled();
});
it('When onAfterLeave is triggered , containerVisible should be false', async () => {
wrapper.vm.onAfterLeave();
expect(wrapper.vm.containerVisible).toBeFalsy();
});
2022-09-14 11:26:01 +00:00
});