Skip to content

Commit 0e07093

Browse files
committed
ease setting up tests
1 parent e05e521 commit 0e07093

File tree

8 files changed

+37
-28
lines changed

8 files changed

+37
-28
lines changed

tests/_helpers/elements/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {ShadowInput} from './shadow-input'
2+
3+
const customElements = {
4+
'shadow-input': ShadowInput,
5+
}
6+
7+
export type CustomElements = {
8+
[k in keyof typeof customElements]: typeof customElements[k] extends {
9+
new (): infer T
10+
}
11+
? T
12+
: never
13+
}
14+
15+
export function registerCustomElements() {
16+
Object.entries(customElements).forEach(([name, constructor]) => {
17+
if (!globalThis.customElements.get(name)) {
18+
globalThis.customElements.define(name, constructor)
19+
}
20+
})
21+
}

tests/_helpers/shadow-input.ts renamed to tests/_helpers/elements/shadow-input.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ template.innerHTML = `
55
<input>
66
`
77

8-
class ShadowInput extends HTMLElement {
8+
export class ShadowInput extends HTMLElement {
99
private $input?: HTMLInputElement
1010

1111
static getObservedAttributes() {
@@ -40,11 +40,3 @@ class ShadowInput extends HTMLElement {
4040
return this.$input?.value ?? ''
4141
}
4242
}
43-
44-
export type {ShadowInput}
45-
46-
export function defineShadowInputCustomElementIfNotDefined() {
47-
if (window.customElements.get('shadow-input') === undefined) {
48-
window.customElements.define('shadow-input', ShadowInput)
49-
}
50-
}

tests/_helpers/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {CustomElements, registerCustomElements} from './elements'
2+
13
// this is pretty helpful:
24
// https://codesandbox.io/s/quizzical-worker-eo909
35

@@ -11,5 +13,9 @@ expect.addSnapshotSerializer({
1113
print: val => String((<null | {snapshot?: string}>val)?.snapshot),
1214
})
1315

16+
registerCustomElements()
17+
18+
export type {CustomElements}
19+
1420
export {render, setup} from './setup'
1521
export {addEventListener, addListeners} from './listeners'

tests/clipboard/copy.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {defineShadowInputCustomElementIfNotDefined} from '../_helpers/shadow-input'
21
import userEvent from '#src'
32
import {render, setup} from '#testHelpers'
43

@@ -102,7 +101,6 @@ describe('without Clipboard API', () => {
102101

103102
describe('on shadow DOM', () => {
104103
test('copy in an input element', async () => {
105-
defineShadowInputCustomElementIfNotDefined()
106104
const {user} = setup('<shadow-input value="test"></shadow-input>', {
107105
selection: {anchorOffset: 0, focusOffset: 4},
108106
})

tests/clipboard/cut.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type {ShadowInput} from '../_helpers/shadow-input'
2-
import {defineShadowInputCustomElementIfNotDefined} from '../_helpers/shadow-input'
1+
import type {ShadowInput} from '../_helpers/elements/shadow-input'
32
import userEvent from '#src'
43
import {render, setup} from '#testHelpers'
54

@@ -110,7 +109,6 @@ describe('without Clipboard API', () => {
110109
})
111110
describe('on shadow DOM', () => {
112111
test('cut in an input element', async () => {
113-
defineShadowInputCustomElementIfNotDefined()
114112
const {element, user} = setup<ShadowInput>(
115113
'<shadow-input value="test"></shadow-input>',
116114
{

tests/clipboard/paste.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import type {ShadowInput} from '../_helpers/shadow-input'
2-
import {defineShadowInputCustomElementIfNotDefined} from '../_helpers/shadow-input'
31
import userEvent from '#src'
4-
import {render, setup} from '#testHelpers'
2+
import {CustomElements, render, setup} from '#testHelpers'
53
import {createDataTransfer} from '#src/utils'
64

75
test('paste with empty clipboard', async () => {
@@ -154,8 +152,9 @@ describe('without Clipboard API', () => {
154152

155153
describe('on shadow DOM', () => {
156154
test('paste into an input element', async () => {
157-
defineShadowInputCustomElementIfNotDefined()
158-
const {element, user} = setup<ShadowInput>('<shadow-input></shadow-input>')
155+
const {element, user} = setup<CustomElements['shadow-input']>(
156+
'<shadow-input></shadow-input>',
157+
)
159158

160159
await user.paste('test')
161160

tests/keyboard/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import userEvent from '#src'
2-
import {addListeners, render, setup} from '#testHelpers'
3-
import {
4-
defineShadowInputCustomElementIfNotDefined,
5-
ShadowInput,
6-
} from '../_helpers/shadow-input'
2+
import {addListeners, CustomElements, render, setup} from '#testHelpers'
73

84
it('type without focus', async () => {
95
const {element, user} = setup('<input/>', {focus: false})
@@ -170,8 +166,9 @@ test('disabling activeElement moves action to HTMLBodyElement', async () => {
170166

171167
describe('on shadow DOM', () => {
172168
test('type into an input element', async () => {
173-
defineShadowInputCustomElementIfNotDefined()
174-
const {element, user} = setup<ShadowInput>('<shadow-input></shadow-input>')
169+
const {element, user} = setup<CustomElements['shadow-input']>(
170+
'<shadow-input></shadow-input>',
171+
)
175172

176173
await user.keyboard('test')
177174

tests/utils/focus/getActiveElement.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {defineShadowInputCustomElementIfNotDefined} from '../../_helpers/shadow-input'
21
import {setup} from '#testHelpers'
32
import {getActiveElementOrBody} from '#src/utils'
43

@@ -16,7 +15,6 @@ test('default to body as active element', async () => {
1615

1716
describe('on shadow DOM', () => {
1817
test('get focused element inside shadow tree', async () => {
19-
defineShadowInputCustomElementIfNotDefined()
2018
const {element} = setup('<shadow-input></shadow-input>')
2119

2220
expect(getActiveElementOrBody(document)).toBe(

0 commit comments

Comments
 (0)