primevue-mirror/components/tristatecheckbox/TriStateCheckbox.spec.js

110 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-09-14 11:26:01 +00:00
import { config, mount } from '@vue/test-utils';
2022-09-06 12:03:37 +00:00
import TriStateCheckbox from './TriStateCheckbox.vue';
2022-09-14 11:26:01 +00:00
config.global.mocks = {
$primevue: {
config: {
locale: {
aria: {
trueLabel: 'trueLabel',
falseLabel: 'falseLabel',
nullLabel: 'nullLabel'
}
}
}
}
};
let wrapper;
const modelValues = [true, false, null];
const emittedResults = [false, null, true];
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
describe('TriStateCheckbox.vue', () => {
2022-09-06 12:03:37 +00:00
beforeEach(() => {
2022-09-14 11:26:01 +00:00
wrapper = mount(TriStateCheckbox);
2022-12-09 09:18:21 +00:00
});
2022-09-14 11:26:01 +00:00
it('When onClick method triggered some methods effect', () => {
2022-12-08 14:13:16 +00:00
const mockUpdateModel = vi.fn();
2022-09-14 11:26:01 +00:00
wrapper.vm.updateModel = mockUpdateModel;
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
wrapper.vm.onClick('test');
expect(wrapper.vm.updateModel).toBeCalled();
expect(wrapper.emitted()['click']).toBeTruthy();
expect(wrapper.emitted()['change']).toBeTruthy();
2022-09-06 12:03:37 +00:00
});
2022-09-14 11:26:01 +00:00
it('When event.code is not equal Enter methods should not be effected', async () => {
wrapper.vm.onKeyDown({ code: 'test' });
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
expect(wrapper.emitted().keydown).toBeFalsy();
});
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
it('When event.code is equal Enter some methods should be triggered', async () => {
2022-12-08 14:13:16 +00:00
const mockUpdateModel = vi.fn();
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
wrapper.vm.updateModel = mockUpdateModel;
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
wrapper.vm.onKeyDown({ code: 'Enter', preventDefault: () => {} });
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
expect(wrapper.vm.updateModel).toBeCalled();
expect(wrapper.emitted().keydown).toBeTruthy();
2022-09-06 12:03:37 +00:00
});
2022-09-14 11:26:01 +00:00
it('When onBlur is triggered focused property should be false', async () => {
wrapper.vm.onBlur();
expect(wrapper.vm.focused).toBeFalsy();
expect(wrapper.emitted().blur).toBeTruthy();
});
2022-12-09 09:18:21 +00:00
2022-09-14 11:26:01 +00:00
it('is icon changed', async () => {
2022-09-06 12:03:37 +00:00
expect(wrapper.find('.p-checkbox-icon').classes()).not.toContain('pi-check');
expect(wrapper.find('.p-checkbox-icon').classes()).not.toContain('pi-times');
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ modelValue: true });
2022-09-06 12:03:37 +00:00
expect(wrapper.find('.p-checkbox-icon').classes()).toContain('pi-check');
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ modelValue: false });
2022-09-06 12:03:37 +00:00
expect(wrapper.find('.p-checkbox-icon').classes()).toContain('pi-times');
});
2022-09-14 11:26:01 +00:00
});
2022-12-09 09:18:21 +00:00
describe('UpdateModel method tests', () => {
beforeEach(() => {
wrapper = mount(TriStateCheckbox);
});
it('When disable props true updateModal should not triggered emit', async () => {
await wrapper.setProps({
disabled: true,
modelValue: null
});
wrapper.vm.updateModel();
expect(wrapper.emitted()['update:modelValue']).toBeFalsy();
});
2022-12-09 09:20:24 +00:00
it('When disable props false updateModal should triggered emit', () => {
2022-12-09 09:18:21 +00:00
wrapper.vm.updateModel();
expect(wrapper.emitted()['update:modelValue']).toBeTruthy();
});
modelValues.forEach((modelValue, index) => {
it('When modelValue changed update model emitted value should be change', async () => {
await wrapper.setProps({
modelValue
});
wrapper.vm.updateModel();
expect(wrapper.emitted()['update:modelValue']).toEqual([[emittedResults[index]]]);
});
});
});