Skip to content

Commit aa18d3a

Browse files
authored
Merge pull request #201 from github/typescript-tests
Convert tests to TypeScript
2 parents 00fc037 + e2623f9 commit aa18d3a

File tree

10 files changed

+661
-75
lines changed

10 files changed

+661
-75
lines changed

package-lock.json

Lines changed: 590 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"clean": "tsc --build --clean",
2929
"lint": "eslint . --ignore-path .gitignore",
3030
"pretest": "npm run build && npm run lint",
31-
"test": "web-test-runner test/*.js --node-resolve",
31+
"test": "web-test-runner test/* --node-resolve",
3232
"postpublish": "npm publish --ignore-scripts --@github:registry='https://npm.pkg.github.com'"
3333
},
3434
"prettier": "@github/prettier-config",
@@ -38,6 +38,7 @@
3838
"@open-wc/testing": "^3.1.2",
3939
"@typescript-eslint/eslint-plugin": "^5.16.0",
4040
"@typescript-eslint/parser": "^5.16.0",
41+
"@web/dev-server-esbuild": "^0.3.0",
4142
"@web/test-runner": "^0.13.27",
4243
"eslint": "^8.12.0",
4344
"eslint-plugin-github": "^4.3.6",

test/attr.js renamed to test/attr.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,15 @@ describe('initializeAttrs', () => {
155155
})
156156

