Skip to content

Commit d07f6f4

Browse files
koddssonkeithamus
andcommitted
Get test/target.ts from use-delegates
Co-authored-by: Keith Cirkel <keithamus@users.noreply.github.com>
1 parent e89d825 commit d07f6f4

1 file changed

Lines changed: 106 additions & 115 deletions

File tree

test/target.ts

Lines changed: 106 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,115 @@
11
import {expect, fixture, html} from '@open-wc/testing'
2-
import {fake, replace} from 'sinon'
3-
import {findTarget, findTargets} from '../src/findtarget.js'
4-
5-
describe('findTarget', () => {
6-
window.customElements.define('find-target-test-element', class extends HTMLElement {})
2+
import {target, targets, Targetable} from '../src/target.js'
3+
import {use} from '../src/use.js'
4+
5+
describe('Targetable', () => {
6+
@use(Targetable)
7+
class TargetTest extends HTMLElement {
8+
@target foo
9+
bar = 'hello'
10+
count = 0
11+
#baz
12+
get baz() {
13+
return this.#baz
14+
}
15+
@target set baz(value: Element) {
16+
this.count += 1
17+
this.#baz = value
18+
}
19+
@target qux
20+
@target shadow
21+
22+
@target bing
23+
@targets foos
24+
bars = 'hello'
25+
counts = 0
26+
#bazs
27+
get bazs() {
28+
return this.#bazs
29+
}
30+
@target set bazs(value: Element) {
31+
this.counts += 1
32+
this.#bazs = value
33+
}
34+
@target quxs
35+
@target shadows
36+
}
37+
window.customElements.define('target-test', TargetTest)
738

839
let instance
9-
10-
it('calls querySelectorAll with the controller name and target name', async () => {
11-
instance = await fixture(html`<find-target-test-element />`)
12-
replace(instance, 'querySelectorAll', fake.returns([]))
13-
findTarget(instance, 'foo')
14-
expect(instance.querySelectorAll).to.have.been.calledOnceWith('[data-target~="find-target-test-element.foo"]')
15-
})
16-
17-
it('returns the first element where closest tag is the controller', async () => {
18-
instance = await fixture(html`
19-
<find-target-test-element>
20-
<find-target-test-element>
21-
<div id="1" data-target="find-target-test-element.foo"></div>
22-
</find-target-test-element>
23-
<div id="2" data-target="find-target-test-element.foo"></div>
24-
</find-target-test-element>
25-
`)
26-
expect(findTarget(instance, 'foo')).to.have.attribute('id', '2')
27-
})
28-
29-
it('returns the first element that has the exact target name', async () => {
30-
instance = await fixture(html`
31-
<find-target-test-element>
32-
<div id="1" data-target="find-target-test-element.foobar"></div>
33-
<div id="2" data-target="find-target-test-element.foo"></div>
34-
</find-target-test-element>
35-
`)
36-
expect(findTarget(instance, 'foo')).to.have.attribute('id', '2')
40+
beforeEach(async () => {
41+
instance = await fixture(html`<target-test>
42+
<target-test>
43+
<div id="el1" data-target="target-test.barfoo target-test.foobar"></div>
44+
<div id="el2" data-target="target-test.foo" data-targets="target-test.foos"></div>
45+
<div id="el3" data-target="target-test.bing"></div>
46+
</target-test>
47+
<div id="el4" data-target="target-test.foo" data-targets="target-test.foos"></div>
48+
<div id="el5" data-target="target-test.baz" data-targets="target-test.foos"></div>
49+
<div id="el6" data-target="target-test.bar target-test.bing"></div>
50+
<div id="el7" data-target="target-test.bazbaz"></div>
51+
<div id="el8" data-target="other-target.qux target-test.qux"></div>
52+
</target-test>`)
3753
})
3854

39-
it('returns targets when there are mutliple target values', async () => {
40-
instance = await fixture(html`
41-
<find-target-test-element>
42-
<div id="1" data-target="find-target-test-element.barfoo find-target-test-element.foobar"></div>
43-
<div id="2" data-target="find-target-test-element.foo"></div>
44-
</find-target-test-element>
45-
`)
46-
expect(findTarget(instance, 'foo')).to.have.attribute('id', '2')
47-
expect(findTarget(instance, 'barfoo')).to.have.attribute('id', '1')
48-
expect(findTarget(instance, 'barfoo')).to.equal(findTarget(instance, 'foobar'))
49-
})
50-
51-
it('returns targets when there are mutliple target values with different controllers', async () => {
52-
instance = await fixture(html`
53-
<find-target-test-element>
54-
<div id="1" data-target="other-controller.barfoo find-target-test-element.foobar"></div>
55-
</find-target-test-element>
56-
`)
57-
expect(findTarget(instance, 'foobar')).to.have.attribute('id', '1')
58-
expect(findTarget(instance, 'barfoo')).to.equal(undefined)
59-
})
60-
61-
it('returns targets from the shadowRoot, if available', async () => {
62-
instance = await fixture(html`<find-target-test-element></find-target-test-element>`)
63-
instance.attachShadow({mode: 'open'})
64-
const el = document.createElement('div')
65-
el.setAttribute('data-target', 'find-target-test-element.foobar')
66-
67-
instance.shadowRoot.appendChild(el)
68-
69-
expect(findTarget(instance, 'foobar')).to.equal(el)
55+
describe('target', () => {
56+
it('returns the first element where closest tag is the controller', async () => {
57+
expect(instance).to.have.property('foo').exist.with.attribute('id', 'el4')
58+
expect(instance.querySelector('target-test')).to.have.property('foo').exist.with.attribute('id', 'el2')
59+
})
60+
61+
it('does not assign to non-target decorated properties', async () => {
62+
expect(instance).to.have.property('bar', 'hello')
63+
})
64+
65+
it('returns the first element that has the exact target name', async () => {
66+
expect(instance).to.have.property('baz').exist.with.attribute('id', 'el5')
67+
})
68+
69+
it('returns target when there are mutliple target values', async () => {
70+
expect(instance).to.have.property('bing').exist.with.attribute('id', 'el6')
71+
})
72+
73+
it('returns targets when there are mutliple target values with different controllers', async () => {
74+
expect(instance).to.have.property('qux').exist.with.attribute('id', 'el8')
75+
})
76+
77+
it('returns targets from the shadowRoot, if available', async () => {
78+
instance.attachShadow({mode: 'open'})
79+
const el = document.createElement('div')
80+
el.setAttribute('data-target', 'target-test.shadow')
81+
instance.shadowRoot.appendChild(el)
82+
expect(instance).to.have.property('shadow', el)
83+
})
84+
85+
it('prioritises shadowRoot targets over others', async () => {
86+
instance.attachShadow({mode: 'open'})
87+
const shadowEl = document.createElement('div')
88+
shadowEl.setAttribute('data-target', 'target-test.foo')
89+
instance.shadowRoot.appendChild(shadowEl)
90+
expect(instance).to.have.property('foo', shadowEl)
91+
})
7092
})
7193

72-
it('prioritises shadowRoot targets over others', async () => {
73-
instance = await fixture(html` <find-target-test-element>
74-
<div data-target="find-target-test-element.foobar"></div>
75-
</find-target-test-element>`)
76-
instance.attachShadow({mode: 'open'})
77-
const shadowEl = document.createElement('div')
78-
shadowEl.setAttribute('data-target', 'find-target-test-element.foobar')
79-
instance.shadowRoot.appendChild(shadowEl)
80-
expect(findTarget(instance, 'foobar')).to.equal(shadowEl)
81-
})
82-
})
83-
84-
describe('findTargets', () => {
85-
let instance
86-
87-
it('calls querySelectorAll with the controller name and target name', async () => {
88-
instance = await fixture(html`<find-target-test-element>
89-
<div id="1" data-targets="find-target-test-element.foo"></div>
90-
<div id="2" data-targets="find-target-test-element.foo"></div>
91-
<div id="3" data-targets="find-target-test-element.bar"></div>
92-
</find-target-test-element>`)
93-
const els = findTargets(instance, 'foo')
94-
expect(els).to.have.lengthOf(2)
95-
expect(els[0]).to.have.attribute('id', '1')
96-
expect(els[1]).to.have.attribute('id', '2')
97-
})
98-
99-
it('returns all elements where closest tag is the controller', async () => {
100-
instance = await fixture(html`<find-target-test-element>
101-
<div id="1" data-targets="find-target-test-element.foo"></div>
102-
<div id="2" data-targets="find-target-test-element.foo"></div>
103-
<find-target-test-element>
104-
<div id="3" data-targets="find-target-test-element.foo"></div>
105-
</find-target-test-element>
106-
</find-target-test-element>`)
107-
const els = findTargets(instance, 'foo')
108-
expect(els).to.have.lengthOf(2)
109-
expect(els[0]).to.have.attribute('id', '1')
110-
expect(els[1]).to.have.attribute('id', '2')
111-
})
112-
113-
it('returns all elements inside a shadow root', async () => {
114-
instance = await fixture(html`<find-target-test-element></find-target-test-element>`)
115-
instance.attachShadow({mode: 'open'})
116-
const els = [document.createElement('div'), document.createElement('div'), document.createElement('div')]
117-
for (const el of els) el.setAttribute('data-targets', 'find-target-test-element.foo')
118-
119-
instance.shadowRoot.append(els[1])
120-
instance.append(els[0], els[2])
121-
122-
expect(findTargets(instance, 'foo')).to.eql([els[1], els[0], els[2]])
94+
describe('targets', () => {
95+
it('returns all elements where closest tag is the controller', async () => {
96+
expect(instance).to.have.property('foos').with.lengthOf(2)
97+
expect(instance).to.have.nested.property('foos[0]').with.attribute('id', 'el4')
98+
expect(instance).to.have.nested.property('foos[1]').with.attribute('id', 'el5')
99+
})
100+
101+
it('returns all elements inside a shadow root', async () => {
102+
instance.attachShadow({mode: 'open'})
103+
const els = [document.createElement('div'), document.createElement('div'), document.createElement('div')]
104+
for (const el of els) el.setAttribute('data-targets', 'target-test.foos')
105+
instance.shadowRoot.append(...els)
106+
107+
expect(instance).to.have.property('foos').with.lengthOf(5)
108+
expect(instance).to.have.nested.property('foos[0]', els[0])
109+
expect(instance).to.have.nested.property('foos[1]', els[1])
110+
expect(instance).to.have.nested.property('foos[2]', els[2])
111+
expect(instance).to.have.nested.property('foos[3]').with.attribute('id', 'el4')
112+
expect(instance).to.have.nested.property('foos[4]').with.attribute('id', 'el5')
113+
})
123114
})
124115
})

0 commit comments

Comments
 (0)