@api-client/ui 0.5.0 → 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/demo/md/dialog/dialog.ts +135 -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/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
package/demo/md/dialog/dialog.ts
CHANGED
|
@@ -18,6 +18,8 @@ class ComponentDemoPage extends DemoPage {
|
|
|
18
18
|
|
|
19
19
|
@reactive() accessor overflowOpened = false
|
|
20
20
|
|
|
21
|
+
@reactive() accessor formOpened = false
|
|
22
|
+
|
|
21
23
|
protected openSimple(): void {
|
|
22
24
|
this.simpleOpened = true
|
|
23
25
|
}
|
|
@@ -45,6 +47,15 @@ class ComponentDemoPage extends DemoPage {
|
|
|
45
47
|
this.reportClosingReason(e.detail)
|
|
46
48
|
}
|
|
47
49
|
|
|
50
|
+
protected openForm(): void {
|
|
51
|
+
this.formOpened = true
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected formClosed(e: CustomEvent<UiDialogClosingReason>): void {
|
|
55
|
+
this.formOpened = false
|
|
56
|
+
this.reportClosingReason(e.detail)
|
|
57
|
+
}
|
|
58
|
+
|
|
48
59
|
imperativeDialog: UiDialog | null = null
|
|
49
60
|
|
|
50
61
|
protected openImperative(): void {
|
|
@@ -81,7 +92,8 @@ class ComponentDemoPage extends DemoPage {
|
|
|
81
92
|
contentTemplate(): TemplateResult {
|
|
82
93
|
return html`
|
|
83
94
|
<a href="../">Back</a>
|
|
84
|
-
${this.simpleDialog()} ${this.fullDialog()} ${this.overflowDialog()} ${this.
|
|
95
|
+
${this.simpleDialog()} ${this.fullDialog()} ${this.overflowDialog()} ${this.formDialog()}
|
|
96
|
+
${this.renderImperativeDialog()}
|
|
85
97
|
`
|
|
86
98
|
}
|
|
87
99
|
|
|
@@ -244,6 +256,128 @@ class ComponentDemoPage extends DemoPage {
|
|
|
244
256
|
`
|
|
245
257
|
}
|
|
246
258
|
|
|
259
|
+
formDialog(): TemplateResult {
|
|
260
|
+
return html`
|
|
261
|
+
<section class="demo-section">
|
|
262
|
+
<h2 class="title-large">Form dialog</h2>
|
|
263
|
+
<p>This dialog contains a form with validation and submit handling.</p>
|
|
264
|
+
<ui-button color="filled" @click="${this.openForm}">Open Form Dialog</ui-button>
|
|
265
|
+
<form @submit="${this.handleFormSubmit}" style="display: contents;">
|
|
266
|
+
<ui-dialog
|
|
267
|
+
?open="${this.formOpened}"
|
|
268
|
+
@close="${this.formClosed}"
|
|
269
|
+
modal
|
|
270
|
+
style="--ui-dialog-max-width: 500px;"
|
|
271
|
+
submitClose
|
|
272
|
+
>
|
|
273
|
+
<ui-icon slot="icon" icon="info"></ui-icon>
|
|
274
|
+
<span slot="title">User Registration</span>
|
|
275
|
+
|
|
276
|
+
<div style="display: flex; flex-direction: column; gap: 16px;">
|
|
277
|
+
<label style="display: flex; flex-direction: column; gap: 4px;">
|
|
278
|
+
<span style="font-weight: 500; color: var(--md-sys-color-on-surface);">Full Name *</span>
|
|
279
|
+
<input
|
|
280
|
+
type="text"
|
|
281
|
+
name="fullName"
|
|
282
|
+
required
|
|
283
|
+
style="padding: 12px; border: 1px solid var(--md-sys-color-outline); border-radius: 4px; font-size: 14px;"
|
|
284
|
+
placeholder="Enter your full name"
|
|
285
|
+
/>
|
|
286
|
+
</label>
|
|
287
|
+
|
|
288
|
+
<label style="display: flex; flex-direction: column; gap: 4px;">
|
|
289
|
+
<span style="font-weight: 500; color: var(--md-sys-color-on-surface);">Email *</span>
|
|
290
|
+
<input
|
|
291
|
+
type="email"
|
|
292
|
+
name="email"
|
|
293
|
+
required
|
|
294
|
+
style="padding: 12px; border: 1px solid var(--md-sys-color-outline); border-radius: 4px; font-size: 14px;"
|
|
295
|
+
placeholder="Enter your email address"
|
|
296
|
+
/>
|
|
297
|
+
</label>
|
|
298
|
+
|
|
299
|
+
<label style="display: flex; flex-direction: column; gap: 4px;">
|
|
300
|
+
<span style="font-weight: 500; color: var(--md-sys-color-on-surface);">Phone Number</span>
|
|
301
|
+
<input
|
|
302
|
+
type="tel"
|
|
303
|
+
name="phone"
|
|
304
|
+
style="padding: 12px; border: 1px solid var(--md-sys-color-outline); border-radius: 4px; font-size: 14px;"
|
|
305
|
+
placeholder="Enter your phone number (optional)"
|
|
306
|
+
/>
|
|
307
|
+
</label>
|
|
308
|
+
|
|
309
|
+
<label style="display: flex; flex-direction: column; gap: 4px;">
|
|
310
|
+
<span style="font-weight: 500; color: var(--md-sys-color-on-surface);">Department</span>
|
|
311
|
+
<select
|
|
312
|
+
name="department"
|
|
313
|
+
style="padding: 12px; border: 1px solid var(--md-sys-color-outline); border-radius: 4px; font-size: 14px; background: white;"
|
|
314
|
+
>
|
|
315
|
+
<option value="">Select a department</option>
|
|
316
|
+
<option value="engineering">Engineering</option>
|
|
317
|
+
<option value="design">Design</option>
|
|
318
|
+
<option value="marketing">Marketing</option>
|
|
319
|
+
<option value="sales">Sales</option>
|
|
320
|
+
<option value="support">Support</option>
|
|
321
|
+
</select>
|
|
322
|
+
</label>
|
|
323
|
+
|
|
324
|
+
<label style="display: flex; align-items: center; gap: 8px; margin-top: 8px;">
|
|
325
|
+
<input type="checkbox" name="newsletter" style="margin: 0;" />
|
|
326
|
+
<span style="font-size: 14px; color: var(--md-sys-color-on-surface);">
|
|
327
|
+
Subscribe to our newsletter
|
|
328
|
+
</span>
|
|
329
|
+
</label>
|
|
330
|
+
|
|
331
|
+
<label style="display: flex; align-items: center; gap: 8px;">
|
|
332
|
+
<input type="checkbox" name="terms" required style="margin: 0;" />
|
|
333
|
+
<span style="font-size: 14px; color: var(--md-sys-color-on-surface);">
|
|
334
|
+
I agree to the <a href="#" style="color: var(--md-sys-color-primary);">Terms and Conditions</a> *
|
|
335
|
+
</span>
|
|
336
|
+
</label>
|
|
337
|
+
</div>
|
|
338
|
+
|
|
339
|
+
<ui-button color="text" slot="button" value="dismiss">Cancel</ui-button>
|
|
340
|
+
<ui-button color="filled" slot="button" value="confirm" type="submit">Register</ui-button>
|
|
341
|
+
</ui-dialog>
|
|
342
|
+
</form>
|
|
343
|
+
</section>
|
|
344
|
+
`
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
protected handleFormSubmit(e: CustomEvent): void {
|
|
348
|
+
e.preventDefault()
|
|
349
|
+
|
|
350
|
+
// Get form data from the event
|
|
351
|
+
const form = e.target as HTMLFormElement
|
|
352
|
+
const formData = new FormData(form)
|
|
353
|
+
|
|
354
|
+
// Convert to regular object for easier handling
|
|
355
|
+
const data: Record<string, string | boolean> = {}
|
|
356
|
+
for (const [key, value] of formData.entries()) {
|
|
357
|
+
if (key === 'newsletter' || key === 'terms') {
|
|
358
|
+
data[key] = value === 'on'
|
|
359
|
+
} else {
|
|
360
|
+
data[key] = value as string
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
console.log('Form submitted with data:', data)
|
|
365
|
+
|
|
366
|
+
// Simulate form validation
|
|
367
|
+
if (!data.fullName || !data.email) {
|
|
368
|
+
console.error('Required fields are missing')
|
|
369
|
+
return
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (!data.terms) {
|
|
373
|
+
console.error('Terms and conditions must be accepted')
|
|
374
|
+
return
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// If validation passes, the dialog will close automatically
|
|
378
|
+
console.log('Registration successful!', data)
|
|
379
|
+
}
|
|
380
|
+
|
|
247
381
|
renderImperativeDialog(): TemplateResult {
|
|
248
382
|
return html`
|
|
249
383
|
<section class="demo-section">
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@api-client/ui",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Internal UI component library for the API Client ecosystem.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"main": "build/src/index.js",
|
|
@@ -182,7 +182,7 @@
|
|
|
182
182
|
"dompurify": "^3.2.5",
|
|
183
183
|
"idb-keyval": "^6.1.0",
|
|
184
184
|
"lit": "^3.2.1",
|
|
185
|
-
"marked": "^
|
|
185
|
+
"marked": "^16.0.0",
|
|
186
186
|
"monaco-editor": "^0.52.2",
|
|
187
187
|
"nanoid": "^5.1.5",
|
|
188
188
|
"prismjs": "^1.28.0",
|
|
@@ -247,7 +247,8 @@ export default class MarkedHighlight extends LitElement {
|
|
|
247
247
|
pedantic: this.pedantic,
|
|
248
248
|
gfm: true,
|
|
249
249
|
}
|
|
250
|
-
|
|
250
|
+
// eslint-disable-next-line no-misleading-character-class
|
|
251
|
+
let out = marked(data.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/, ''), opts) as string
|
|
251
252
|
if (this.sanitize) {
|
|
252
253
|
if (this.sanitizer) {
|
|
253
254
|
out = this.sanitizer(out)
|
|
@@ -240,7 +240,7 @@ export default class BaseButton extends UiElement {
|
|
|
240
240
|
}
|
|
241
241
|
|
|
242
242
|
override handleClick(e: MouseEvent): void {
|
|
243
|
-
super.handleClick(
|
|
243
|
+
// Do not call super.handleClick() here, as it would call `endPress` again.
|
|
244
244
|
if (this.disabled) {
|
|
245
245
|
e.preventDefault()
|
|
246
246
|
e.stopPropagation()
|
|
@@ -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
|
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.
|