Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b2b7a33

Browse files
committedJun 18, 2018
feat: remove getComponentInstance
1 parent eb0ca98 commit b2b7a33

File tree

7 files changed

+22
-122
lines changed

7 files changed

+22
-122
lines changed
 

‎README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,15 @@ The second parameter in `createComponent` is the `options` parameter, which look
7676
}
7777
```
7878

79-
`detectChanges`: runs `detectChanges` on the fixture<br/>
80-
`declarations`: passed to the `TestBed`<br/>
81-
`providers`: passed to the `TestBed`<br/>
82-
`imports`: passed to the `TestBed`<br/>
83-
`schemas`: passed to the `TestBed`<br/>
79+
`detectChanges`: runs `detectChanges` on the fixture
80+
81+
`declarations`: passed to the `TestBed`
82+
83+
`providers`: passed to the `TestBed`
84+
85+
`imports`: passed to the `TestBed`
86+
87+
`schemas`: passed to the `TestBed`
8488

8589
The `createComponent` function returns an object consisting all of the query functions from [dom-testing-library][dom-testing-library] and adds the following properties:
8690

@@ -106,12 +110,6 @@ The Angular fixture.
106110

107111
Calls the the Angular `TestBed.get` function.
108112

109-
#### `getComponentInstance(selector?: string) => T`
110-
111-
Gets the Angular component instance.
112-
113-
The `selector` is required when the template syntax is being used, in order to get the component.
114-
115113
### `fireEvent`
116114

117115
Exposes the `fireEvent` from [dom-testing-library](dom-testing-library).

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ComponentFixture } from '@angular/core/testing';
44
export interface Result<T> {
55
container: HTMLElement;
66
getFromTestBed: (token: any, notFoundValue?: any) => any;
7-
getComponentInstance: <C = T>(selector?: string) => C;
87
debug: () => void;
98
detectChanges: (checkNoChanges?: boolean) => void;
109
fixture: ComponentFixture<any>;

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ export async function createComponent<T>(
3939
fixture,
4040
container: fixture.nativeElement,
4141
getFromTestBed: TestBed.get,
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-
},
4842
detectChanges: (checkNoChanges?: boolean) => fixture.detectChanges(checkNoChanges),
4943
debug: () => console.log(prettyDOM(fixture.nativeElement)),
5044
...getQueriesForElement(fixture.nativeElement),

‎projects/ngx-testing-library/tests/__snapshots__/getComponentInstance.spec.ts.snap

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎projects/ngx-testing-library/tests/counter/counter.spec.ts

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test('Counter actions via template syntax', async () => {
3636
});
3737

3838
test('Counter actions via component syntax', async () => {
39-
const { getComponentInstance } = await createComponent(
39+
const { getByText, detectChanges, getByTestId } = await createComponent(
4040
{
4141
component: CounterComponent,
4242
parameters: {
@@ -48,39 +48,21 @@ test('Counter actions via component syntax', async () => {
4848
},
4949
);
5050

51-
const counter = getComponentInstance();
52-
counter.increment();
53-
expect(counter.counter).toBe(11);
51+
getByText('+').click();
52+
detectChanges();
53+
expect(getByText('Current Count: 11')).toBeTruthy();
54+
expect(getByTestId('count').textContent).toBe('Current Count: 11');
5455

55-
counter.decrement();
56-
expect(counter.counter).toBe(10);
56+
getByText('-').click();
57+
detectChanges();
58+
expect(getByText('Current Count: 10')).toBeTruthy();
59+
expect(getByTestId('count').textContent).toBe('Current Count: 10');
5760
});
5861

5962
test('Counter actions via component syntax without parameters', async () => {
60-
const { getComponentInstance } = await createComponent(
61-
{
62-
component: CounterComponent,
63-
},
64-
{
65-
declarations: [CounterComponent],
66-
},
67-
);
68-
69-
const counter = getComponentInstance();
70-
counter.increment();
71-
expect(counter.counter).toBe(1);
72-
73-
counter.decrement();
74-
expect(counter.counter).toBe(0);
75-
});
76-
77-
test('Counter actions via component syntax and dom-testing-library functions', async () => {
7863
const { getByText, detectChanges, getByTestId } = await createComponent(
7964
{
8065
component: CounterComponent,
81-
parameters: {
82-
counter: 10,
83-
},
8466
},
8567
{
8668
declarations: [CounterComponent],
@@ -89,11 +71,11 @@ test('Counter actions via component syntax and dom-testing-library functions', a
8971

9072
getByText('+').click();
9173
detectChanges();
92-
expect(getByText('Current Count: 11')).toBeTruthy();
93-
expect(getByTestId('count').textContent).toBe('Current Count: 11');
74+
expect(getByText('Current Count: 1')).toBeTruthy();
75+
expect(getByTestId('count').textContent).toBe('Current Count: 1');
9476

9577
getByText('-').click();
9678
detectChanges();
97-
expect(getByText('Current Count: 10')).toBeTruthy();
98-
expect(getByTestId('count').textContent).toBe('Current Count: 10');
79+
expect(getByText('Current Count: 0')).toBeTruthy();
80+
expect(getByTestId('count').textContent).toBe('Current Count: 0');
9981
});

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,6 @@ import { ReactiveFormsModule } from '@angular/forms';
22
import { createComponent, fireEvent } from '../../src/public_api';
33
import { LoginFormComponent } from './form.component';
44

5-
test('login form submits using the template syntax', async () => {
6-
const fakeUser = { username: 'jackiechan', password: 'hiya! 🥋' };
7-
const { getComponentInstance, getByLabelText, getByText, container } = await createComponent(
8-
`<login-form></login-form>`,
9-
{
10-
declarations: [LoginFormComponent],
11-
imports: [ReactiveFormsModule],
12-
},
13-
);
14-
15-
const loginForm = getComponentInstance<LoginFormComponent>('login-form');
16-
loginForm.handleLogin.emit = jest.fn();
17-
18-
const usernameNode = getByLabelText(/username/i) as HTMLInputElement;
19-
const passwordNode = getByLabelText(/password/i) as HTMLInputElement;
20-
const submitButtonNode = getByText(/submit/i);
21-
const formNode = container.querySelector('form');
22-
23-
usernameNode.value = fakeUser.username;
24-
fireEvent.input(usernameNode);
25-
26-
passwordNode.value = fakeUser.password;
27-
fireEvent.input(passwordNode);
28-
29-
fireEvent.submit(formNode);
30-
31-
expect(loginForm.handleLogin.emit).toHaveBeenCalledTimes(1);
32-
expect(loginForm.handleLogin.emit).toHaveBeenCalledWith(fakeUser);
33-
expect(submitButtonNode.type).toBe('submit');
34-
});
35-
365
test('login form submits using the component syntax', async () => {
376
const fakeUser = { username: 'jackiechan', password: 'hiya! 🥋' };
387
const handleLogin = {

‎projects/ngx-testing-library/tests/getComponentInstance.spec.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.