Skip to content

Commit 1688830

Browse files
committed
test: restructure the tests
1 parent e4636c6 commit 1688830

File tree

6 files changed

+75
-74
lines changed

6 files changed

+75
-74
lines changed

projects/ngx-testing-library/src/lib/ngx-testing-library.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ export async function createComponent<T>(
3939
fixture,
4040
container: fixture.nativeElement,
4141
get: TestBed.get,
42-
getComponentInstance: <C = T>(selector?: string) =>
43-
selector ? fixture.debugElement.query(By.css(selector)).componentInstance : fixture.componentInstance,
42+
getComponentInstance: <C = T>(selector?: string) => {
43+
if (isTemplate && !selector) {
44+
throw new Error('When using the template syntax, you must provide a selector');
45+
}
46+
return selector ? fixture.debugElement.query(By.css(selector)).componentInstance : fixture.componentInstance;
47+
},
4448
detectChanges: (checkNoChanges?: boolean) => fixture.detectChanges(checkNoChanges),
4549
debug: () => console.log(prettyDOM(fixture.nativeElement)),
4650
...getQueriesForElement(fixture.nativeElement),

projects/ngx-testing-library/tests/counter.spec.ts renamed to projects/ngx-testing-library/tests/counter/counter.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, Input } from '@angular/core';
2-
import { createComponent, fireEvent } from '../src/public_api';
2+
import { createComponent, fireEvent } from '../../src/public_api';
33

44
@Component({
55
selector: 'counter',

projects/ngx-testing-library/tests/form.spec.ts

-71
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div>
2+
<form [formGroup]="loginForm" (ngSubmit)="handleSubmit()">
3+
<label for="username-input">Username</label>
4+
<input id="username-input" placeholder="Username..." name="username" formControlName="username" />
5+
<label id="password-label">Password</label>
6+
<input placeholder="Password..." type="password" name="password" aria-labelledby="password-label" formControlName="password"
7+
/>
8+
<button type="submit">Submit</button>
9+
</form>
10+
<div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Component, Input, Output, EventEmitter } from '@angular/core';
2+
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
3+
4+
@Component({
5+
selector: 'login-form',
6+
templateUrl: './form.component.html',
7+
})
8+
export class LoginFormComponent {
9+
@Output() handleLogin = new EventEmitter<{ username: string; password: string }>();
10+
11+
loginForm: FormGroup;
12+
constructor(private fb: FormBuilder) {
13+
this.createForm();
14+
}
15+
16+
createForm() {
17+
this.loginForm = this.fb.group({
18+
username: ['', Validators.required],
19+
password: ['', Validators.required],
20+
});
21+
}
22+
23+
handleSubmit() {
24+
if (this.loginForm.valid) {
25+
this.handleLogin.emit(this.loginForm.value);
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { ReactiveFormsModule } from '@angular/forms';
2+
import { LoginFormComponent } from './form.component';
3+
import { createComponent, fireEvent } from '../../src/public_api';
4+
5+
test('login form submits', async () => {
6+
const fakeUser = { username: 'jackiechan', password: 'hiya! 🥋' };
7+
const { getComponentInstance, getByLabelText, getByText } = await createComponent(`<login-form></login-form>`, {
8+
declarations: [LoginFormComponent],
9+
imports: [ReactiveFormsModule],
10+
});
11+
12+
const loginForm = getComponentInstance<LoginFormComponent>('login-form');
13+
loginForm.handleLogin.emit = jest.fn();
14+
15+
const usernameNode = getByLabelText(/username/i) as HTMLInputElement;
16+
const passwordNode = getByLabelText(/password/i) as HTMLInputElement;
17+
const submitButtonNode = getByText(/submit/i);
18+
19+
usernameNode.value = fakeUser.username;
20+
fireEvent.input(usernameNode);
21+
22+
passwordNode.value = fakeUser.password;
23+
fireEvent.input(passwordNode);
24+
25+
loginForm.handleSubmit();
26+
27+
expect(loginForm.handleLogin.emit).toHaveBeenCalledTimes(1);
28+
expect(loginForm.handleLogin.emit).toHaveBeenCalledWith(fakeUser);
29+
expect(submitButtonNode.type).toBe('submit');
30+
});

0 commit comments

Comments
 (0)