Skip to content

Commit e4636c6

Browse files
committed
refactor: getComponentInstance
1 parent 1428652 commit e4636c6

File tree

5 files changed

+17
-23
lines changed

5 files changed

+17
-23
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Type } from '@angular/core';
22

3-
export interface Result {
3+
export interface Result<T> {
44
container: any;
55
get: (token: any, notFoundValue?: any) => any;
6-
getComponentInstance: <T>(selectorOrComponent: string | Type<T>) => T;
6+
getComponentInstance: <I = T>(selector?: string) => I;
77
debug: () => void;
88
detectChanges: (checkNoChanges?: boolean) => void;
99
fixture: any;

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import { Options, Result, ComponentInput } from './models';
88
@Component({ selector: 'test-component', template: '' })
99
class TestComponent {}
1010

11-
export async function createComponent(template: string, options: Options): Promise<Result>;
12-
export async function createComponent<T>(component: ComponentInput<T>, options: Options): Promise<Result>;
11+
export async function createComponent<T>(template: string, options: Options): Promise<Result<T>>;
12+
export async function createComponent<T>(component: ComponentInput<T>, options: Options): Promise<Result<T>>;
1313
export async function createComponent<T>(
1414
templateOrComponent: string | ComponentInput<T>,
1515
{ detectChanges = true, declarations = [], providers = [], imports = [], schemas = [] }: Options,
16-
): Promise<Result> {
16+
): Promise<Result<T>> {
1717
const isTemplate = typeof templateOrComponent === 'string';
1818
const testComponent = isTemplate ? [TestComponent] : [];
1919

@@ -39,12 +39,10 @@ export async function createComponent<T>(
3939
fixture,
4040
container: fixture.nativeElement,
4141
get: TestBed.get,
42-
getComponentInstance: <C>(selectorOrComponent: string | C) =>
43-
typeof selectorOrComponent === 'string'
44-
? fixture.debugElement.query(By.css(selectorOrComponent)).componentInstance
45-
: fixture.componentInstance,
46-
debug: () => console.log(prettyDOM(fixture.nativeElement)),
42+
getComponentInstance: <C = T>(selector?: string) =>
43+
selector ? fixture.debugElement.query(By.css(selector)).componentInstance : fixture.componentInstance,
4744
detectChanges: (checkNoChanges?: boolean) => fixture.detectChanges(checkNoChanges),
45+
debug: () => console.log(prettyDOM(fixture.nativeElement)),
4846
...getQueriesForElement(fixture.nativeElement),
4947
};
5048
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ test('Counter actions - fireEvent', async () => {
5656
});
5757

5858
test('Counter actions via component', async () => {
59-
const { getComponentInstance } = await createComponent<CounterComponent>(
59+
const { getComponentInstance } = await createComponent(
6060
{
6161
component: CounterComponent,
6262
parameters: {
@@ -68,7 +68,7 @@ test('Counter actions via component', async () => {
6868
},
6969
);
7070

71-
const counter = getComponentInstance(CounterComponent);
71+
const counter = getComponentInstance();
7272
counter.increment();
7373
counter.increment();
7474
counter.increment();
@@ -79,7 +79,7 @@ test('Counter actions via component', async () => {
7979
});
8080

8181
test('Counter actions via component without parameters', async () => {
82-
const { getComponentInstance } = await createComponent<CounterComponent>(
82+
const { getComponentInstance } = await createComponent(
8383
{
8484
component: CounterComponent,
8585
},
@@ -88,7 +88,7 @@ test('Counter actions via component without parameters', async () => {
8888
},
8989
);
9090

91-
const counter = getComponentInstance(CounterComponent);
91+
const counter = getComponentInstance();
9292
counter.increment();
9393
counter.increment();
9494
counter.increment();
@@ -99,7 +99,7 @@ test('Counter actions via component without parameters', async () => {
9999
});
100100

101101
test('Counter via component - test template', async () => {
102-
const { getByText, detectChanges, getByTestId } = await createComponent<CounterComponent>(
102+
const { getByText, detectChanges, getByTestId } = await createComponent(
103103
{
104104
component: CounterComponent,
105105
parameters: {

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,10 @@ export class LoginFormComponent {
4545

4646
test('login form submits', async () => {
4747
const fakeUser = { username: 'jackiechan', password: 'hiya! 🥋' };
48-
const { getComponentInstance, container, getByLabelText, getByText } = await createComponent(
49-
`<login-form></login-form>`,
50-
{
51-
declarations: [LoginFormComponent],
52-
imports: [ReactiveFormsModule],
53-
},
54-
);
48+
const { getComponentInstance, getByLabelText, getByText } = await createComponent(`<login-form></login-form>`, {
49+
declarations: [LoginFormComponent],
50+
imports: [ReactiveFormsModule],
51+
});
5552

5653
const loginForm = getComponentInstance<LoginFormComponent>('login-form');
5754
loginForm.handleLogin.emit = jest.fn();

tslint.json

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"variable-declaration": "nospace"
6161
}
6262
],
63-
"unified-signatures": true,
6463
"variable-name": false,
6564
"whitespace": [true, "check-branch", "check-decl", "check-operator", "check-separator", "check-type"],
6665
"no-output-on-prefix": true,

0 commit comments

Comments
 (0)