2022-02-04 14:59:58 +08:00
|
|
|
import { computed, defineComponent, nextTick } from 'vue'
|
|
|
|
import { mount } from '@vue/test-utils'
|
2022-04-12 01:10:25 +08:00
|
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
2022-02-11 11:03:15 +08:00
|
|
|
import { debugWarn } from '@element-plus/utils'
|
2022-02-04 14:59:58 +08:00
|
|
|
import { useDeprecated } from '../use-deprecated'
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
|
|
|
vi.mock('@element-plus/utils/error', async () => {
|
2022-02-04 14:59:58 +08:00
|
|
|
return {
|
2022-02-21 14:28:22 +08:00
|
|
|
...(await vi.importActual<any>('@element-plus/utils/error')),
|
2022-04-12 01:10:25 +08:00
|
|
|
debugWarn: vi.fn(),
|
2022-02-04 14:59:58 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const DummyComponent = defineComponent({
|
|
|
|
props: {
|
|
|
|
shouldWarn: Boolean,
|
|
|
|
},
|
|
|
|
setup(props) {
|
|
|
|
useDeprecated(
|
|
|
|
{
|
|
|
|
from: 'oldApi',
|
|
|
|
replacement: 'newApi',
|
|
|
|
scope: 'dummyComponent',
|
|
|
|
version: 'some version',
|
|
|
|
ref: '',
|
|
|
|
},
|
|
|
|
computed(() => props.shouldWarn)
|
|
|
|
)
|
2022-02-21 14:28:22 +08:00
|
|
|
return () => AXIOM
|
2022-02-04 14:59:58 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('useDeprecated', () => {
|
2022-02-21 14:28:22 +08:00
|
|
|
afterEach(() => {
|
|
|
|
vi.restoreAllMocks()
|
2022-02-04 14:59:58 +08:00
|
|
|
})
|
2022-02-21 14:28:22 +08:00
|
|
|
|
2022-02-04 14:59:58 +08:00
|
|
|
it('should warn when condition is true', async () => {
|
|
|
|
mount(DummyComponent, {
|
|
|
|
props: {
|
|
|
|
shouldWarn: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(debugWarn).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not warn when condition is false', async () => {
|
|
|
|
mount(DummyComponent)
|
|
|
|
await nextTick()
|
|
|
|
expect(debugWarn).not.toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
})
|