|
| 1 | +import {expect} from '@esm-bundle/chai'; |
| 2 | +import {on, once} from '@spearwolf/eventize'; |
| 3 | +import {ComponentContext} from '@spearwolf/shadow-objects'; |
| 4 | +import {shadowObjects} from '@spearwolf/shadow-objects/shadow-objects.js'; |
| 5 | +import '@spearwolf/shadow-objects/shae-ent.js'; |
| 6 | +import '@spearwolf/shadow-objects/shae-worker.js'; |
| 7 | +import {findElementsById} from '../../src/findElementsById.js'; |
| 8 | +import {render} from '../../src/render.js'; |
| 9 | + |
| 10 | +describe('ShadowObjectCreationAPI.emit helper', () => { |
| 11 | + beforeEach(async () => { |
| 12 | + render(` |
| 13 | + <shae-worker local no-autostart auto-sync="no" id="localEnv" no-structured-clone></shae-worker> |
| 14 | +
|
| 15 | + <shae-ent id="a" token="A"></shae-ent> |
| 16 | + <shae-ent id="b" token="B"></shae-ent> |
| 17 | + `); |
| 18 | + |
| 19 | + await Promise.all(['shae-worker', 'shae-ent'].map((name) => customElements.whenDefined(name))); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + ComponentContext.get().clear(); |
| 24 | + const localEnv = document.getElementById('localEnv'); |
| 25 | + if (localEnv && localEnv.shadowEnv) { |
| 26 | + localEnv.shadowEnv.destroy(); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + it('emits events on the entity by default', async () => { |
| 31 | + let emitted = false; |
| 32 | + |
| 33 | + class MyShadowObject { |
| 34 | + constructor({entity, emit}) { |
| 35 | + once(entity, 'foo', () => { |
| 36 | + emitted = true; |
| 37 | + }); |
| 38 | + emit('foo'); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + shadowObjects.define('A', MyShadowObject); |
| 43 | + |
| 44 | + const [localEnv] = findElementsById('localEnv'); |
| 45 | + await localEnv.start(); |
| 46 | + await localEnv.shadowEnv.syncWait(); |
| 47 | + |
| 48 | + expect(emitted).to.be.true; |
| 49 | + }); |
| 50 | + |
| 51 | + it('emits events on a specific target if provided', async () => { |
| 52 | + let emitted = false; |
| 53 | + |
| 54 | + class MyShadowObject { |
| 55 | + constructor({emit}) { |
| 56 | + const otherObj = {}; |
| 57 | + on(otherObj, 'bar', () => { |
| 58 | + emitted = true; |
| 59 | + }); |
| 60 | + emit(otherObj, 'bar'); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + shadowObjects.define('B', MyShadowObject); |
| 65 | + |
| 66 | + const [localEnv] = findElementsById('localEnv'); |
| 67 | + await localEnv.start(); |
| 68 | + await localEnv.shadowEnv.syncWait(); |
| 69 | + |
| 70 | + expect(emitted).to.be.true; |
| 71 | + }); |
| 72 | +}); |
0 commit comments