@api-client/ui 0.4.5 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/src/elements/highlight/MarkedHighlight.d.ts.map +1 -1
- package/build/src/elements/highlight/MarkedHighlight.js +2 -1
- package/build/src/elements/highlight/MarkedHighlight.js.map +1 -1
- package/build/src/md/button/internals/base.js +1 -1
- package/build/src/md/button/internals/base.js.map +1 -1
- package/build/src/md/dialog/internals/Dialog.d.ts +18 -0
- package/build/src/md/dialog/internals/Dialog.d.ts.map +1 -1
- package/build/src/md/dialog/internals/Dialog.js +60 -2
- package/build/src/md/dialog/internals/Dialog.js.map +1 -1
- package/build/src/md/menu/internal/Menu.d.ts +1 -0
- package/build/src/md/menu/internal/Menu.d.ts.map +1 -1
- package/build/src/md/menu/internal/Menu.js +41 -1
- package/build/src/md/menu/internal/Menu.js.map +1 -1
- package/build/src/md/menu/internal/Menu.styles.d.ts.map +1 -1
- package/build/src/md/menu/internal/Menu.styles.js +4 -0
- package/build/src/md/menu/internal/Menu.styles.js.map +1 -1
- package/demo/md/dialog/dialog.ts +135 -1
- package/demo/md/menu/index.ts +216 -1
- package/package.json +2 -2
- package/src/elements/highlight/MarkedHighlight.ts +2 -1
- package/src/md/button/internals/base.ts +1 -1
- package/src/md/dialog/internals/Dialog.ts +54 -1
- package/src/md/menu/internal/Menu.styles.ts +4 -0
- package/src/md/menu/internal/Menu.ts +43 -1
- package/test/README.md +372 -0
- package/test/dom-assertions.test.ts +182 -0
- package/test/helpers/TestUtils.ts +243 -0
- package/test/helpers/UiMock.ts +83 -13
- package/test/md/dialog/UiDialog.test.ts +169 -0
- package/test/setup.test.ts +217 -0
- package/test/setup.ts +117 -0
|
@@ -8,6 +8,7 @@ import type { TypedEvents } from '../../../core/types.js'
|
|
|
8
8
|
import { ifDefined } from 'lit/directives/if-defined.js'
|
|
9
9
|
import { SyntheticSubmitEvent } from '../../../events/SyntheticSubmitEvent.js'
|
|
10
10
|
import '../../button/ui-button.js'
|
|
11
|
+
import { bound } from '../../../decorators/bound.js'
|
|
11
12
|
|
|
12
13
|
export interface UiDialogClosingReason {
|
|
13
14
|
/**
|
|
@@ -137,6 +138,8 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
|
|
|
137
138
|
* - When the submit event is not cancelled, then:
|
|
138
139
|
* - The default confirm action is invoked.
|
|
139
140
|
* - The dialog is closed.
|
|
141
|
+
*
|
|
142
|
+
* @deprecated Wrap the content in a `<form>` element instead.
|
|
140
143
|
*/
|
|
141
144
|
@property({ type: Boolean }) accessor useForm: boolean | undefined
|
|
142
145
|
|
|
@@ -147,6 +150,15 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
|
|
|
147
150
|
* @attribute
|
|
148
151
|
*/
|
|
149
152
|
@property({ type: String }) accessor confirmValue: string | undefined
|
|
153
|
+
/**
|
|
154
|
+
* When the dialog is wrapped in a form, set this to `true` to close the dialog
|
|
155
|
+
* when the form is submitted.
|
|
156
|
+
*
|
|
157
|
+
* Note that the dialog doesn't perform any validation of the form. It only closes
|
|
158
|
+
* when the form is submitted, regardless of the application logic. The `submit` event
|
|
159
|
+
* is dispatched by the dialog when the form is valid.
|
|
160
|
+
*/
|
|
161
|
+
@property({ type: Boolean }) accessor submitClose: boolean | undefined
|
|
150
162
|
|
|
151
163
|
/**
|
|
152
164
|
* A reference to the underlying dialog element.
|
|
@@ -173,14 +185,32 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
|
|
|
173
185
|
|
|
174
186
|
#formId?: string
|
|
175
187
|
|
|
188
|
+
/**
|
|
189
|
+
* @deprecated Use `useForm` instead.
|
|
190
|
+
*/
|
|
176
191
|
get formId(): string | undefined {
|
|
177
192
|
return this.#formId
|
|
178
193
|
}
|
|
179
194
|
|
|
195
|
+
/**
|
|
196
|
+
* @deprecated This will be removed in the future.
|
|
197
|
+
*/
|
|
180
198
|
@state() accessor #inject: TemplateResult | RenderFunction | undefined
|
|
181
199
|
|
|
200
|
+
/**
|
|
201
|
+
* @deprecated This will be removed in the future.
|
|
202
|
+
*/
|
|
182
203
|
#pendingStyles?: CSSResultOrNative[]
|
|
183
204
|
|
|
205
|
+
/**
|
|
206
|
+
* A reference to the parent form element.
|
|
207
|
+
* When a form is found, the dialog will hook into the form's submit event
|
|
208
|
+
* and close the dialog when the form is submitted.
|
|
209
|
+
* Since the `submit` event is dispatched when the form is valid,
|
|
210
|
+
* we can use this to close the dialog and not worry about the form validation.
|
|
211
|
+
*/
|
|
212
|
+
#form: HTMLFormElement | null = null
|
|
213
|
+
|
|
184
214
|
constructor() {
|
|
185
215
|
super()
|
|
186
216
|
|
|
@@ -192,6 +222,7 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
|
|
|
192
222
|
* Allows to inject a template into the internals of the element.
|
|
193
223
|
* This is helpful when working with imperative dialogs.
|
|
194
224
|
* @param content The content to inject into the body.
|
|
225
|
+
* @deprecated This will be removed in the future. To use forms, wrap the content in a `<form>` element.
|
|
195
226
|
*/
|
|
196
227
|
inject(content?: TemplateResult | RenderFunction, styles?: CSSResultOrNative[]): void {
|
|
197
228
|
this.#inject = content
|
|
@@ -210,6 +241,22 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
|
|
|
210
241
|
}
|
|
211
242
|
}
|
|
212
243
|
|
|
244
|
+
override connectedCallback(): void {
|
|
245
|
+
super.connectedCallback()
|
|
246
|
+
this.#form = this.closest('form')
|
|
247
|
+
if (this.#form) {
|
|
248
|
+
this.#form.addEventListener('submit', this.handleFormSubmit)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
override disconnectedCallback(): void {
|
|
253
|
+
super.disconnectedCallback()
|
|
254
|
+
if (this.#form) {
|
|
255
|
+
this.#form.removeEventListener('submit', this.handleFormSubmit)
|
|
256
|
+
this.#form = null
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
213
260
|
protected override firstUpdated(): void {
|
|
214
261
|
const styles = this.#pendingStyles
|
|
215
262
|
if (styles) {
|
|
@@ -226,6 +273,13 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
|
|
|
226
273
|
}
|
|
227
274
|
}
|
|
228
275
|
|
|
276
|
+
@bound
|
|
277
|
+
protected handleFormSubmit(): void {
|
|
278
|
+
if (this.submitClose) {
|
|
279
|
+
this.handleInteraction('confirm')
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
229
283
|
override handleClick(e: MouseEvent): void {
|
|
230
284
|
super.handleClick(e)
|
|
231
285
|
const path = e.composedPath()
|
|
@@ -238,7 +292,6 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
|
|
|
238
292
|
// Adds support for forms.
|
|
239
293
|
// When a form's submit button is clicked we yield the flow control to the form.
|
|
240
294
|
// This way the form can handle the submit event.
|
|
241
|
-
e.preventDefault()
|
|
242
295
|
return
|
|
243
296
|
}
|
|
244
297
|
const { value = '' } = button
|
|
@@ -9,6 +9,10 @@ export default css`
|
|
|
9
9
|
margin: 0;
|
|
10
10
|
padding: 0;
|
|
11
11
|
border: none;
|
|
12
|
+
overflow: hidden;
|
|
13
|
+
/* in most cases the max-height won't matter as this assumes the whole screen to be available, which is rarely the truth. */
|
|
14
|
+
max-height: 90vh;
|
|
15
|
+
overflow: auto;
|
|
12
16
|
}
|
|
13
17
|
|
|
14
18
|
:host(:popover-open) {
|
|
@@ -70,10 +70,12 @@ export default class Menu extends UiList {
|
|
|
70
70
|
this.open = !this.open
|
|
71
71
|
this.ariaExpanded = String(this.open)
|
|
72
72
|
this.tabIndex = this.open ? 0 : -1
|
|
73
|
+
const result = super.togglePopover(force)
|
|
73
74
|
if (this.open) {
|
|
75
|
+
this.positionMenu()
|
|
74
76
|
this.focus()
|
|
75
77
|
}
|
|
76
|
-
return
|
|
78
|
+
return result
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
/**
|
|
@@ -84,6 +86,7 @@ export default class Menu extends UiList {
|
|
|
84
86
|
this.ariaExpanded = 'true'
|
|
85
87
|
this.showPopover()
|
|
86
88
|
this.open = true
|
|
89
|
+
this.positionMenu()
|
|
87
90
|
this.focus()
|
|
88
91
|
this.dispatchEvent(new CustomEvent('open', { bubbles: false, composed: true }))
|
|
89
92
|
}
|
|
@@ -100,6 +103,45 @@ export default class Menu extends UiList {
|
|
|
100
103
|
this.dispatchEvent(new CustomEvent('close', { bubbles: false, composed: true }))
|
|
101
104
|
}
|
|
102
105
|
|
|
106
|
+
positionMenu(): void {
|
|
107
|
+
// when there's more space above the anchor, position the menu above it
|
|
108
|
+
const box = this.getBoundingClientRect()
|
|
109
|
+
// Now, we determine, whether to position the menu above or below the anchor
|
|
110
|
+
// in a way, that if we have enough space below the anchor, we position it below,
|
|
111
|
+
// otherwise we position it above the anchor.
|
|
112
|
+
|
|
113
|
+
// our starting point is the anchor being positioned below the anchor
|
|
114
|
+
const menuBottom = box.top + box.height
|
|
115
|
+
if (menuBottom <= innerHeight) {
|
|
116
|
+
// if the menu fits below the anchor, we leave it as is.
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
// we do not make association from the menu to the anchor, so we make an assumption
|
|
120
|
+
// that the anchor is 40px high, which is the default height of a button.
|
|
121
|
+
// it can be different, but this is a good starting point.
|
|
122
|
+
const anchorHeight = 40
|
|
123
|
+
const anchorBottom = box.top
|
|
124
|
+
const anchorTop = anchorBottom - anchorHeight
|
|
125
|
+
const menuHeight = box.height
|
|
126
|
+
|
|
127
|
+
const spaceBelow = innerHeight - anchorBottom
|
|
128
|
+
const spaceAbove = anchorTop
|
|
129
|
+
|
|
130
|
+
const diffBelow = spaceBelow - menuHeight
|
|
131
|
+
const diffAbove = spaceAbove - menuHeight
|
|
132
|
+
// The initial check ensures the menu does not fit below. Now, check if it fits above.
|
|
133
|
+
if (diffAbove >= 0) {
|
|
134
|
+
this.style.setProperty('position-area', 'top span-right')
|
|
135
|
+
} else if (diffAbove > diffBelow) {
|
|
136
|
+
// It doesn't fit in either direction. Choose the one with less overflow (larger, i.e., less negative, diff).
|
|
137
|
+
this.style.setProperty('position-area', 'top span-right')
|
|
138
|
+
this.style.maxHeight = `${spaceAbove}px`
|
|
139
|
+
} else {
|
|
140
|
+
this.style.setProperty('position-area', 'bottom span-right')
|
|
141
|
+
this.style.maxHeight = `${spaceBelow}px`
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
103
145
|
/**
|
|
104
146
|
* Handles beforetoggle event from popover
|
|
105
147
|
*/
|
package/test/README.md
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
# Test Setup Guide
|
|
2
|
+
|
|
3
|
+
This document explains how to use the test infrastructure for the UI component library.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The test setup provides:
|
|
8
|
+
|
|
9
|
+
- **Global test utilities** for common testing patterns
|
|
10
|
+
- **Custom matchers** for better assertions
|
|
11
|
+
- **UI interaction helpers** for simulating user actions
|
|
12
|
+
- **Async helpers** for handling asynchronous operations
|
|
13
|
+
- **Performance testing utilities**
|
|
14
|
+
|
|
15
|
+
## Test Configuration
|
|
16
|
+
|
|
17
|
+
### Web Test Runner
|
|
18
|
+
|
|
19
|
+
Tests run using [Web Test Runner](https://modern-web.dev/docs/test-runner/overview/) with:
|
|
20
|
+
|
|
21
|
+
- **Playwright** for browser automation
|
|
22
|
+
- **OAuth2 mock server** for authentication testing
|
|
23
|
+
- **Coverage reporting** with lcov format
|
|
24
|
+
- **Custom test HTML** with Material Design 3 styles
|
|
25
|
+
|
|
26
|
+
### File Structure
|
|
27
|
+
|
|
28
|
+
```plain
|
|
29
|
+
test/
|
|
30
|
+
├── setup.ts # Global test setup
|
|
31
|
+
├── setup.test.ts # Example test demonstrating setup
|
|
32
|
+
├── env.ts # Environment configuration
|
|
33
|
+
├── helpers/
|
|
34
|
+
│ ├── UiMock.ts # UI interaction utilities
|
|
35
|
+
│ └── TestUtils.ts # Extended test utilities
|
|
36
|
+
└── [component-tests]/ # Component-specific tests
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Running Tests
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
# Run all tests
|
|
43
|
+
npm run test
|
|
44
|
+
|
|
45
|
+
# Run tests in watch mode
|
|
46
|
+
npm run test:watch
|
|
47
|
+
|
|
48
|
+
# Run tests with coverage
|
|
49
|
+
npm run test:coverage
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Global Test Utilities
|
|
53
|
+
|
|
54
|
+
The setup provides global utilities accessible via `window.testUtils`:
|
|
55
|
+
|
|
56
|
+
### waitForElement(selector, timeout?)
|
|
57
|
+
|
|
58
|
+
Wait for an element to appear in the DOM:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
const element = await window.testUtils.waitForElement('#my-element', 5000)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### waitForCondition(condition, timeout?)
|
|
65
|
+
|
|
66
|
+
Wait for a condition to become true:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
await window.testUtils.waitForCondition(() => someVariable === true, 5000)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Custom Matchers
|
|
73
|
+
|
|
74
|
+
Enhanced assertion helpers in `customMatchers`:
|
|
75
|
+
|
|
76
|
+
### toHaveClasses(element, ...classes)
|
|
77
|
+
|
|
78
|
+
Check if element has specific CSS classes:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
const result = customMatchers.toHaveClasses(element, 'active', 'highlighted')
|
|
82
|
+
assert.isTrue(result.pass)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### toBeVisible(element)
|
|
86
|
+
|
|
87
|
+
Check if element is visible (not hidden by CSS):
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const result = customMatchers.toBeVisible(element)
|
|
91
|
+
assert.isTrue(result.pass)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### toHaveAttribute(element, attribute, value?)
|
|
95
|
+
|
|
96
|
+
Check if element has specific attribute:
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
const result = customMatchers.toHaveAttribute(element, 'data-test', 'value')
|
|
100
|
+
assert.isTrue(result.pass)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## UI Interaction (UiMock)
|
|
104
|
+
|
|
105
|
+
Simulate user interactions:
|
|
106
|
+
|
|
107
|
+
### Keyboard Events
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// Single key events
|
|
111
|
+
UiMock.keyDown(element, 'Enter')
|
|
112
|
+
UiMock.keyUp(element, 'Escape')
|
|
113
|
+
|
|
114
|
+
// Full key press (down + up)
|
|
115
|
+
await UiMock.keyPress(element, 'Space', { ctrlKey: true })
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Mouse Events
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
// Get element center
|
|
122
|
+
const center = UiMock.getMiddleOfElement(element)
|
|
123
|
+
|
|
124
|
+
// Move mouse between points
|
|
125
|
+
await UiMock.moveMouse(fromPoint, toPoint, steps)
|
|
126
|
+
|
|
127
|
+
// Drag and drop
|
|
128
|
+
await UiMock.dragAndDrop(sourceElement, targetElement)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### File Drag Events
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
const file = new File(['content'], 'test.txt', { type: 'text/plain' })
|
|
135
|
+
const dragEvent = UiMock.getFileDragEvent('drop', { file })
|
|
136
|
+
element.dispatchEvent(dragEvent)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Async Helpers
|
|
140
|
+
|
|
141
|
+
Handle asynchronous operations:
|
|
142
|
+
|
|
143
|
+
### waitForAll(conditions, timeout?)
|
|
144
|
+
|
|
145
|
+
Wait for multiple conditions:
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
await asyncHelpers.waitForAll([
|
|
149
|
+
() => condition1,
|
|
150
|
+
() => condition2,
|
|
151
|
+
], 5000)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### waitForAny(conditions, timeout?)
|
|
155
|
+
|
|
156
|
+
Wait for any condition to be true:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const index = await asyncHelpers.waitForAny([
|
|
160
|
+
() => condition1,
|
|
161
|
+
() => condition2,
|
|
162
|
+
])
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### retry(operation, maxAttempts?, baseDelay?)
|
|
166
|
+
|
|
167
|
+
Retry failed operations with exponential backoff:
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
const result = await asyncHelpers.retry(async () => {
|
|
171
|
+
const response = await fetch('/api/data')
|
|
172
|
+
if (!response.ok) throw new Error('Failed')
|
|
173
|
+
return response.json()
|
|
174
|
+
}, 3, 100)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Test Factories
|
|
178
|
+
|
|
179
|
+
Create reusable test data and element factories:
|
|
180
|
+
|
|
181
|
+
### Data Factory
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
const userFactory = testFactory.createDataFactory({
|
|
185
|
+
id: 1,
|
|
186
|
+
name: 'Test User',
|
|
187
|
+
email: 'test@example.com',
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
const user1 = userFactory() // Uses defaults
|
|
191
|
+
const user2 = userFactory({ name: 'Custom User' }) // Override specific fields
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Element Test Suite
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
const suite = testFactory.createElementTestSuite('my-element', '../src/my-element.js')
|
|
198
|
+
|
|
199
|
+
// In tests:
|
|
200
|
+
await suite.setup()
|
|
201
|
+
const element = await suite.createElement({ attribute: 'value' }, 'content')
|
|
202
|
+
// ... test element ...
|
|
203
|
+
suite.cleanup()
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Performance Testing
|
|
207
|
+
|
|
208
|
+
Measure and assert performance:
|
|
209
|
+
|
|
210
|
+
### measureTime(fn)
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
const { result, duration } = await performanceHelpers.measureTime(async () => {
|
|
214
|
+
return await someExpensiveOperation()
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
console.log(`Operation took ${duration}ms`)
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### expectWithinTime(operation, maxDuration, message?)
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
const result = await performanceHelpers.expectWithinTime(
|
|
224
|
+
() => renderLargeList(),
|
|
225
|
+
1000, // max 1 second
|
|
226
|
+
'List rendering should be fast'
|
|
227
|
+
)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Writing Component Tests
|
|
231
|
+
|
|
232
|
+
### Basic Structure
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
import { assert, fixture, html } from '@open-wc/testing'
|
|
236
|
+
import { customMatchers } from '../helpers/TestUtils.js'
|
|
237
|
+
import { UiMock } from '../helpers/UiMock.js'
|
|
238
|
+
|
|
239
|
+
// Import test setup
|
|
240
|
+
import '../setup.js'
|
|
241
|
+
|
|
242
|
+
// Import component
|
|
243
|
+
import '../src/my-component.js'
|
|
244
|
+
|
|
245
|
+
describe('MyComponent', () => {
|
|
246
|
+
async function basicFixture() {
|
|
247
|
+
return fixture(html`<my-component></my-component>`)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
it('should render correctly', async () => {
|
|
251
|
+
const element = await basicFixture()
|
|
252
|
+
|
|
253
|
+
// Use custom matchers
|
|
254
|
+
const result = customMatchers.toBeVisible(element)
|
|
255
|
+
assert.isTrue(result.pass)
|
|
256
|
+
|
|
257
|
+
// Test functionality
|
|
258
|
+
await UiMock.keyPress(element, 'Enter')
|
|
259
|
+
|
|
260
|
+
// Wait for changes
|
|
261
|
+
await window.testUtils.waitForCondition(() =>
|
|
262
|
+
element.hasAttribute('data-processed')
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
assert.isTrue(element.hasAttribute('data-processed'))
|
|
266
|
+
})
|
|
267
|
+
})
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Testing Custom Elements
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
describe('CustomElement', () => {
|
|
274
|
+
let element: HTMLElement
|
|
275
|
+
|
|
276
|
+
beforeEach(async () => {
|
|
277
|
+
element = await fixture(html`<custom-element
|
|
278
|
+
.property="${'value'}"
|
|
279
|
+
@event="${(e) => console.log(e)}"
|
|
280
|
+
></custom-element>`)
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
it('should handle properties', () => {
|
|
284
|
+
// Test property binding
|
|
285
|
+
assert.equal(element.property, 'value')
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
it('should handle events', async () => {
|
|
289
|
+
let eventFired = false
|
|
290
|
+
element.addEventListener('custom-event', () => {
|
|
291
|
+
eventFired = true
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
// Trigger event
|
|
295
|
+
element.dispatchEvent(new CustomEvent('trigger'))
|
|
296
|
+
|
|
297
|
+
await window.testUtils.waitForCondition(() => eventFired)
|
|
298
|
+
assert.isTrue(eventFired)
|
|
299
|
+
})
|
|
300
|
+
})
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Environment Variables
|
|
304
|
+
|
|
305
|
+
Test environment configuration is available through the mock server:
|
|
306
|
+
|
|
307
|
+
- OAuth2 server details available in test environment
|
|
308
|
+
- Custom environment setup via `env.ts`
|
|
309
|
+
|
|
310
|
+
## Coverage Reports
|
|
311
|
+
|
|
312
|
+
Coverage reports are generated in `coverage/` directory:
|
|
313
|
+
|
|
314
|
+
- `lcov.info` - Machine-readable coverage data
|
|
315
|
+
- `lcov-report/` - Human-readable HTML reports
|
|
316
|
+
|
|
317
|
+
Open `coverage/lcov-report/index.html` to view detailed coverage information.
|
|
318
|
+
|
|
319
|
+
## Best Practices
|
|
320
|
+
|
|
321
|
+
1. **Use the global setup**: Import `../setup.js` in test files
|
|
322
|
+
2. **Leverage custom matchers**: Use `customMatchers` for better assertions
|
|
323
|
+
3. **Simulate real interactions**: Use `UiMock` for realistic user interactions
|
|
324
|
+
4. **Handle async operations**: Use `asyncHelpers` for waiting and retrying
|
|
325
|
+
5. **Create reusable factories**: Use `testFactory` for common test data
|
|
326
|
+
6. **Test performance**: Use `performanceHelpers` for timing-critical code
|
|
327
|
+
7. **Clean up properly**: The global setup handles cleanup automatically
|
|
328
|
+
8. **Use meaningful assertions**: Prefer descriptive custom matchers over generic ones
|
|
329
|
+
|
|
330
|
+
## DOM Element Assertions
|
|
331
|
+
|
|
332
|
+
When comparing DOM elements, always use the special DOM assertion methods from `@open-wc/testing`:
|
|
333
|
+
|
|
334
|
+
### assert.dom.equal(actual, expected)
|
|
335
|
+
|
|
336
|
+
Compare DOM elements for equality:
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
const element1 = document.querySelector('#test')
|
|
340
|
+
const element2 = await window.testUtils.waitForElement('#test')
|
|
341
|
+
assert.dom.equal(element1, element2)
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### assert.dom.notEqual(actual, expected)
|
|
345
|
+
|
|
346
|
+
Assert that DOM elements are not the same:
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
const element1 = document.querySelector('#test1')
|
|
350
|
+
const element2 = document.querySelector('#test2')
|
|
351
|
+
assert.dom.notEqual(element1, element2)
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
### assert.dom.equalSnapshot(element, snapshot)
|
|
355
|
+
|
|
356
|
+
Compare element with a snapshot:
|
|
357
|
+
|
|
358
|
+
```typescript
|
|
359
|
+
const element = await fixture(html`<div>Content</div>`)
|
|
360
|
+
assert.dom.equalSnapshot(element, '<div>Content</div>')
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### assert.dom.notEqualSnapshot(element, snapshot)
|
|
364
|
+
|
|
365
|
+
Assert element doesn't match snapshot:
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
const element = await fixture(html`<div>Changed</div>`)
|
|
369
|
+
assert.dom.notEqualSnapshot(element, '<div>Original</div>')
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
> ⚠️ **Important**: Never use regular `assert.equal()` for DOM elements as it won't work correctly. Always use `assert.dom.*` methods for DOM comparisons.
|