|
| 1 | +import { |
| 2 | + defineCustomElement, |
| 3 | + h, |
| 4 | + nextTick, |
| 5 | + ref, |
| 6 | + renderSlot, |
| 7 | + VueElement |
| 8 | +} from '../src' |
| 9 | + |
| 10 | +describe('defineCustomElement', () => { |
| 11 | + const container = document.createElement('div') |
| 12 | + document.body.appendChild(container) |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + container.innerHTML = '' |
| 16 | + }) |
| 17 | + |
| 18 | + describe('mounting/unmount', () => { |
| 19 | + const E = defineCustomElement({ |
| 20 | + render: () => h('div', 'hello') |
| 21 | + }) |
| 22 | + customElements.define('my-element', E) |
| 23 | + |
| 24 | + test('should work', () => { |
| 25 | + container.innerHTML = `<my-element></my-element>` |
| 26 | + const e = container.childNodes[0] as VueElement |
| 27 | + expect(e).toBeInstanceOf(E) |
| 28 | + expect(e._instance).toBeTruthy() |
| 29 | + expect(e.shadowRoot!.innerHTML).toBe(`<div>hello</div>`) |
| 30 | + }) |
| 31 | + |
| 32 | + test('should work w/ manual instantiation', () => { |
| 33 | + const e = new E() |
| 34 | + // should lazy init |
| 35 | + expect(e._instance).toBe(null) |
| 36 | + // should initialize on connect |
| 37 | + container.appendChild(e) |
| 38 | + expect(e._instance).toBeTruthy() |
| 39 | + expect(e.shadowRoot!.innerHTML).toBe(`<div>hello</div>`) |
| 40 | + }) |
| 41 | + |
| 42 | + test('should unmount on remove', async () => { |
| 43 | + container.innerHTML = `<my-element></my-element>` |
| 44 | + const e = container.childNodes[0] as VueElement |
| 45 | + container.removeChild(e) |
| 46 | + await nextTick() |
| 47 | + expect(e._instance).toBe(null) |
| 48 | + expect(e.shadowRoot!.innerHTML).toBe('') |
| 49 | + }) |
| 50 | + |
| 51 | + test('should not unmount on move', async () => { |
| 52 | + container.innerHTML = `<div><my-element></my-element></div>` |
| 53 | + const e = container.childNodes[0].childNodes[0] as VueElement |
| 54 | + const i = e._instance |
| 55 | + // moving from one parent to another - this will trigger both disconnect |
| 56 | + // and connected callbacks synchronously |
| 57 | + container.appendChild(e) |
| 58 | + await nextTick() |
| 59 | + // should be the same instance |
| 60 | + expect(e._instance).toBe(i) |
| 61 | + expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>') |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe('props', () => { |
| 66 | + const E = defineCustomElement({ |
| 67 | + props: ['foo', 'bar', 'bazQux'], |
| 68 | + render() { |
| 69 | + return [ |
| 70 | + h('div', null, this.foo), |
| 71 | + h('div', null, this.bazQux || (this.bar && this.bar.x)) |
| 72 | + ] |
| 73 | + } |
| 74 | + }) |
| 75 | + customElements.define('my-el-props', E) |
| 76 | + |
| 77 | + test('props via attribute', async () => { |
| 78 | + // bazQux should map to `baz-qux` attribute |
| 79 | + container.innerHTML = `<my-el-props foo="hello" baz-qux="bye"></my-el-props>` |
| 80 | + const e = container.childNodes[0] as VueElement |
| 81 | + expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div><div>bye</div>') |
| 82 | + |
| 83 | + // change attr |
| 84 | + e.setAttribute('foo', 'changed') |
| 85 | + await nextTick() |
| 86 | + expect(e.shadowRoot!.innerHTML).toBe('<div>changed</div><div>bye</div>') |
| 87 | + |
| 88 | + e.setAttribute('baz-qux', 'changed') |
| 89 | + await nextTick() |
| 90 | + expect(e.shadowRoot!.innerHTML).toBe( |
| 91 | + '<div>changed</div><div>changed</div>' |
| 92 | + ) |
| 93 | + }) |
| 94 | + |
| 95 | + test('props via properties', async () => { |
| 96 | + const e = new E() |
| 97 | + e.foo = 'one' |
| 98 | + e.bar = { x: 'two' } |
| 99 | + container.appendChild(e) |
| 100 | + expect(e.shadowRoot!.innerHTML).toBe('<div>one</div><div>two</div>') |
| 101 | + |
| 102 | + e.foo = 'three' |
| 103 | + await nextTick() |
| 104 | + expect(e.shadowRoot!.innerHTML).toBe('<div>three</div><div>two</div>') |
| 105 | + |
| 106 | + e.bazQux = 'four' |
| 107 | + await nextTick() |
| 108 | + expect(e.shadowRoot!.innerHTML).toBe('<div>three</div><div>four</div>') |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + describe('emits', () => { |
| 113 | + const E = defineCustomElement({ |
| 114 | + setup(_, { emit }) { |
| 115 | + emit('created') |
| 116 | + return () => |
| 117 | + h('div', { |
| 118 | + onClick: () => emit('my-click', 1) |
| 119 | + }) |
| 120 | + } |
| 121 | + }) |
| 122 | + customElements.define('my-el-emits', E) |
| 123 | + |
| 124 | + test('emit on connect', () => { |
| 125 | + const e = new E() |
| 126 | + const spy = jest.fn() |
| 127 | + e.addEventListener('created', spy) |
| 128 | + container.appendChild(e) |
| 129 | + expect(spy).toHaveBeenCalled() |
| 130 | + }) |
| 131 | + |
| 132 | + test('emit on interaction', () => { |
| 133 | + container.innerHTML = `<my-el-emits></my-el-emits>` |
| 134 | + const e = container.childNodes[0] as VueElement |
| 135 | + const spy = jest.fn() |
| 136 | + e.addEventListener('my-click', spy) |
| 137 | + e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click')) |
| 138 | + expect(spy).toHaveBeenCalled() |
| 139 | + expect(spy.mock.calls[0][0]).toMatchObject({ |
| 140 | + detail: [1] |
| 141 | + }) |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + describe('slots', () => { |
| 146 | + const E = defineCustomElement({ |
| 147 | + render() { |
| 148 | + return [ |
| 149 | + h('div', null, [ |
| 150 | + renderSlot(this.$slots, 'default', undefined, () => [ |
| 151 | + h('div', 'fallback') |
| 152 | + ]) |
| 153 | + ]), |
| 154 | + h('div', null, renderSlot(this.$slots, 'named')) |
| 155 | + ] |
| 156 | + } |
| 157 | + }) |
| 158 | + customElements.define('my-el-slots', E) |
| 159 | + |
| 160 | + test('default slot', () => { |
| 161 | + container.innerHTML = `<my-el-slots><span>hi</span></my-el-slots>` |
| 162 | + const e = container.childNodes[0] as VueElement |
| 163 | + // native slots allocation does not affect innerHTML, so we just |
| 164 | + // verify that we've rendered the correct native slots here... |
| 165 | + expect(e.shadowRoot!.innerHTML).toBe( |
| 166 | + `<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>` |
| 167 | + ) |
| 168 | + }) |
| 169 | + }) |
| 170 | + |
| 171 | + describe('provide/inject', () => { |
| 172 | + const Consumer = defineCustomElement({ |
| 173 | + inject: ['foo'], |
| 174 | + render(this: any) { |
| 175 | + return h('div', this.foo.value) |
| 176 | + } |
| 177 | + }) |
| 178 | + customElements.define('my-consumer', Consumer) |
| 179 | + |
| 180 | + test('over nested usage', async () => { |
| 181 | + const foo = ref('injected!') |
| 182 | + const Provider = defineCustomElement({ |
| 183 | + provide: { |
| 184 | + foo |
| 185 | + }, |
| 186 | + render() { |
| 187 | + return h('my-consumer') |
| 188 | + } |
| 189 | + }) |
| 190 | + customElements.define('my-provider', Provider) |
| 191 | + container.innerHTML = `<my-provider><my-provider>` |
| 192 | + const provider = container.childNodes[0] as VueElement |
| 193 | + const consumer = provider.shadowRoot!.childNodes[0] as VueElement |
| 194 | + |
| 195 | + expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`) |
| 196 | + |
| 197 | + foo.value = 'changed!' |
| 198 | + await nextTick() |
| 199 | + expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`) |
| 200 | + }) |
| 201 | + |
| 202 | + test('over slot composition', async () => { |
| 203 | + const foo = ref('injected!') |
| 204 | + const Provider = defineCustomElement({ |
| 205 | + provide: { |
| 206 | + foo |
| 207 | + }, |
| 208 | + render() { |
| 209 | + return renderSlot(this.$slots, 'default') |
| 210 | + } |
| 211 | + }) |
| 212 | + customElements.define('my-provider-2', Provider) |
| 213 | + |
| 214 | + container.innerHTML = `<my-provider-2><my-consumer></my-consumer><my-provider-2>` |
| 215 | + const provider = container.childNodes[0] |
| 216 | + const consumer = provider.childNodes[0] as VueElement |
| 217 | + expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`) |
| 218 | + |
| 219 | + foo.value = 'changed!' |
| 220 | + await nextTick() |
| 221 | + expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`) |
| 222 | + }) |
| 223 | + }) |
| 224 | +}) |
0 commit comments