Files
element-plus/packages/hooks/__tests__/use-model-toggle.test.tsx
dopamine 0ca1570aa1 chore: upgrade to Vue 3.5 (#22096)
* chore: upgrade deps

* chore: replace __ExtractPublicPropTypes with ExtractPublicPropTypes

* fix: get rid of type errors

* fix: resolve test errors with @vue/test-utils v2.4.6

* fix: resolve test errors with Vue 3.5.22

* ci: set pnpm flag

* chore: update the Vue peer dependency version

* Apply suggestion from @tolking

Co-authored-by: qiang <qw13131wang@gmail.com>

* docs: update example code

Co-authored-by: warmthsea <2586244885@qq.com>

* chore: remove csstype (#22487)

* chore: fix merge code type error

* chore: fix test:ssr error

- Cannot read properties of undefined (reading 'getSSRProps')

* chore: fix typecheck:vitest error

* chore: update pnpm yaml file

* test: fix collapse accordion error

* chore: update deps

* chore: fix type error

* chore: lock file

* chore: sync change

sync with the remove of vue macro

* refactor: use computed instead of eagerComputed

* fix: timeline.test.tsx typecheck

* chore: clean lock file

try dont throw CodeFactor issues in ci

did:
- rm pnpm-lock.yaml
- rm -rf ./**/node_modules
- pnpm store prune
- pnpm cache delete
- pnpm install

Also stay in 3.1.0 for vue-tsc in order to avoid the warnings of
template refs, see https://github.com/vuejs/language-tools/issues/5815

* chore: format code

---------

Co-authored-by: Dsaquel <291874700n@gmail.com>
Co-authored-by: qiang <qw13131wang@gmail.com>
Co-authored-by: warmthsea <2586244885@qq.com>
Co-authored-by: Noblet Ouways <91417411+Dsaquel@users.noreply.github.com>
Co-authored-by: sea <45450994+warmthsea@users.noreply.github.com>
Co-authored-by: btea <2356281422@qq.com>
2025-12-16 09:34:03 +08:00

180 lines
4.7 KiB
TypeScript

import { defineComponent, nextTick, reactive, ref } from 'vue'
import { mount } from '@vue/test-utils'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { useModelToggle, useModelToggleProps } from '../use-model-toggle'
import type { VueWrapper } from '@vue/test-utils'
const AXIOM = 'Rem is the best girl'
const onShow = vi.fn()
const onHide = vi.fn()
let flag = true
const shouldProceed = () => flag
const Comp = defineComponent({
props: {
...useModelToggleProps,
disabled: Boolean,
},
setup(props) {
const indicator = ref(false)
const { show, hide, toggle } = useModelToggle({
indicator,
onShow,
onHide,
shouldProceed,
shouldHideWhenRouteChanges: ref(true),
})
return () => {
return (
<>
<button class="show" onClick={show}>
show
</button>
<button class="hide" onClick={hide}>
hide
</button>
<button class="toggle" onClick={toggle}>
toggle
</button>
{indicator.value || props.modelValue ? <div>{AXIOM}</div> : undefined}
</>
)
}
},
})
describe('use-model-toggle', () => {
let wrapper: VueWrapper<any>
beforeEach(() => {
flag = true
wrapper = mount(Comp)
})
afterEach(() => {
wrapper.unmount()
})
it('should render correctly', async () => {
expect(wrapper.text()).not.toContain(AXIOM)
})
it('should show and hide via API calls', async () => {
expect(wrapper.text()).not.toContain(AXIOM)
await wrapper.find('.show').trigger('click')
expect(wrapper.text()).toContain(AXIOM)
expect(onShow).toHaveBeenCalledTimes(1)
await wrapper.find('.hide').trigger('click')
expect(wrapper.text()).not.toContain(AXIOM)
expect(onHide).toHaveBeenCalledTimes(1)
})
it('should call callbacks correctly', async () => {
expect(wrapper.text()).not.toContain(AXIOM)
await wrapper.find('.show').trigger('click')
expect(wrapper.text()).toContain(AXIOM)
expect(onShow).toHaveBeenCalledTimes(1)
await wrapper.find('.show').trigger('click')
expect(onShow).toHaveBeenCalledTimes(1)
await wrapper.find('.hide').trigger('click')
expect(wrapper.text()).not.toContain(AXIOM)
expect(onHide).toHaveBeenCalledTimes(1)
await wrapper.find('.hide').trigger('click')
expect(onHide).toHaveBeenCalledTimes(1)
})
it('should toggle show and hide via API calls', async () => {
expect(wrapper.text()).not.toContain(AXIOM)
await wrapper.find('.toggle').trigger('click')
expect(wrapper.text()).toContain(AXIOM)
expect(onShow).toHaveBeenCalledTimes(1)
await wrapper.find('.toggle').trigger('click')
expect(wrapper.text()).not.toContain(AXIOM)
expect(onHide).toHaveBeenCalledTimes(1)
})
it('should not proceed when the should proceed returns false', async () => {
flag = false
expect(wrapper.text()).not.toContain(AXIOM)
await wrapper.find('.show').trigger('click')
expect(wrapper.text()).not.toContain(AXIOM)
expect(onShow).not.toHaveBeenCalled()
await wrapper.find('.toggle').trigger('click')
expect(wrapper.text()).not.toContain(AXIOM)
expect(onShow).not.toHaveBeenCalled()
})
it('should bind with modelValue', async () => {
wrapper.unmount()
const model = ref(false)
const disabled = ref(false)
wrapper = mount({
setup: () => () => (
<Comp v-model={model.value} disabled={disabled.value} />
),
})
expect(wrapper.findComponent(Comp).text()).not.toContain(AXIOM)
await wrapper.find('.show').trigger('click')
expect(model.value).toBe(true)
expect(wrapper.findComponent(Comp).text()).toContain(AXIOM)
await wrapper.find('.hide').trigger('click')
expect(onHide).toHaveBeenCalledTimes(1)
expect(model.value).toBe(false)
expect(wrapper.findComponent(Comp).text()).not.toContain(AXIOM)
model.value = true
disabled.value = true
await nextTick()
// when disabled emits false that modifies the model
expect(model.value).toBe(false)
// should not hide when disabled
await wrapper.find('.hide').trigger('click')
expect(onHide).toHaveBeenCalledTimes(1)
})
it('should hide when route changes', async () => {
wrapper.unmount()
const router = reactive({
test: '/',
})
wrapper = mount(Comp, {
global: {
config: {
globalProperties: {
// @ts-ignore
$route: router,
},
},
},
})
expect(wrapper.text()).not.toContain(AXIOM)
await wrapper.find('.show').trigger('click')
expect(wrapper.text()).toContain(AXIOM)
expect(onHide).toHaveBeenCalledTimes(0)
router.test = '/test/changed'
await nextTick()
expect(onHide).toHaveBeenCalledTimes(1)
})
})