Skip to content

Commit 966ca59

Browse files
committed
fix: return the debug element
1 parent 0db3a5c commit 966ca59

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
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-16
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export async function render<SutType, WrapperType = SutType>(
7777
excludeComponentDeclaration,
7878
wrapper,
7979
}),
80-
imports: addAutoImports(sut,{
80+
imports: addAutoImports(sut, {
8181
imports: imports.concat(defaultImports),
8282
routes,
8383
}),
@@ -129,23 +129,23 @@ export async function render<SutType, WrapperType = SutType>(
129129
const [path, params] = (basePath + href).split('?');
130130
const queryParams = params
131131
? 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[]>)
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[]>)
143143
: undefined;
144144

145145
const navigateOptions: NavigationExtras | undefined = queryParams
146146
? {
147-
queryParams,
148-
}
147+
queryParams,
148+
}
149149
: undefined;
150150

151151
const doNavigate = () => {
@@ -172,7 +172,7 @@ export async function render<SutType, WrapperType = SutType>(
172172
rerender,
173173
change,
174174
// @ts-ignore: fixture assigned
175-
debugElement: typeof sut === 'string' ? fixture.debugElement : fixture.debugElement.query(By.directive(sut)),
175+
debugElement: typeof sut === 'string' ? fixture.debugElement : fixture.debugElement.query(By.all(sut)),
176176
// @ts-ignore: fixture assigned
177177
container: fixture.nativeElement,
178178
debug: (element = fixture.nativeElement, maxLength, options) =>
@@ -398,7 +398,7 @@ if (typeof process === 'undefined' || !process.env?.ATL_SKIP_AUTO_CLEANUP) {
398398
}
399399

400400
@Component({ selector: 'atl-wrapper-component', template: '' })
401-
class WrapperComponent { }
401+
class WrapperComponent {}
402402

403403
/**
404404
* 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)