|
| 1 | +import { ComponentPublicInstance } from 'vue' |
| 2 | + |
| 3 | +import { mount, config } from '../../src' |
| 4 | +import { WrapperAPI } from '../../src/types' |
| 5 | + |
| 6 | +declare module '../../src/vue-wrapper' { |
| 7 | + interface VueWrapper<T extends ComponentPublicInstance> { |
| 8 | + width(): number |
| 9 | + $el: Element |
| 10 | + myMethod(): void |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +const textValue = `I'm the innerHTML` |
| 15 | +const mountComponent = () => mount({ template: `<h1>${textValue}</h1>` }) |
| 16 | + |
| 17 | +describe('Plugin', () => { |
| 18 | + describe('#install method', () => { |
| 19 | + beforeEach(() => { |
| 20 | + config.plugins.VueWrapper.reset() |
| 21 | + }) |
| 22 | + |
| 23 | + it('extends wrappers with the return values from the install function', () => { |
| 24 | + const width = 230 |
| 25 | + const plugin = () => ({ width }) |
| 26 | + config.plugins.VueWrapper.install(plugin) |
| 27 | + const wrapper = mountComponent() |
| 28 | + expect(wrapper).toHaveProperty('width', width) |
| 29 | + }) |
| 30 | + |
| 31 | + it('receives the wrapper inside the plugin setup', () => { |
| 32 | + const plugin = (wrapper: WrapperAPI) => { |
| 33 | + return { |
| 34 | + $el: wrapper.element // simple aliases |
| 35 | + } |
| 36 | + } |
| 37 | + config.plugins.VueWrapper.install(plugin) |
| 38 | + const wrapper = mountComponent() |
| 39 | + expect(wrapper.$el.innerHTML).toEqual(textValue) |
| 40 | + }) |
| 41 | + |
| 42 | + it('supports functions', () => { |
| 43 | + const myMethod = jest.fn() |
| 44 | + const plugin = () => ({ myMethod }) |
| 45 | + config.plugins.VueWrapper.install(plugin) |
| 46 | + mountComponent().myMethod() |
| 47 | + expect(myMethod).toHaveBeenCalledTimes(1) |
| 48 | + }) |
| 49 | + |
| 50 | + describe('error states', () => { |
| 51 | + const plugins = [ |
| 52 | + () => false, |
| 53 | + () => true, |
| 54 | + () => [], |
| 55 | + true, |
| 56 | + false, |
| 57 | + 'property', |
| 58 | + 120 |
| 59 | + ] |
| 60 | + |
| 61 | + it.each(plugins)( |
| 62 | + 'Calling install with %p is handled gracefully', |
| 63 | + (plugin) => { |
| 64 | + config.plugins.VueWrapper.install(plugin) |
| 65 | + expect(() => mountComponent()).not.toThrow() |
| 66 | + } |
| 67 | + ) |
| 68 | + }) |
| 69 | + }) |
| 70 | +}) |
0 commit comments