Skip to content

Commit f93c802

Browse files
ethanWallacedaine
andauthored
tests(angular): Add file-uploader and error-summary tests e2e tests (#1007)
* test: Add file-uploader and error-summary tests to angular * Update packages/angular/tests/e2e/file-uploader.spec.ts Co-authored-by: Daine Trinidad <[email protected]> * Update packages/angular/tests/app/src/app/components/fileuploader/file-uploader.component.ts Co-authored-by: Daine Trinidad <[email protected]> --------- Co-authored-by: Daine Trinidad <[email protected]>
1 parent 96ddb49 commit f93c802

File tree

6 files changed

+190
-4
lines changed

6 files changed

+190
-4
lines changed

packages/angular/tests/app/src/app/app.routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { Routes } from '@angular/router';
22
import { HomeComponent } from './components/home/home.components';
33
import { FormsComponent } from './components/forms/forms.component';
44
import { NavComponent } from './components/navigation/navigation.components';
5+
import { FileUploaderComponent } from './components/fileuploader/file-uploader.component';
56

67
export const routes: Routes = [
78
{ path: '', redirectTo: '/home', pathMatch: 'full' },
89
{ path: 'home', component: HomeComponent },
910
{ path: 'forms', component: FormsComponent },
11+
{ path: 'file-uploader', component: FileUploaderComponent },
1012
{
1113
path: 'nav-one',
1214
component: NavComponent,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Component } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
import { GcdsComponentsModule } from '@cdssnc/gcds-components-angular';
4+
5+
@Component({
6+
selector: 'app-fileuploader',
7+
standalone: true,
8+
imports: [CommonModule, GcdsComponentsModule],
9+
template: `
10+
<gcds-breadcrumbs hide-canada-link>
11+
<gcds-breadcrumbs-item href="/home" routerLink="/home">
12+
Home
13+
</gcds-breadcrumbs-item>
14+
</gcds-breadcrumbs>
15+
16+
<gcds-heading tag="h1">File Uploader Test</gcds-heading>
17+
18+
<form novalidate>
19+
<section class="b-solid p-300 mb-300" id="file-uploader">
20+
<gcds-heading tag="h2" margin-top="0">Gcds-file-uploader</gcds-heading>
21+
<p>
22+
Testing whether the file-uploader still functions properly in Angular.
23+
</p>
24+
25+
<gcds-file-uploader
26+
name="file-uploader"
27+
label="Upload your files"
28+
hint="You can upload multiple files."
29+
[multiple]="true"
30+
(change)="handleChange($event)"
31+
></gcds-file-uploader>
32+
33+
<p>You entered: <span id="files"></span></p>
34+
</section>
35+
</form>
36+
`,
37+
})
38+
export class FileUploaderComponent {
39+
handleChange(event: any) {
40+
const inputFiles = event.target.files;
41+
const filesElement = document.getElementById('files');
42+
if (filesElement) {
43+
filesElement.innerText = Array.from(inputFiles)
44+
.map(file => (file as File).name)
45+
.join(', ');
46+
}
47+
}
48+
}

packages/angular/tests/app/src/app/components/forms/forms.component.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { FormsModule } from '@angular/forms';
1717
<gcds-heading tag="h1">Form components</gcds-heading>
1818
1919
<form>
20+
<gcds-error-summary></gcds-error-summary>
2021
<section class="b-solid p-300 mb-300" id="input">
2122
<gcds-heading tag="h2" margin-top="0">Gcds-input</gcds-heading>
2223
<p>Text input and number input</p>
@@ -27,6 +28,8 @@ import { FormsModule } from '@angular/forms';
2728
name="name"
2829
label="Full name"
2930
[(ngModel)]="name"
31+
validate-on="submit"
32+
[required]="true"
3033
></gcds-input>
3134
3235
<gcds-input
@@ -35,6 +38,8 @@ import { FormsModule } from '@angular/forms';
3538
name="number"
3639
label="Number"
3740
[(ngModel)]="number"
41+
validate-on="submit"
42+
[required]="true"
3843
></gcds-input>
3944
4045
<p>
@@ -53,6 +58,8 @@ import { FormsModule } from '@angular/forms';
5358
textarea-id="message"
5459
name="message"
5560
[(ngModel)]="message"
61+
validate-on="submit"
62+
[required]="true"
5663
></gcds-textarea>
5764
5865
<p>
@@ -69,13 +76,17 @@ import { FormsModule } from '@angular/forms';
6976
name="date-full"
7077
format="full"
7178
[(ngModel)]="dateFull"
79+
validate-on="submit"
80+
[required]="true"
7281
></gcds-date-input>
7382
7483
<gcds-date-input
7584
legend="Compact date"
7685
name="date-compact"
7786
format="compact"
7887
[(ngModel)]="dateCompact"
88+
validate-on="submit"
89+
[required]="true"
7990
></gcds-date-input>
8091
8192
<div>
@@ -97,6 +108,8 @@ import { FormsModule } from '@angular/forms';
97108
name="select"
98109
default-value="Select an option"
99110
[(ngModel)]="select"
111+
validate-on="submit"
112+
[required]="true"
100113
>
101114
<option value="1">1</option>
102115
<option value="2">2</option>
@@ -116,11 +129,13 @@ import { FormsModule } from '@angular/forms';
116129
name="radio"
117130
legend="Radios"
118131
options='[
119-
{ "label": "Label for radio 1", "id": "radio1", "value": "radio1"},
120-
{ "label": "Label for radio 2", "id": "radio2", "value": "radio2"},
121-
{ "label": "Label for radio 3", "id": "radio3", "value": "radio3"}
122-
]'
132+
{ "label": "Label for radio 1", "id": "radio1", "value": "radio1"},
133+
{ "label": "Label for radio 2", "id": "radio2", "value": "radio2"},
134+
{ "label": "Label for radio 3", "id": "radio3", "value": "radio3"}
135+
]'
123136
[(ngModel)]="radio"
137+
validate-on="submit"
138+
[required]="true"
124139
></gcds-radios>
125140
126141
<p>
@@ -140,13 +155,18 @@ import { FormsModule } from '@angular/forms';
140155
{ "label": "Label for checkbox 3", "id": "check3", "value": "check3"}
141156
]'
142157
[(ngModel)]="check"
158+
validate-on="submit"
159+
[required]="true"
143160
></gcds-checkboxes>
144161
145162
<p>
146163
You entered: <span id="input-check">{{ check }}</span>
147164
</p>
148165
</section>
149166
167+
<gcds-button type="submit" button-role="primary" id="submit">
168+
Submit
169+
</gcds-button>
150170
<gcds-button
151171
type="button"
152172
button-role="primary"