157157
describe('attr', () => {
158-
class AttrTestElement extends HTMLElement {}
159-
attr(AttrTestElement.prototype, 'foo')
160-
attr(AttrTestElement.prototype, 'bar')
158+
class AttrTestElement extends HTMLElement {
159+
@attr foo
160+
@attr bar
161+
}
161162
window.customElements.define('attr-test-element', AttrTestElement)
162163

163-
class ExtendedAttrTestElement extends AttrTestElement {}
164-
attr(ExtendedAttrTestElement.prototype, 'baz')
164+
class ExtendedAttrTestElement extends AttrTestElement {
165+
@attr baz
166+
}
165167
window.customElements.define('extended-attr-test-element', ExtendedAttrTestElement)
166168

167169
it('populates the "default" list for initializeAttrs', () => {
@@ -214,25 +216,28 @@ describe('defineObservedAttributes', () => {
214216
})
215217

216218
it('will reflect values from attr calls', () => {
217-
class TestElement extends HTMLElement {}
219+
class TestElement extends HTMLElement {
220+
@attr foo
221+
}
218222
defineObservedAttributes(TestElement)
219-
attr(TestElement.prototype, 'foo')
220223
expect(TestElement.observedAttributes).to.eql(['data-foo'])
221224
})
222225

223226
it('will reflect values even if set after definition', () => {
224-
class TestElement extends HTMLElement {}
227+
class TestElement extends HTMLElement {
228+
@attr foo
229+
}
225230
defineObservedAttributes(TestElement)
226-
attr(TestElement.prototype, 'foo')
227231
TestElement.observedAttributes = ['a', 'b', 'c']
228232
expect(TestElement.observedAttributes).to.eql(['data-foo', 'a', 'b', 'c'])
229233
})
230234

231235
it('will reflect values from extended elements', () => {
232-
class TestElement extends HTMLElement {}
236+
class TestElement extends HTMLElement {
237+
@attr foo
238+
}
233239
class ExtendedTestElement extends TestElement {}
234240
defineObservedAttributes(ExtendedTestElement)
235-
attr(TestElement.prototype, 'foo')
236241
expect(ExtendedTestElement.observedAttributes).to.eql(['data-foo'])
237242
})
238243
})
File renamed without changes.
Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,39 @@ describe('controller', () => {
1616
})
1717

1818
it('calls register', async () => {
19+
@controller
1920
class ControllerRegisterElement extends HTMLElement {}
20-
controller(ControllerRegisterElement)
2121
const instance = document.createElement('controller-register')
2222
root.appendChild(instance)
2323
expect(instance).to.be.instanceof(ControllerRegisterElement)
2424
})
2525

2626
it('adds data-catalyst to elements', async () => {
27-
controller(class ControllerDataAttrElement extends HTMLElement {})
27+
@controller
28+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
29+
class ControllerDataAttrElement extends HTMLElement {}
30+
2831
const instance = document.createElement('controller-data-attr')
2932
root.appendChild(instance)
3033
expect(instance.hasAttribute('data-catalyst')).to.equal(true)
3134
expect(instance.getAttribute('data-catalyst')).to.equal('')
3235
})
3336

3437
it('binds controllers before custom connectedCallback behaviour', async () => {
35-
controller(
36-
class ControllerBindOrderElement extends HTMLElement {
37-
foo() {
38-
return 'foo'
39-
}
38+
@controller
39+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
40+
class ControllerBindOrderElement extends HTMLElement {
41+
foo() {
42+
return 'foo'
4043
}
41-
)
42-
controller(
43-
class ControllerBindOrderSubElement extends HTMLElement {
44-
connectedCallback() {
45-
this.dispatchEvent(new CustomEvent('loaded'))
46-
}
44+
}
45+
@controller
46+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
47+
class ControllerBindOrderSubElement extends HTMLElement {
48+
connectedCallback() {
49+
this.dispatchEvent(new CustomEvent('loaded'))
4750
}
48-
)
51+
}
4952

5053
const instance = document.createElement('controller-bind-order')
5154
replace(instance, 'foo', fake(instance.foo))
@@ -59,20 +62,20 @@ describe('controller', () => {
5962
})
6063

6164
it('binds shadowRoots after connectedCallback behaviour', async () => {
62-
controller(
63-
class ControllerBindShadowElement extends HTMLElement {
64-
connectedCallback() {
65-
this.attachShadow({mode: 'open'})
66-
const button = document.createElement('button')
67-
button.setAttribute('data-action', 'click:controller-bind-shadow#foo')
68-
this.shadowRoot.appendChild(button)
69-
}
70-
71-
foo() {
72-
return 'foo'
73-
}
65+
@controller
66+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
67+
class ControllerBindShadowElement extends HTMLElement {
68+
connectedCallback() {
69+
this.attachShadow({mode: 'open'})
70+
const button = document.createElement('button')
71+
button.setAttribute('data-action', 'click:controller-bind-shadow#foo')
72+
this.shadowRoot.appendChild(button)
73+
}
74+
75+
foo() {
76+
return 'foo'
7477
}
75-
)
78+
}
7679
const instance = document.createElement('controller-bind-shadow')
7780
replace(instance, 'foo', fake(instance.foo))
7881
root.appendChild(instance)
@@ -83,13 +86,13 @@ describe('controller', () => {
8386
})
8487

8588
it('binds auto shadowRoots', async () => {
86-
controller(
87-
class ControllerBindAutoShadowElement extends HTMLElement {
88-
foo() {
89-
return 'foo'
90-
}
89+
@controller
90+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
91+
class ControllerBindAutoShadowElement extends HTMLElement {
92+
foo() {
93+
return 'foo'
9194
}
92-
)
95+
}
9396
const instance = document.createElement('controller-bind-auto-shadow')
9497
const template = document.createElement('template')
9598
template.setAttribute('data-shadowroot', 'open')
@@ -108,30 +111,32 @@ describe('controller', () => {
108111
})
109112

110113
it('upgrades child decendants when connected', () => {
111-
controller(class ChildElementElement extends HTMLElement {})
112-
controller(
113-
class ParentElementElement extends HTMLElement {
114-
connectedCallback() {
115-
const child = this.querySelector('child-element')
116-
expect(child.matches(':defined')).to.equal(true)
117-
}
114+
@controller
115+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
116+
class ChildElementElement extends HTMLElement {}
117+
@controller
118+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
119+
class ParentElementElement extends HTMLElement {
120+
connectedCallback() {
121+
const child = this.querySelector('child-element')
122+
expect(child.matches(':defined')).to.equal(true)
118123
}
119-
)
124+
}
120125

121126
// eslint-disable-next-line github/unescaped-html-literal
122127
root.innerHTML = '<parent-element><child-element></child-element></parent-element>'
123128
})
124129

125130
describe('attrs', () => {
126131
let attrValues = []
132+
@controller
127133
class AttributeTestElement extends HTMLElement {
128134
foo = 'baz'
129135
attributeChangedCallback() {
130136
attrValues.push(this.getAttribute('data-foo'))
131137
attrValues.push(this.foo)
132138
}
133139
}
134-
controller(AttributeTestElement)
135140
attr(AttributeTestElement.prototype, 'foo')
136141

137142
beforeEach(() => {
File renamed without changes.

test/karma.config.cjs

Lines changed: 0 additions & 21 deletions
This file was deleted.
File renamed without changes.

web-test-runner.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {esbuildPlugin} from '@web/dev-server-esbuild'
2+
3+
export default {
4+
files: ['test/*'],
5+
plugins: [esbuildPlugin({ts: true})]
6+
}

0 commit comments

Comments
 (0)