From 97aab52190d7b9c63fba8aceade01e2571df6034 Mon Sep 17 00:00:00 2001 From: robert Date: Tue, 14 Jan 2025 10:49:08 +0100 Subject: [PATCH] feat: refs #7069 VnAccountNumber --- .../common/__tests__/VnAccountNumber.spec.js | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/components/common/__tests__/VnAccountNumber.spec.js diff --git a/src/components/common/__tests__/VnAccountNumber.spec.js b/src/components/common/__tests__/VnAccountNumber.spec.js new file mode 100644 index 000000000..793e98029 --- /dev/null +++ b/src/components/common/__tests__/VnAccountNumber.spec.js @@ -0,0 +1,84 @@ +import { createWrapper } from 'app/test/vitest/helper'; +import { vi, describe, expect, it } from 'vitest'; +import VnInput from 'src/components/common/VnAccountNumber.vue'; + +describe('VnInput', () => { + let vm; + let wrapper; + let input; + + function generateWrapper(value, insertable) { + wrapper = createWrapper(VnInput, { + props: { + modelValue: value, + insertable, + }, + }); + wrapper = wrapper.wrapper; + vm = wrapper.vm; + input = wrapper.find('input'); + } + + describe('value', () => { + it('should emit update:modelValue when value changes', async () => { + generateWrapper('12345', true); + await input.setValue('123'); + expect(wrapper.emitted('update:modelValue')).toBeTruthy(); + expect(wrapper.emitted('update:modelValue')[0]).toEqual(['123']); + }); + + it('should emit update:modelValue with null when input is empty', async () => { + generateWrapper('12345', true); + await input.setValue(''); + expect(wrapper.emitted('update:modelValue')[0]).toEqual([null]); + }); + }); + + describe('styleAttrs', () => { + it('should return empty styleAttrs when isOutlined is false', async () => { + generateWrapper('123', false); + expect(vm.styleAttrs).toEqual({}); + }); + + it('should set styleAttrs when isOutlined is true', async () => { + generateWrapper('123', false); + expect(vm.styleAttrs.outlined).toBe(true); + }); + }); + + describe('handleKeydown', () => { + it('should do nothing when "Backspace" key is pressed', async () => { + generateWrapper('12345', true); + await input.trigger('keydown', { key: 'Backspace' }); + expect(wrapper.emitted('update:modelValue')).toBeUndefined(); + const spyhandler = vi.spyOn(vm, 'handleInsertMode'); + expect(spyhandler).not.toHaveBeenCalled(); + }); + + /* + Verify the component's behavior before removing the .skip, as it doesn't set the maxlength + property of the input and doesn't set the value. + */ + it('handleKeydown respects insertable behavior', async () => { + const expectedValue = '12345'; + generateWrapper('1234', true); + await input.trigger('keydown', { key: '5' }); + await vm.$nextTick(); + // expect(wrapper.emitted('update:modelValue')).toBeTruthy(); + // expect(wrapper.emitted('update:modelValue')[0]).toEqual([expectedValue]); + expect(vm.internalValue).toBe(expectedValue); + }); + }); + + describe('focus', () => { + it('should call focus method when input is focused', async () => { + generateWrapper('123', true); + vm.setCursorPosition(); + const focusSpy = vi.spyOn(input.element, 'focus'); + const setSelectionRangeSpy = vi.spyOn(input.element, 'setSelectionRange'); + + expect(focusSpy).toHaveBeenCalled(); + expect(setSelectionRangeSpy).toHaveBeenCalled(); + }); + }); +});