packages/angular/tests/app/src/app/components/home/home.components.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ import { GcdsComponentsModule } from '@cdssnc/gcds-components-angular';
2222
Navigation components
2323
</gcds-link>
2424
</li>
25+
<li>
26+
<p>Extra tests:</p>
27+
<ul class="list-disc">
28+
<li>
29+
<gcds-link href="/file-uploader" routerLink="/file-uploader">
30+
File uploader
31+
</gcds-link>
32+
</li>
33+
</ul>
34+
</li>
2535
</ul>
2636
`,
2737
})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { test, expect } from '@playwright/test';
2+
3+
test('File uploader loads and functions', async ({ page }) => {
4+
await page.goto('http://localhost:4200/file-uploader');
5+
6+
const fileInput = await page.locator('input[type="file"]');
7+
await expect(fileInput).toBeVisible();
8+
9+
// Simulate single file upload
10+
const filePath = 'tests/e2e/file-uploader.spec.ts';
11+
await fileInput.setInputFiles(filePath);
12+
13+
await expect(await page.locator('#files')).toHaveText(
14+
'file-uploader.spec.ts',
15+
);
16+
17+
// Remove file
18+
await page.locator('.file-uploader__uploaded-file button').click();
19+
await expect(await page.locator('#files')).toHaveText('');
20+
21+
// Simulate mutliple file upload
22+
const multipleFilePath = [
23+
'tests/e2e/file-uploader.spec.ts',
24+
'tests/e2e/app.spec.ts',
25+
];
26+
await fileInput.setInputFiles(multipleFilePath);
27+
28+
await expect(await page.locator('#files')).toHaveText(
29+
'file-uploader.spec.ts, app.spec.ts',
30+
);
31+
32+
// Remove files
33+
await page.locator('.file-uploader__uploaded-file button').first().click();
34+
await page.locator('.file-uploader__uploaded-file button').first().click();
35+
await expect(await page.locator('#files')).toHaveText('');
36+
});

packages/angular/tests/e2e/ngmodel.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,73 @@ test('Form component ngModel - setting programatically', async ({ page }) => {
159159
await expect(page.locator('#input-radios')).toHaveText('');
160160
await expect(page.locator('#input-check')).toHaveText('');
161161
});
162+
163+
test(`Error summary`, async ({ page }) => {
164+
await page.goto('http://localhost:4200/forms');
165+
166+
expect(await page.locator('gcds-error-summary a').count()).toBe(0);
167+
168+
await expect(page.locator('#input-name')).toHaveText('');
169+
await expect(page.locator('#input-message')).toHaveText('');
170+
await expect(page.locator('#input-date-full')).toHaveText('');
171+
await expect(page.locator('#input-date-compact')).toHaveText('');
172+
await expect(page.locator('#input-select')).toHaveText('');
173+
await expect(page.locator('#input-radios')).toHaveText('');
174+
await expect(page.locator('#input-check')).toHaveText('');
175+
176+
await page.locator('button[type="submit"]').click();
177+
178+
await page.waitForTimeout(500); // Wait for error summary to render
179+
180+
let errorLinks = await page.locator('gcds-error-summary a').all();
181+
182+
expect(errorLinks.length).toBe(7);
183+
184+
await errorLinks[0].click();
185+
await expect(page.locator('input[name="name"]')).toBeFocused();
186+
await page.locator('input[name="name"]').fill('John');
187+
188+
await errorLinks[1].click();
189+
await expect(page.locator('textarea[name="message"]')).toBeFocused();
190+
await page.locator('textarea[name="message"]').fill('This is a message.');
191+
192+
await errorLinks[2].click();
193+
const dateFull = page.locator('gcds-date-input[name="date-full"]');
194+
195+
await expect(
196+
await dateFull.evaluate(el => el === document.activeElement),
197+
).toBe(true);
198+
await dateFull.evaluate(el => el.setAttribute('value', '2024-12-31'));
199+
200+
await errorLinks[3].click();
201+
const dateCompact = page.locator('gcds-date-input[name="date-compact"]');
202+
203+
await expect(
204+
await dateCompact.evaluate(el => el === document.activeElement),
205+
).toBe(true);
206+
await dateCompact.evaluate(el => el.setAttribute('value', '2024-12'));
207+
208+
await errorLinks[4].click();
209+
await expect(page.locator('gcds-select select[name="select"]')).toBeFocused();
210+
await page.locator('gcds-select select[name="select"]').selectOption('1');
211+
212+
await errorLinks[5].click();
213+
await expect(page.locator('gcds-radios fieldset')).toBeFocused();
214+
await page
215+
.locator('gcds-radios input[value="radio1"]')
216+
.check({ force: true });
217+
218+
await errorLinks[6].click();
219+
await expect(page.locator('gcds-checkboxes fieldset')).toBeFocused();
220+
await page
221+
.locator('gcds-checkboxes input[value="check1"]')
222+
.check({ force: true });
223+
224+
await page.locator('button[type="submit"]').click();
225+
226+
await page.waitForTimeout(500); // Wait for error summary to render
227+
228+
errorLinks = await page.locator('gcds-error-summary a').all();
229+
230+
expect(errorLinks.length).toBe(0);
231+
});

0 commit comments

Comments
 (0)