|
| 1 | +import { expectError, expectType } from 'tsd' |
| 2 | +import { defineComponent } from 'vue' |
| 3 | +import { shallowMount } from '../src' |
| 4 | + |
| 5 | +const AppWithDefine = defineComponent({ |
| 6 | + props: { |
| 7 | + a: { |
| 8 | + type: String, |
| 9 | + required: true |
| 10 | + }, |
| 11 | + b: Number |
| 12 | + }, |
| 13 | + template: '' |
| 14 | +}) |
| 15 | + |
| 16 | +// accept props |
| 17 | +let wrapper = shallowMount(AppWithDefine, { |
| 18 | + props: { a: 'Hello', b: 2 } |
| 19 | +}) |
| 20 | +// vm is properly typed |
| 21 | +expectType<string>(wrapper.vm.a) |
| 22 | + |
| 23 | +// can receive extra props |
| 24 | +// ideally, it should not |
| 25 | +// but the props have type { a: string } & VNodeProps |
| 26 | +// which allows any property |
| 27 | +shallowMount(AppWithDefine, { |
| 28 | + props: { a: 'Hello', c: 2 } |
| 29 | +}) |
| 30 | + |
| 31 | +// wrong prop type should not compile |
| 32 | +expectError( |
| 33 | + shallowMount(AppWithDefine, { |
| 34 | + props: { a: 2 } |
| 35 | + }) |
| 36 | +) |
| 37 | + |
| 38 | +const AppWithProps = { |
| 39 | + props: { |
| 40 | + a: { |
| 41 | + type: String, |
| 42 | + required: true |
| 43 | + } |
| 44 | + }, |
| 45 | + template: '' |
| 46 | +} |
| 47 | + |
| 48 | +// accept props |
| 49 | +wrapper = shallowMount(AppWithProps, { |
| 50 | + props: { a: 'Hello' } |
| 51 | +}) |
| 52 | +// vm is properly typed |
| 53 | +expectType<string>(wrapper.vm.a) |
| 54 | + |
| 55 | +// can't receive extra props |
| 56 | +expectError( |
| 57 | + shallowMount(AppWithProps, { |
| 58 | + props: { a: 'Hello', b: 2 } |
| 59 | + }) |
| 60 | +) |
| 61 | + |
| 62 | +// wrong prop type should not compile |
| 63 | +expectError( |
| 64 | + shallowMount(AppWithProps, { |
| 65 | + props: { a: 2 } |
| 66 | + }) |
| 67 | +) |
| 68 | + |
| 69 | +const AppWithArrayProps = { |
| 70 | + props: ['a'], |
| 71 | + template: '' |
| 72 | +} |
| 73 | + |
| 74 | +// accept props |
| 75 | +wrapper = shallowMount(AppWithArrayProps, { |
| 76 | + props: { a: 'Hello' } |
| 77 | +}) |
| 78 | +// vm is properly typed |
| 79 | +expectType<string>(wrapper.vm.a) |
| 80 | + |
| 81 | +// can receive extra props |
| 82 | +// as they are declared as `string[]` |
| 83 | +shallowMount(AppWithArrayProps, { |
| 84 | + props: { a: 'Hello', b: 2 } |
| 85 | +}) |
| 86 | + |
| 87 | +const AppWithoutProps = { |
| 88 | + template: '' |
| 89 | +} |
| 90 | + |
| 91 | +// can't receive extra props |
| 92 | +expectError( |
| 93 | + (wrapper = shallowMount(AppWithoutProps, { |
| 94 | + props: { b: 'Hello' } |
| 95 | + })) |
| 96 | +) |
| 97 | + |
| 98 | +// except if explicitly cast |
| 99 | +shallowMount(AppWithoutProps, { |
| 100 | + props: { b: 'Hello' } as never |
| 101 | +}) |
0 commit comments