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

97 lines
3.2 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';
2022-09-06 12:03:37 +00:00
import Sidebar from './Sidebar.vue';
describe('Sidebar.vue', () => {
let wrapper;
beforeEach(() => {
wrapper = mount(Sidebar, {
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
},
props: {
2023-01-03 23:14:50 +00:00
visible: false,
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
wrapper.setProps({ visible: true });
});
afterEach(() => {
vi.clearAllMocks();
2022-09-06 12:03:37 +00:00
});
2023-01-03 23:14:50 +00:00
it('When component is mounted, sidebar should be exist', () => {
2022-09-06 12:03:37 +00:00
expect(wrapper.find('.p-sidebar.p-component').exists()).toBe(true);
});
2023-01-03 23:14:50 +00:00
it('When mask element triggered, sidebar should be hide', async () => {
const unbindOutsideClickListenerSpy = vi.spyOn(wrapper.vm, 'unbindOutsideClickListener');
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);
expect(unbindOutsideClickListenerSpy).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);
expect(wrapper.vm.maskVisible).toBeTruthy();
expect(focusSpy).toHaveBeenCalled();
2022-09-06 12:03:37 +00:00
});
2023-01-03 23:14:50 +00:00
it('When transition trigger to onLeave, unbindOutsideClickListener should be triggered', async () => {
const unbindOutsideClickListenerSpy = vi.spyOn(wrapper.vm, 'unbindOutsideClickListener');
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);
expect(unbindOutsideClickListenerSpy).toHaveBeenCalled();
2022-09-06 12:03:37 +00:00
});
2022-12-08 11:04:25 +00:00
2023-01-03 23:14:50 +00:00
it('When transition trigger to onAfterEnter, bindOutsideClickListener should be triggered', async () => {
const bindOutsideClickListenerSpy = vi.spyOn(wrapper.vm, 'bindOutsideClickListener');
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-03 23:14:50 +00:00
expect(bindOutsideClickListenerSpy).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
});
2022-09-14 11:26:01 +00:00
});