diff --git a/packages/components/switch/__tests__/switch.test.ts b/packages/components/switch/__tests__/switch.test.tsx
similarity index 92%
rename from packages/components/switch/__tests__/switch.test.ts
rename to packages/components/switch/__tests__/switch.test.tsx
index 3583dabf1a5cb..8eaadc9ebe647 100644
--- a/packages/components/switch/__tests__/switch.test.ts
+++ b/packages/components/switch/__tests__/switch.test.tsx
@@ -5,6 +5,8 @@ import { debugWarn } from '@element-plus/utils'
import { Checked, CircleClose } from '@element-plus/icons-vue'
import { ElFormItem } from '@element-plus/components/form'
import Switch from '../src/switch.vue'
+import type { VueWrapper } from '@vue/test-utils'
+import type { SwitchInstance } from '../src/switch'
vi.mock('@element-plus/utils/error', () => ({
debugWarn: vi.fn(),
@@ -142,7 +144,7 @@ describe('Switch.vue', () => {
`,
methods: {
- handleChange(val) {
+ handleChange(val: boolean) {
this.target = val
},
},
@@ -214,6 +216,33 @@ describe('Switch.vue', () => {
expect(vm.value).toEqual('100')
})
+ test('default switch active-value is false', async () => {
+ const wrapper = mount({
+ components: {
+ 'el-switch': Switch,
+ },
+ template: `
+
+
+
+ `,
+ data() {
+ return {
+ value: false,
+ onValue: false,
+ offValue: true,
+ }
+ },
+ })
+ const vm = wrapper.vm
+
+ const coreWrapper = wrapper.find('.el-switch__core')
+ await coreWrapper.trigger('click')
+ expect(vm.value).toEqual(true)
+ await coreWrapper.trigger('click')
+ expect(vm.value).toEqual(false)
+ })
+
test('value is the single source of truth', async () => {
const wrapper = mount({
components: {
@@ -227,7 +256,8 @@ describe('Switch.vue', () => {
})
const vm = wrapper.vm
const coreWrapper = wrapper.find('.el-switch__core')
- const switchWrapper = wrapper.findComponent(Switch)
+ const switchWrapper: VueWrapper =
+ wrapper.findComponent(Switch)
const switchVm = switchWrapper.vm
const inputEl = vm.$el.querySelector('input')
@@ -253,7 +283,8 @@ describe('Switch.vue', () => {
})
const vm = wrapper.vm
const coreWrapper = wrapper.find('.el-switch__core')
- const switchWrapper = wrapper.findComponent(Switch)
+ const switchWrapper: VueWrapper =
+ wrapper.findComponent(Switch)
const switchVm = switchWrapper.vm
const inputEl = vm.$el.querySelector('input')
diff --git a/packages/components/switch/src/switch.vue b/packages/components/switch/src/switch.vue
index 98e6940621db0..1de336f93fe1e 100644
--- a/packages/components/switch/src/switch.vue
+++ b/packages/components/switch/src/switch.vue
@@ -82,10 +82,9 @@
-