Skip to content

Commit a90332d

Browse files
authored
Merge pull request #214 from github/clean-up-register-tests
2 parents f0e9761 + 5e26203 commit a90332d

2 files changed

Lines changed: 42 additions & 19 deletions

File tree

src/register.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {dasherize} from './dasherize.js'
88
*
99
* Example: HelloController => hello-controller
1010
*/
11-
export function register(classObject: CustomElement): void {
11+
export function register(classObject: CustomElement): CustomElement {
1212
const name = dasherize(classObject.name).replace(/-element$/, '')
1313

1414
try {
@@ -19,8 +19,7 @@ export function register(classObject: CustomElement): void {
1919
} catch (e: unknown) {
2020
// The only reason for window.customElements.define to throw a `NotSupportedError`
2121
// is if the element has already been defined.
22-
if (e instanceof DOMException && e.name === 'NotSupportedError') return
23-
24-
throw e
22+
if (!(e instanceof DOMException && e.name === 'NotSupportedError')) throw e
2523
}
24+
return classObject
2625
}

test/register.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,73 @@
11
import {expect} from '@open-wc/testing'
2+
import {restore, replace, fake} from 'sinon'
23
import {register} from '../src/register.js'
34

45
describe('register', () => {
6+
afterEach(() => {
7+
restore()
8+
})
9+
510
it('registers the class as a custom element, normalising the class name', () => {
6-
class MyFirstController {}
7-
register(MyFirstController)
8-
expect(window.customElements.get('my-first-controller')).to.equal(MyFirstController)
11+
@register
12+
class MyFirstClass {}
13+
expect(window.customElements.get('my-first-class')).to.equal(MyFirstClass)
914
})
1015

1116
it('does not register controllers that already exist', () => {
1217
{
13-
class MySecondController {}
14-
register(MySecondController)
15-
expect(window.customElements.get('my-second-controller')).to.equal(MySecondController)
18+
@register
19+
class MySecondClass {}
20+
expect(window.customElements.get('my-second-class')).to.equal(MySecondClass)
21+
}
22+
{
23+
@register
24+
class MySecondClass {}
25+
expect(window.customElements.get('my-second-class')).to.not.equal(MySecondClass)
1626
}
27+
})
28+
29+
it('will redefine controllers, catching on errors', () => {
30+
replace(customElements, 'define', fake())
31+
replace(
32+
customElements,
33+
'get',
34+
fake(() => class {})
35+
)
1736
{
18-
class MySecondController {}
19-
register(MySecondController)
20-
expect(window.customElements.get('my-second-controller')).to.not.equal(MySecondController)
37+
@register
38+
class MyThirdClass {}
39+
expect(customElements.define).to.be.calledOnceWithExactly('my-third-class', MyThirdClass)
2140
}
41+
expect(() => {
42+
@register
43+
class MyThirdClass {}
44+
expect(customElements.define).to.be.calledOnceWithExactly('my-third-class', MyThirdClass)
45+
}).to.throw(Error)
2246
})
2347

2448
it('dasherises class names', () => {
49+
@register
2550
class ThisIsAnExampleOfDasherisedClassNames {}
26-
register(ThisIsAnExampleOfDasherisedClassNames)
2751
expect(window.customElements.get('this-is-an-example-of-dasherised-class-names')).to.equal(
2852
ThisIsAnExampleOfDasherisedClassNames
2953
)
3054
})
3155

3256
it('will intuitively dasherize acryonyms', () => {
57+
@register
3358
class URLBar {}
34-
register(URLBar)
3559
expect(window.customElements.get('url-bar')).to.equal(URLBar)
3660
})
3761

3862
it('dasherizes cap suffixed names correctly', () => {
63+
@register
3964
class ClipX {}
40-
register(ClipX)
4165
expect(window.customElements.get('clip-x')).to.equal(ClipX)
4266
})
4367

4468
it('automatically drops the `Element` suffix', () => {
45-
class AutoCompleteElement {}
46-
register(AutoCompleteElement)
47-
expect(window.customElements.get('auto-complete')).to.equal(AutoCompleteElement)
69+
@register
70+
class FirstSuffixElement {}
71+
expect(window.customElements.get('first-suffix')).to.equal(FirstSuffixElement)
4872
})
4973
})

0 commit comments

Comments
 (0)