primevue-mirror/components/lib/select/Select.spec.js

370 lines
12 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-08 14:13:16 +00:00
import { h } from 'vue';
2024-04-18 14:17:41 +00:00
import Select from './Select.vue';
2022-09-06 12:03:37 +00:00
2024-04-18 14:17:41 +00:00
describe('Select.vue', () => {
2022-09-14 11:26:01 +00:00
let wrapper;
2022-09-06 12:03:37 +00:00
beforeEach(async () => {
2024-04-18 14:17:41 +00:00
wrapper = mount(Select, {
2022-09-06 12:03:37 +00:00
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
}
});
await wrapper.trigger('click');
});
2024-04-18 14:17:41 +00:00
it('should Select exist', () => {
expect(wrapper.find('.p-select.p-component').exists()).toBe(true);
expect(wrapper.find('.p-select-panel').exists()).toBe(true);
expect(wrapper.find('.p-select-empty-message').exists()).toBe(true);
2022-09-06 12:03:37 +00:00
expect(wrapper.find('.p-inputwrapper-filled').exists()).toBe(false);
2022-09-14 14:26:41 +00:00
expect(wrapper.find('.p-inputwrapper-focus').exists()).toBe(true);
2022-09-14 11:26:01 +00:00
});
2022-09-06 12:03:37 +00:00
});
describe('option checks', () => {
let wrapper;
2022-09-14 11:26:01 +00:00
beforeEach(async () => {
2024-04-18 14:17:41 +00:00
wrapper = mount(Select, {
2022-09-06 12:03:37 +00:00
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
},
props: {
options: [
2022-09-14 11:26:01 +00:00
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
2022-09-06 12:03:37 +00:00
],
optionLabel: 'name',
optionValue: 'code',
placeholder: 'Select a City'
}
});
await wrapper.trigger('click');
});
it('should show the options', () => {
2024-04-18 14:17:41 +00:00
expect(wrapper.find('.p-select-label.p-placeholder').text()).toBe('Select a City');
expect(wrapper.find('.p-select-items-wrapper > .p-select-items').exists()).toBe(true);
expect(wrapper.find('.p-select-item').exists()).toBe(true);
expect(wrapper.findAll('.p-select-item').length).toBe(5);
expect(wrapper.findAll('.p-select-item')[0].text()).toBe('New York');
2022-09-06 12:03:37 +00:00
});
});
2022-12-08 11:04:25 +00:00
describe('clear checks', () => {
let wrapper;
beforeEach(async () => {
2024-04-18 14:17:41 +00:00
wrapper = mount(Select, {
2022-12-08 11:04:25 +00:00
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
},
props: {
clearIcon: 'pi pi-discord',
modelValue: 'value',
showClear: true
}
});
await wrapper.trigger('click');
});
it('should have correct icon', () => {
2024-04-18 14:17:41 +00:00
expect(wrapper.find('.p-select-clear-icon').classes()).toContain('pi-discord');
2022-12-08 11:04:25 +00:00
});
it('should clear with delete key', async () => {
const updateModelSpy = vi.spyOn(wrapper.vm, 'updateModel');
2024-04-18 14:17:41 +00:00
await wrapper.find('.p-select-label.p-inputtext').trigger('keydown', { code: 'Delete' });
expect(updateModelSpy).toHaveBeenCalledOnce();
expect(updateModelSpy).toHaveBeenCalledWith(expect.any(KeyboardEvent), null);
});
2022-12-08 11:04:25 +00:00
});
2022-09-06 12:03:37 +00:00
describe('editable checks', () => {
let wrapper;
2022-09-14 11:26:01 +00:00
beforeEach(async () => {
2024-04-18 14:17:41 +00:00
wrapper = mount(Select, {
2022-09-06 12:03:37 +00:00
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
},
props: {
options: [
2022-09-14 11:26:01 +00:00
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
2022-09-06 12:03:37 +00:00
],
optionLabel: 'name',
optionValue: 'code',
placeholder: 'Select a City',
editable: true
}
});
await wrapper.trigger('click');
});
it('should show the options', () => {
2024-04-18 14:17:41 +00:00
expect(wrapper.find('.p-select-label.p-placeholder').exists()).toBe(false);
expect(wrapper.find('.p-select-label.p-inputtext').exists()).toBe(true);
2022-09-06 12:03:37 +00:00
});
});
describe('option groups checks', () => {
let wrapper;
2022-09-14 11:26:01 +00:00
beforeEach(async () => {
2024-04-18 14:17:41 +00:00
wrapper = mount(Select, {
2022-09-06 12:03:37 +00:00
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
},
props: {
options: [
{
2022-09-14 11:26:01 +00:00
label: 'Germany',
code: 'DE',
2022-09-06 12:03:37 +00:00
items: [
2022-09-14 11:26:01 +00:00
{ label: 'Berlin', value: 'Berlin' },
{ label: 'Frankfurt', value: 'Frankfurt' },
{ label: 'Hamburg', value: 'Hamburg' },
{ label: 'Munich', value: 'Munich' }
2022-09-06 12:03:37 +00:00
]
},
{
2022-09-14 11:26:01 +00:00
label: 'USA',
code: 'US',
2022-09-06 12:03:37 +00:00
items: [
2022-09-14 11:26:01 +00:00
{ label: 'Chicago', value: 'Chicago' },
{ label: 'Los Angeles', value: 'Los Angeles' },
{ label: 'New York', value: 'New York' },
{ label: 'San Francisco', value: 'San Francisco' }
2022-09-06 12:03:37 +00:00
]
},
{
2022-09-14 11:26:01 +00:00
label: 'Japan',
code: 'JP',
2022-09-06 12:03:37 +00:00
items: [
2022-09-14 11:26:01 +00:00
{ label: 'Kyoto', value: 'Kyoto' },
{ label: 'Osaka', value: 'Osaka' },
{ label: 'Tokyo', value: 'Tokyo' },
{ label: 'Yokohama', value: 'Yokohama' }
2022-09-06 12:03:37 +00:00
]
}
],
optionLabel: 'label',
optionGroupLabel: 'label',
optionGroupChildren: 'items'
}
});
await wrapper.trigger('click');
});
it('should show the option groups', () => {
2024-04-18 14:17:41 +00:00
expect(wrapper.findAll('.p-select-item-group').length).toBe(3);
expect(wrapper.findAll('.p-select-item-group')[0].text()).toBe('Germany');
2022-09-06 12:03:37 +00:00
});
});
describe('templating checks', () => {
let wrapper;
2022-09-14 11:26:01 +00:00
beforeEach(async () => {
2024-04-18 14:17:41 +00:00
wrapper = mount(Select, {
2022-09-06 12:03:37 +00:00
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
},
slots: {
2022-09-14 11:26:01 +00:00
header: h('span', { class: 'header-slot' }, 'Header slot'),
footer: h('span', { class: 'footer-slot' }, 'Footer slot'),
option: h('span', { class: 'option-slot' }, 'Option slot'),
optiongroup: h('span', { class: 'optiongroup-slot' }, 'OptionGroup slot'),
emptyfilter: h('span', { class: 'emptyfilter-slot' }, 'Empty filter slot')
2022-09-06 12:03:37 +00:00
},
props: {
options: [
{
2022-09-14 11:26:01 +00:00
label: 'Germany',
code: 'DE',
2022-09-06 12:03:37 +00:00
items: [
2022-09-14 11:26:01 +00:00
{ label: 'Berlin', value: 'Berlin' },
{ label: 'Frankfurt', value: 'Frankfurt' },
{ label: 'Hamburg', value: 'Hamburg' },
{ label: 'Munich', value: 'Munich' }
2022-09-06 12:03:37 +00:00
]
}
],
optionLabel: 'label',
optionGroupLabel: 'label',
optionGroupChildren: 'items'
}
});
await wrapper.trigger('click');
});
it('should see header and footer slots', () => {
expect(wrapper.find('.header-slot').exists()).toBe(true);
expect(wrapper.find('.header-slot').text()).toBe('Header slot');
expect(wrapper.find('.footer-slot').exists()).toBe(true);
expect(wrapper.find('.footer-slot').text()).toBe('Footer slot');
expect(wrapper.find('.option-slot').exists()).toBe(true);
expect(wrapper.find('.option-slot').text()).toBe('Option slot');
expect(wrapper.find('.optiongroup-slot').exists()).toBe(true);
expect(wrapper.find('.optiongroup-slot').text()).toBe('OptionGroup slot');
});
});
describe('empty templating checks', () => {
let wrapper;
2022-09-14 11:26:01 +00:00
beforeEach(async () => {
2024-04-18 14:17:41 +00:00
wrapper = mount(Select, {
2022-09-06 12:03:37 +00:00
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
},
props: {
options: [],
optionLabel: 'label',
optionGroupLabel: 'label',
optionGroupChildren: 'items',
emptyMessage: 'Need options prop',
filterValue: 'xd'
}
});
await wrapper.trigger('click');
});
it('should see empty slots', () => {
2024-04-18 14:17:41 +00:00
expect(wrapper.find('.p-select-empty-message').exists()).toBe(true);
expect(wrapper.find('.p-select-empty-message').text()).toBe('Need options prop');
2022-09-06 12:03:37 +00:00
});
});
describe('loader checks', () => {
let wrapper;
2022-09-14 11:26:01 +00:00
beforeEach(async () => {
2024-04-18 14:17:41 +00:00
wrapper = mount(Select, {
2022-09-06 12:03:37 +00:00
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
},
props: {
loading: true,
loadingIcon: 'pi pi-discord',
options: [
2022-09-14 11:26:01 +00:00
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
2022-09-06 12:03:37 +00:00
],
optionLabel: 'name',
optionValue: 'code',
placeholder: 'Select a City'
}
});
await wrapper.trigger('click');
});
it('should show the loader', async () => {
2024-04-18 14:17:41 +00:00
expect(wrapper.find('.p-select-trigger-icon').classes()).toContain('pi-discord');
2022-09-06 12:03:37 +00:00
await wrapper.setProps({ loading: false });
2024-04-18 14:17:41 +00:00
expect(wrapper.find('.p-select-trigger-icon').classes()).not.toContain('pi-discord');
2022-09-06 12:03:37 +00:00
});
});
describe('filter checks', () => {
let wrapper;
2022-09-14 11:26:01 +00:00
beforeEach(async () => {
2024-04-18 14:17:41 +00:00
wrapper = mount(Select, {
2022-09-06 12:03:37 +00:00
global: {
plugins: [PrimeVue],
stubs: {
teleport: true
}
},
props: {
filter: true,
2022-12-08 11:04:25 +00:00
filterIcon: 'pi pi-discord',
2022-09-06 12:03:37 +00:00
options: [
2022-09-14 11:26:01 +00:00
{ name: 'Australia', code: 'AU' },
{ name: 'Brazil', code: 'BR' },
{ name: 'China', code: 'CN' },
{ name: 'Egypt', code: 'EG' },
{ name: 'France', code: 'FR' },
{ name: 'Germany', code: 'DE' },
{ name: 'India', code: 'IN' },
{ name: 'Japan', code: 'JP' },
{ name: 'Spain', code: 'ES' },
{ name: 'United States', code: 'US' }
2022-09-06 12:03:37 +00:00
],
optionLabel: 'name'
}
});
await wrapper.trigger('click');
});
it('should make filtering', async () => {
2024-04-18 14:17:41 +00:00
const filterInput = wrapper.find('.p-select-filter');
const filterIcon = wrapper.find('.p-select-filter-icon');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
expect(filterInput.exists()).toBe(true);
2022-12-08 11:04:25 +00:00
expect(filterIcon.classes()).toContain('pi-discord');
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
const event = { target: { value: 'c' } };
2022-12-08 14:13:16 +00:00
const onFilterChange = vi.spyOn(wrapper.vm, 'onFilterChange');
2022-09-06 12:03:37 +00:00
wrapper.vm.onFilterChange(event);
await wrapper.vm.$nextTick();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
expect(onFilterChange).toHaveBeenCalled();
2022-09-14 11:26:01 +00:00
await wrapper.setData({ filterValue: 'c' });
2022-09-06 12:03:37 +00:00
2024-04-18 14:17:41 +00:00
expect(wrapper.findAll('.p-select-item').length).toBe(2);
2022-09-06 12:03:37 +00:00
});
2022-09-14 11:26:01 +00:00
});