Skip to content

Commit db6611d

Browse files
fix: return the debug element (#302)
1 parent 0db3a5c commit db6611d

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

apps/example-app/src/app/examples/19-standalone-component.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { render, screen } from '@testing-library/angular';
22
import { StandaloneComponent, StandaloneWithChildComponent } from './19-standalone-component';
33

4-
test('is possible to render a standalone component', async () => {
4+
test('can render a standalone component', async () => {
55
await render(StandaloneComponent);
66

77
const content = screen.getByTestId('standalone');
88

99
expect(content).toHaveTextContent('Standalone Component');
1010
});
1111

12-
test('is possibl to render a standalone component with a child', async () => {
12+
test('can render a standalone component with a child', async () => {
1313
await render(StandaloneWithChildComponent, {
1414
componentProperties: { name: 'Bob' },
1515
});

apps/example-app/src/app/examples/19-standalone-component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Component, Input } from '@angular/core';
55
template: `<div data-testid="standalone">Standalone Component</div>`,
66
standalone: true,
77
})
8-
export class StandaloneComponent { }
8+
export class StandaloneComponent {}
99

1010
@Component({
1111
selector: 'app-standalone-with-child',

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

+16-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
ɵisStandalone,
1111
} from '@angular/core';
1212
import { ComponentFixture, TestBed, tick } from '@angular/core/testing';
13-
import { By } from '@angular/platform-browser';
1413
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
1514
import { NavigationExtras, Router } from '@angular/router';
1615
import { RouterTestingModule } from '@angular/router/testing';
@@ -77,7 +76,7 @@ export async function render<SutType, WrapperType = SutType>(
7776
excludeComponentDeclaration,
7877
wrapper,
7978
}),
80-
imports: addAutoImports(sut,{
79+
imports: addAutoImports(sut, {
8180
imports: imports.concat(defaultImports),
8281
routes,
8382
}),
@@ -129,23 +128,23 @@ export async function render<SutType, WrapperType = SutType>(
129128
const [path, params] = (basePath + href).split('?');
130129
const queryParams = params
131130
? params.split('&').reduce((qp, q) => {
132-
const [key, value] = q.split('=');
133-
const currentValue = qp[key];
134-
if (typeof currentValue === 'undefined') {
135-
qp[key] = value;
136-
} else if (Array.isArray(currentValue)) {
137-
qp[key] = [...currentValue, value];
138-
} else {
139-
qp[key] = [currentValue, value];
140-
}
141-
return qp;
142-
}, {} as Record<string, string | string[]>)
131+
const [key, value] = q.split('=');
132+
const currentValue = qp[key];
133+
if (typeof currentValue === 'undefined') {
134+
qp[key] = value;
135+
} else if (Array.isArray(currentValue)) {
136+
qp[key] = [...currentValue, value];
137+
} else {
138+
qp[key] = [currentValue, value];
139+
}
140+
return qp;
141+
}, {} as Record<string, string | string[]>)
143142
: undefined;
144143

145144
const navigateOptions: NavigationExtras | undefined = queryParams
146145
? {
147-
queryParams,
148-
}
146+
queryParams,
147+
}
149148
: undefined;
150149

151150
const doNavigate = () => {
@@ -172,7 +171,7 @@ export async function render<SutType, WrapperType = SutType>(
172171
rerender,
173172
change,
174173
// @ts-ignore: fixture assigned
175-
debugElement: typeof sut === 'string' ? fixture.debugElement : fixture.debugElement.query(By.directive(sut)),
174+
debugElement: fixture.debugElement,
176175
// @ts-ignore: fixture assigned
177176
container: fixture.nativeElement,
178177
debug: (element = fixture.nativeElement, maxLength, options) =>
@@ -398,7 +397,7 @@ if (typeof process === 'undefined' || !process.env?.ATL_SKIP_AUTO_CLEANUP) {
398397
}
399398

400399
@Component({ selector: 'atl-wrapper-component', template: '' })
401-
class WrapperComponent { }
400+
class WrapperComponent {}
402401

403402
/**
404403
* Wrap findBy queries to poke the Angular change detection cycle

projects/testing-library/tests/render.spec.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '@angular/core';
1111
import { NoopAnimationsModule, BrowserAnimationsModule } from '@angular/platform-browser/animations';
1212
import { TestBed } from '@angular/core/testing';
13-
import { render, fireEvent } from '../src/public_api';
13+
import { render, fireEvent, screen } from '../src/public_api';
1414

1515
@Component({
1616
selector: 'atl-fixture',
@@ -33,6 +33,21 @@ test('creates queries and events', async () => {
3333
fireEvent.click(view.getByText('button'));
3434
});
3535

36+
describe('standalone', () => {
37+
@Component({
38+
selector: 'atl-fixture',
39+
template: ` {{ name }} `,
40+
})
41+
class StandaloneFixtureComponent {
42+
@Input() name = '';
43+
}
44+
45+
it('renders standalone component', async () => {
46+
await render(StandaloneFixtureComponent, { componentProperties: { name: 'Bob' } });
47+
expect(screen.getByText('Bob')).toBeInTheDocument();
48+
});
49+
});
50+
3651
describe('removeAngularAttributes', () => {
3752
it('should remove angular attribute', async () => {
3853
await render(FixtureComponent, {
@@ -148,6 +163,13 @@ test('waits for angular app initialization before rendering components', async (
148163
],
149164
});
150165

151-
expect(TestBed.inject(ApplicationInitStatus).done).toEqual(true);
166+
expect(TestBed.inject(ApplicationInitStatus).done).toBe(true);
152167
expect(mock).toHaveBeenCalled();
153168
});
169+
170+
test('gets the DebugElement', async () => {
171+
const view = await render(FixtureComponent);
172+
173+
expect(view.debugElement).not.toBeNull();
174+
expect(view.debugElement.componentInstance).toBeInstanceOf(FixtureComponent);
175+
});

0 commit comments

Comments
 (0)