From daaf6a4ddd4999cb9795c52ddb6a0765d20d0efd Mon Sep 17 00:00:00 2001 From: git-mahade Date: Wed, 13 Dec 2023 23:48:01 +0600 Subject: [PATCH] switch --- src/components/DxhSwitch.vue | 51 ++++++++++++++ src/components/__tests__/DxhSwitch.spec.ts | 81 ++++++++++++++++++++++ src/index.ts | 7 +- 3 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 src/components/DxhSwitch.vue create mode 100644 src/components/__tests__/DxhSwitch.spec.ts diff --git a/src/components/DxhSwitch.vue b/src/components/DxhSwitch.vue new file mode 100644 index 0000000..124290f --- /dev/null +++ b/src/components/DxhSwitch.vue @@ -0,0 +1,51 @@ + + + diff --git a/src/components/__tests__/DxhSwitch.spec.ts b/src/components/__tests__/DxhSwitch.spec.ts new file mode 100644 index 0000000..71fd87a --- /dev/null +++ b/src/components/__tests__/DxhSwitch.spec.ts @@ -0,0 +1,81 @@ +import { describe, it, expect } from 'vitest' +import { mount } from '@vue/test-utils' +import DxhSwitch from '../DxhSwitch.vue' + +describe('DxhSwitch.vue', () => { + it('renders checkbox input and label with data-test attributes', async () => { + const wrapper = mount(DxhSwitch, { + props: { + id: 'my-checkbox', + defaultChecked: false, + disabled: false + } + }) + + const input = wrapper.find('[data-test="checkbox-input"]') + const label = wrapper.find('[data-test="checkbox-label"]') + + expect(input.exists()).toBe(true) + expect(label.exists()).toBe(false) + }) + + it('handles change event when checkbox is clicked', async () => { + const wrapper: any = mount(DxhSwitch, { + props: { + id: 'my-checkbox', + defaultChecked: false, + disabled: false + } + }) + + const input = wrapper.find('[data-test="checkbox-input"]') + + await input.trigger('change') + + expect(wrapper.emitted('change')).toBeTruthy() + expect(wrapper.emitted('change')[0][0]).toBe(false) + }) + + it('applies defaultChecked state on mount', async () => { + const wrapper = mount(DxhSwitch, { + props: { + id: 'my-checkbox', + defaultChecked: true, + disabled: false + } + }) + + const input: any = wrapper.find('[data-test="checkbox-input"]') + expect(input.element.checked).toBe(false) + }) + + it('does not handle change event when disabled is true', async () => { + const wrapper = mount(DxhSwitch, { + props: { + id: 'my-checkbox', + defaultChecked: false, + disabled: true + } + }) + + const input = wrapper.find('[data-test="checkbox-input"]') + + await input.trigger('change') + + expect(wrapper.emitted('change')).toBeFalsy() + }) + + it('adds opacity-70 class when disabled is true', async () => { + const wrapper = mount(DxhSwitch, { + props: { + id: 'my-checkbox', + defaultChecked: false, + disabled: true + } + }) + + const container = wrapper.find('[data-test="checkbox-container"]') + + expect(container.classes()).toContain('opacity-70') + }) +}) diff --git a/src/index.ts b/src/index.ts index ee2ff8d..8da035d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ -import DButton from "./components/DButton.vue" -import DInput from "./components/DInput.vue" +import DButton from './components/DButton.vue' +import DInput from './components/DInput.vue' +import DxhSwitch from './components/DxhSwitch.vue' -export default {DButton, DInput} \ No newline at end of file +export default { DButton, DInput, DxhSwitch }