|
| 1 | +import {expect, fixture, html} from '@open-wc/testing' |
| 2 | +import {fake, match} from 'sinon' |
| 3 | +import {register, add} from '../src/tag-observer.js' |
| 4 | + |
| 5 | +describe('tag observer', () => { |
| 6 | + let instance: HTMLElement |
| 7 | + beforeEach(async () => { |
| 8 | + instance = await fixture(html`<section> |
| 9 | + <div data-tagtest="section.a.b.c section.d.e.f doesntexist.g.h.i"></div> |
| 10 | + </section>`) |
| 11 | + }) |
| 12 | + |
| 13 | + it('can register new tag observers', () => { |
| 14 | + register('foo', fake(), fake()) |
| 15 | + }) |
| 16 | + |
| 17 | + it('throws an error when registering a duplicate', () => { |
| 18 | + register('duplicate', fake(), fake()) |
| 19 | + expect(() => register('duplicate', fake(), fake())).to.throw() |
| 20 | + }) |
| 21 | + |
| 22 | + describe('registered behaviour', () => { |
| 23 | + const testParse = fake(v => v.split('.')) |
| 24 | + const testFound = fake() |
| 25 | + register('data-tagtest', testParse, testFound) |
| 26 | + beforeEach(() => { |
| 27 | + add(instance) |
| 28 | + }) |
| 29 | + |
| 30 | + it('uses parse to extract tagged element values', () => { |
| 31 | + expect(testParse).to.be.calledWithExactly('section.a.b.c') |
| 32 | + expect(testParse).to.be.calledWithExactly('section.d.e.f') |
| 33 | + expect(testParse).to.be.calledWithExactly('doesntexist.g.h.i') |
| 34 | + }) |
| 35 | + |
| 36 | + it('calls found with el and args based from testParse', () => { |
| 37 | + const div = instance.querySelector('div')! |
| 38 | + expect(testFound).to.be.calledWithExactly(div, instance, 'data-tagtest', 'a', 'b', 'c') |
| 39 | + expect(testFound).to.be.calledWithExactly(div, instance, 'data-tagtest', 'd', 'e', 'f') |
| 40 | + expect(testFound).to.not.be.calledWithMatch(match.any, match.any, 'data-tagtest', 'g', 'h', 'i') |
| 41 | + }) |
| 42 | + |
| 43 | + it('calls found if added to a node that has tags on itself', () => { |
| 44 | + const div = document.createElement('div') |
| 45 | + div.setAttribute('data-tagtest', 'div.j.k.l') |
| 46 | + add(div) |
| 47 | + expect(testParse).to.be.calledWithExactly('div.j.k.l') |
| 48 | + expect(testFound).to.be.calledWithExactly(div, div, 'data-tagtest', 'j', 'k', 'l') |
| 49 | + }) |
| 50 | + |
| 51 | + it('pierces shadowdom boundaries to find nearest controller', () => { |
| 52 | + const div = document.createElement('div') |
| 53 | + const shadow = div.attachShadow({mode: 'open'}) |
| 54 | + const span = document.createElement('span') |
| 55 | + span.setAttribute('data-tagtest', 'div.m.n.o') |
| 56 | + shadow.append(span) |
| 57 | + add(span) |
| 58 | + expect(testParse).to.be.calledWithExactly('div.m.n.o') |
| 59 | + expect(testFound).to.be.calledWithExactly(span, div, 'data-tagtest', 'm', 'n', 'o') |
| 60 | + }) |
| 61 | + |
| 62 | + it('queries inside shadowdom, and pierces to find nearest controller', () => { |
| 63 | + const div = document.createElement('div') |
| 64 | + const shadow = div.attachShadow({mode: 'open'}) |
| 65 | + const span = document.createElement('span') |
| 66 | + span.setAttribute('data-tagtest', 'div.p.q.r') |
| 67 | + shadow.append(span) |
| 68 | + add(shadow) |
| 69 | + expect(testParse).to.be.calledWithExactly('div.p.q.r') |
| 70 | + expect(testFound).to.be.calledWithExactly(span, div, 'data-tagtest', 'p', 'q', 'r') |
| 71 | + }) |
| 72 | + |
| 73 | + describe('mutations', () => { |
| 74 | + it('calls parse+found on attributes that change', async () => { |
| 75 | + instance.setAttribute('data-tagtest', 'section.s.t.u not.v.w.x') |
| 76 | + await Promise.resolve() |
| 77 | + expect(testParse).to.be.calledWithExactly('section.s.t.u') |
| 78 | + expect(testParse).to.be.calledWithExactly('not.v.w.x') |
| 79 | + expect(testFound).to.be.calledWithExactly(instance, instance, 'data-tagtest', 's', 't', 'u') |
| 80 | + expect(testFound).to.not.be.calledWithMatch(match.any, match.any, 'data-tagtest', 'v', 'w', 'x') |
| 81 | + }) |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
0 commit comments