|
1 | 1 | import {expect} from '@open-wc/testing' |
| 2 | +import {restore, replace, fake} from 'sinon' |
2 | 3 | import {register} from '../src/register.js' |
3 | 4 |
|
4 | 5 | describe('register', () => { |
| 6 | + afterEach(() => { |
| 7 | + restore() |
| 8 | + }) |
| 9 | + |
5 | 10 | 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) |
9 | 14 | }) |
10 | 15 |
|
11 | 16 | it('does not register controllers that already exist', () => { |
12 | 17 | { |
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) |
16 | 26 | } |
| 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 | + ) |
17 | 36 | { |
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) |
21 | 40 | } |
| 41 | + expect(() => { |
| 42 | + @register |
| 43 | + class MyThirdClass {} |
| 44 | + expect(customElements.define).to.be.calledOnceWithExactly('my-third-class', MyThirdClass) |
| 45 | + }).to.throw(Error) |
22 | 46 | }) |
23 | 47 |
|
24 | 48 | it('dasherises class names', () => { |
| 49 | + @register |
25 | 50 | class ThisIsAnExampleOfDasherisedClassNames {} |
26 | | - register(ThisIsAnExampleOfDasherisedClassNames) |
27 | 51 | expect(window.customElements.get('this-is-an-example-of-dasherised-class-names')).to.equal( |
28 | 52 | ThisIsAnExampleOfDasherisedClassNames |
29 | 53 | ) |
30 | 54 | }) |
31 | 55 |
|
32 | 56 | it('will intuitively dasherize acryonyms', () => { |
| 57 | + @register |
33 | 58 | class URLBar {} |
34 | | - register(URLBar) |
35 | 59 | expect(window.customElements.get('url-bar')).to.equal(URLBar) |
36 | 60 | }) |
37 | 61 |
|
38 | 62 | it('dasherizes cap suffixed names correctly', () => { |
| 63 | + @register |
39 | 64 | class ClipX {} |
40 | | - register(ClipX) |
41 | 65 | expect(window.customElements.get('clip-x')).to.equal(ClipX) |
42 | 66 | }) |
43 | 67 |
|
44 | 68 | 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) |
48 | 72 | }) |
49 | 73 | }) |
0 commit comments