|
| 1 | +import { h, nodeOps, render, useInstanceOption } from '@vue/runtime-test' |
| 2 | + |
| 3 | +describe('useInstanceOption', () => { |
| 4 | + test(`'ce' key`, () => { |
| 5 | + let hasInstance: boolean | undefined |
| 6 | + let ce: any |
| 7 | + const Comp = { |
| 8 | + setup() { |
| 9 | + const option = useInstanceOption('ce', true) |
| 10 | + hasInstance = option.hasInstance |
| 11 | + ce = option.value |
| 12 | + return () => null |
| 13 | + }, |
| 14 | + } |
| 15 | + render(h(Comp), nodeOps.createElement('div')) |
| 16 | + expect(hasInstance).toBe(true) |
| 17 | + // custom element definition is not supported in test renderer, so check to access it is enough |
| 18 | + expect(ce).toBeUndefined() |
| 19 | + }) |
| 20 | + |
| 21 | + test(`'type' key`, () => { |
| 22 | + let hasInstance: boolean | undefined |
| 23 | + let type: any |
| 24 | + const Comp = { |
| 25 | + __i18n: { locale: 'en' }, // inject by custom blocks |
| 26 | + setup() { |
| 27 | + const option = useInstanceOption('type', true) |
| 28 | + hasInstance = option.hasInstance |
| 29 | + type = option.value |
| 30 | + return () => null |
| 31 | + }, |
| 32 | + mounted() {}, |
| 33 | + } |
| 34 | + render(h(Comp), nodeOps.createElement('div')) |
| 35 | + expect(hasInstance).toBe(true) |
| 36 | + expect('setup' in type).toBe(true) |
| 37 | + expect('mounted' in type).toBe(true) |
| 38 | + expect(type.__i18n).toEqual({ locale: 'en' }) |
| 39 | + }) |
| 40 | + |
| 41 | + test(`'uid' key`, () => { |
| 42 | + let hasInstance: boolean | undefined |
| 43 | + let uid: any |
| 44 | + const Comp = { |
| 45 | + setup() { |
| 46 | + const option = useInstanceOption('uid', true) |
| 47 | + hasInstance = option.hasInstance |
| 48 | + uid = option.value |
| 49 | + return () => null |
| 50 | + }, |
| 51 | + } |
| 52 | + render(h(Comp), nodeOps.createElement('div')) |
| 53 | + expect(hasInstance).toBe(true) |
| 54 | + expect(typeof uid).toBe('number') |
| 55 | + }) |
| 56 | + |
| 57 | + test('not allowed key', () => { |
| 58 | + let hasInstance: boolean | undefined |
| 59 | + let value: any |
| 60 | + const Comp = { |
| 61 | + setup() { |
| 62 | + const option = useInstanceOption('foo' as any, true) |
| 63 | + hasInstance = option.hasInstance |
| 64 | + value = option.value |
| 65 | + return () => null |
| 66 | + }, |
| 67 | + } |
| 68 | + render(h(Comp), nodeOps.createElement('div')) |
| 69 | + expect(hasInstance).toBe(true) |
| 70 | + expect(value).toBeUndefined() |
| 71 | + expect( |
| 72 | + `useInstanceOption only accepts 'ce', 'type', 'uid' as key, got 'foo'.`, |
| 73 | + ).toHaveBeenWarned() |
| 74 | + }) |
| 75 | + |
| 76 | + test('not active instance', () => { |
| 77 | + const { hasInstance, value } = useInstanceOption('type') |
| 78 | + expect(hasInstance).toBe(false) |
| 79 | + expect(value).toBeUndefined() |
| 80 | + expect( |
| 81 | + 'useInstanceOption called without an active component instance.', |
| 82 | + ).toHaveBeenWarned() |
| 83 | + }) |
| 84 | +}) |
0 commit comments