Skip to content

Commit d36c2ea

Browse files
authored
docs: update examples to standalone components (#400)
Closes #399
1 parent 72a4d3c commit d36c2ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+187
-931
lines changed

apps/example-app-karma/project.json

-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"outputPath": "dist/apps/example-app-karma",
1414
"index": "apps/example-app-karma/src/index.html",
1515
"main": "apps/example-app-karma/src/main.ts",
16-
"polyfills": "apps/example-app-karma/src/polyfills.ts",
1716
"tsConfig": "apps/example-app-karma/tsconfig.app.json",
1817
"assets": ["apps/example-app-karma/src/favicon.ico", "apps/example-app-karma/src/assets"],
1918
"styles": [],
@@ -27,12 +26,6 @@
2726
"maximumWarning": "6kb"
2827
}
2928
],
30-
"fileReplacements": [
31-
{
32-
"replace": "apps/example-app-karma/src/environments/environment.ts",
33-
"with": "apps/example-app-karma/src/environments/environment.prod.ts"
34-
}
35-
],
3629
"outputHashing": "all"
3730
},
3831
"development": {
@@ -74,7 +67,6 @@
7467
"options": {
7568
"main": "apps/example-app-karma/src/test.ts",
7669
"tsConfig": "apps/example-app-karma/tsconfig.spec.json",
77-
"polyfills": "apps/example-app-karma/src/polyfills.ts",
7870
"karmaConfig": "apps/example-app-karma/karma.conf.js"
7971
}
8072
}

apps/example-app-karma/src/app/app.module.ts

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Component } from '@angular/core';
2+
import { FormBuilder, FormControl, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
3+
import userEvent from '@testing-library/user-event';
4+
import { render, screen } from '@testing-library/angular';
5+
import { NgIf } from '@angular/common';
6+
7+
it('should create a component with inputs and a button to submit', async () => {
8+
await render(LoginComponent);
9+
10+
expect(screen.getByRole('textbox', { name: 'email' })).toBeInTheDocument();
11+
expect(screen.getByLabelText('password')).toBeInTheDocument();
12+
expect(screen.getByRole('button', { name: 'submit' })).toBeInTheDocument();
13+
});
14+
15+
it('should display invalid message and submit button must be disabled', async () => {
16+
const user = userEvent.setup();
17+
18+
await render(LoginComponent);
19+
20+
const email = screen.getByRole('textbox', { name: 'email' });
21+
const password = screen.getByLabelText('password');
22+
23+
await user.type(email, 'foo');
24+
await user.type(password, 's');
25+
26+
expect(screen.getAllByText(/is invalid/i).length).toBe(2);
27+
expect(screen.getAllByRole('alert').length).toBe(2);
28+
expect(screen.getByRole('button', { name: 'submit' })).toBeDisabled();
29+
});
30+
31+
@Component({
32+
selector: 'app-login',
33+
standalone: true,
34+
imports: [ReactiveFormsModule, NgIf],
35+
template: `
36+
<h1>Login</h1>
37+
38+
<form [formGroup]="form" (submit)="onSubmit(form)">
39+
<input type="email" aria-label="email" formControlName="email" />
40+
<div *ngIf="email.invalid && (email.dirty || email.touched)" role="alert">Email is invalid</div>
41+
<input type="password" aria-label="password" formControlName="password" />
42+
<div *ngIf="password.invalid && (password.dirty || password.touched)" role="alert">Password is invalid</div>
43+
<button type="submit" aria-label="submit" [disabled]="form.invalid">Submit</button>
44+
</form>
45+
`,
46+
})
47+
class LoginComponent {
48+
form: FormGroup = this.fb.group({
49+
email: ['', [Validators.required, Validators.email]],
50+
password: ['', [Validators.required, Validators.minLength(8)]],
51+
});
52+
53+
constructor(private fb: FormBuilder) {}
54+
55+
blurred() {
56+
console.log('aaaaaaaaac');
57+
console.log('aaaaaaaaac');
58+
console.log('aaaaaaaaac');
59+
console.log('aaaaaaaaac');
60+
console.log('aaaaaaaaac');
61+
console.log('aaaaaaaaac');
62+
console.log('aaaaaaaaac');
63+
}
64+
get email(): FormControl {
65+
return this.form.get('email') as FormControl;
66+
}
67+
68+
get password(): FormControl {
69+
return this.form.get('password') as FormControl;
70+
}
71+
72+
onSubmit(_fg: FormGroup): void {
73+
// do nothing
74+
}
75+
}

apps/example-app-karma/src/app/issues/issue-373.spec.ts

-69
This file was deleted.

apps/example-app-karma/src/app/issues/issue-222.spec.ts renamed to apps/example-app-karma/src/app/issues/rerender.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { render, screen } from '@testing-library/angular';
22

3-
it('https://github.com/testing-library/angular-testing-library/issues/222 with rerender', async () => {
3+
it('can rerender component', async () => {
44
const { rerender } = await render(`<div>Hello {{ name}}</div>`, {
55
componentProperties: {
66
name: 'Sarah',

apps/example-app-karma/src/assets/.gitkeep

Whitespace-only changes.

apps/example-app-karma/src/environments/environment.prod.ts

-3
This file was deleted.

apps/example-app-karma/src/environments/environment.ts

-15
This file was deleted.
-5.3 KB
Binary file not shown.

apps/example-app-karma/src/index.html

-14
This file was deleted.

apps/example-app-karma/src/main.ts

-13
This file was deleted.

apps/example-app-karma/src/polyfills.ts

-65
This file was deleted.

apps/example-app-karma/src/test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
1+
import 'zone.js';
22
import 'zone.js/dist/zone-testing';
33
import { getTestBed } from '@angular/core/testing';
44
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

apps/example-app-karma/tsconfig.app.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"target": "ES2022",
88
"useDefineForClassFields": false
99
},
10-
"files": ["src/main.ts", "src/polyfills.ts"],
10+
"files": ["src/main.ts"],
1111
"include": ["src/**/*.d.ts"],
1212
"exclude": ["**/*.test.ts", "**/*.spec.ts"]
1313
}

apps/example-app-karma/tsconfig.spec.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"target": "ES2022",
77
"useDefineForClassFields": false
88
},
9-
"files": ["src/test.ts", "src/polyfills.ts"],
9+
"files": ["src/test.ts"],
1010
"include": ["**/*.spec.ts", "**/*.d.ts"]
1111
}

apps/example-app/project.json

-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
"maximumWarning": "6kb"
2828
}
2929
],
30-
"fileReplacements": [
31-
{
32-
"replace": "apps/example-app/src/environments/environment.ts",
33-
"with": "apps/example-app/src/environments/environment.prod.ts"
34-
}
35-
],
3630
"outputHashing": "all"
3731
},
3832
"development": {

0 commit comments

Comments
 (0)