Skip to content

Commit c592a99

Browse files
Merge branch 'main' into ng-19
2 parents 77b276e + 4bd4ab2 commit c592a99

File tree

3 files changed

+52
-32
lines changed

3 files changed

+52
-32
lines changed

.all-contributorsrc

+9
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,15 @@
410410
"code",
411411
"test"
412412
]
413+
},
414+
{
415+
"login": "Arthie",
416+
"name": "Arthur Petrie",
417+
"avatar_url": "https://avatars.githubusercontent.com/u/16376476?v=4",
418+
"profile": "https://arthurpetrie.com",
419+
"contributions": [
420+
"code"
421+
]
413422
}
414423
],
415424
"contributorsPerLine": 7,

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ Thanks goes to these people ([emoji key][emojis]):
270270
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Hyperxq"><img src="https://avatars.githubusercontent.com/u/22332354?v=4?s=100" width="100px;" alt="Daniel Ramírez Barrientos"/><br /><sub><b>Daniel Ramírez Barrientos</b></sub></a><br /><a href="https://github.com/testing-library/angular-testing-library/commits?author=Hyperxq" title="Code">💻</a></td>
271271
<td align="center" valign="top" width="14.28%"><a href="https://github.com/mlz11"><img src="https://avatars.githubusercontent.com/u/94069699?v=4?s=100" width="100px;" alt="Mahdi Lazraq"/><br /><sub><b>Mahdi Lazraq</b></sub></a><br /><a href="https://github.com/testing-library/angular-testing-library/commits?author=mlz11" title="Code">💻</a> <a href="https://github.com/testing-library/angular-testing-library/commits?author=mlz11" title="Tests">⚠️</a></td>
272272
</tr>
273+
<tr>
274+
<td align="center" valign="top" width="14.28%"><a href="https://arthurpetrie.com"><img src="https://avatars.githubusercontent.com/u/16376476?v=4?s=100" width="100px;" alt="Arthur Petrie"/><br /><sub><b>Arthur Petrie</b></sub></a><br /><a href="https://github.com/testing-library/angular-testing-library/commits?author=Arthie" title="Code">💻</a></td>
275+
</tr>
273276
</tbody>
274277
</table>
275278

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

+40-32
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import {
22
ApplicationInitStatus,
33
ChangeDetectorRef,
44
Component,
5-
isStandalone,
65
NgZone,
76
OnChanges,
87
OutputRef,
98
OutputRefSubscription,
109
SimpleChange,
1110
SimpleChanges,
1211
Type,
12+
isStandalone,
1313
} from '@angular/core';
1414
import { ComponentFixture, DeferBlockBehavior, DeferBlockState, TestBed, tick } from '@angular/core/testing';
1515
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
@@ -27,15 +27,16 @@ import {
2727
waitForOptions as dtlWaitForOptions,
2828
within as dtlWithin,
2929
} from '@testing-library/dom';
30+
import { getConfig } from './config';
3031
import {
3132
ComponentOverride,
33+
OutputRefKeysWithCallback,
3234
RenderComponentOptions,
3335
RenderResult,
3436
RenderTemplateOptions,
3537
OutputRefKeysWithCallback,
3638
Config,
3739
} from './models';
38-
import { getConfig } from './config';
3940

4041
type SubscribedOutput<T> = readonly [key: keyof T, callback: (v: any) => void, subscription: OutputRefSubscription];
4142

@@ -72,7 +73,7 @@ export async function render<SutType, WrapperType = SutType>(
7273
on = {},
7374
componentProviders = [],
7475
childComponentOverrides = [],
75-
componentImports: componentImports,
76+
componentImports,
7677
excludeComponentDeclaration = false,
7778
routes = [],
7879
removeAngularAttributes = false,
@@ -119,12 +120,9 @@ export async function render<SutType, WrapperType = SutType>(
119120

120121
await TestBed.compileComponents();
121122

122-
componentProviders
123-
.reduce((acc, provider) => acc.concat(provider), [] as any[])
124-
.forEach((p: any) => {
125-
const { provide, ...provider } = p;
126-
TestBed.overrideProvider(provide, provider);
127-
});
123+
for (const { provide, ...provider } of componentProviders) {
124+
TestBed.overrideProvider(provide, provider);
125+
}
128126

129127
const componentContainer = createComponentFixture(sut, wrapper);
130128

@@ -161,7 +159,9 @@ export async function render<SutType, WrapperType = SutType>(
161159
let result;
162160

163161
if (zone) {
164-
await zone.run(() => (result = doNavigate()));
162+
await zone.run(() => {
163+
result = doNavigate();
164+
});
165165
} else {
166166
result = doNavigate();
167167
}
@@ -202,15 +202,17 @@ export async function render<SutType, WrapperType = SutType>(
202202
if (removeAngularAttributes) {
203203
createdFixture.nativeElement.removeAttribute('ng-version');
204204
const idAttribute = createdFixture.nativeElement.getAttribute('id');
205-
if (idAttribute && idAttribute.startsWith('root')) {
205+
if (idAttribute?.startsWith('root')) {
206206
createdFixture.nativeElement.removeAttribute('id');
207207
}
208208
}
209209

210210
mountedFixtures.add(createdFixture);
211211

212212
let isAlive = true;
213-
createdFixture.componentRef.onDestroy(() => (isAlive = false));
213+
createdFixture.componentRef.onDestroy(() => {
214+
isAlive = false;
215+
});
214216

215217
if (hasOnChangesHook(createdFixture.componentInstance) && Object.keys(properties).length > 0) {
216218
const changes = getChangesObj(null, componentProperties);
@@ -321,10 +323,15 @@ export async function render<SutType, WrapperType = SutType>(
321323
},
322324
debugElement: fixture.debugElement,
323325
container: fixture.nativeElement,
324-
debug: (element = fixture.nativeElement, maxLength, options) =>
325-
Array.isArray(element)
326-
? element.forEach((e) => console.log(dtlPrettyDOM(e, maxLength, options)))
327-
: console.log(dtlPrettyDOM(element, maxLength, options)),
326+
debug: (element = fixture.nativeElement, maxLength, options) => {
327+
if (Array.isArray(element)) {
328+
for (const e of element) {
329+
console.log(dtlPrettyDOM(e, maxLength, options));
330+
}
331+
} else {
332+
console.log(dtlPrettyDOM(element, maxLength, options));
333+
}
334+
},
328335
...replaceFindWithFindAndDetectChanges(dtlGetQueriesForElement(fixture.nativeElement, queries)),
329336
};
330337
}
@@ -426,9 +433,11 @@ function overrideComponentImports<SutType>(sut: Type<SutType> | string, imports:
426433
}
427434

428435
function overrideChildComponentProviders(componentOverrides: ComponentOverride<any>[]) {
429-
componentOverrides?.forEach(({ component, providers }) => {
430-
TestBed.overrideComponent(component, { set: { providers } });
431-
});
436+
if (componentOverrides) {
437+
for (const { component, providers } of componentOverrides) {
438+
TestBed.overrideComponent(component, { set: { providers } });
439+
}
440+
}
432441
}
433442

434443
function hasOnChangesHook<SutType>(componentInstance: SutType): componentInstance is SutType & OnChanges {
@@ -442,13 +451,10 @@ function hasOnChangesHook<SutType>(componentInstance: SutType): componentInstanc
442451

443452
function getChangesObj(oldProps: Record<string, any> | null, newProps: Record<string, any>) {
444453
const isFirstChange = oldProps === null;
445-
return Object.keys(newProps).reduce<SimpleChanges>(
446-
(changes, key) => ({
447-
...changes,
448-
[key]: new SimpleChange(isFirstChange ? null : oldProps[key], newProps[key], isFirstChange),
449-
}),
450-
{} as Record<string, any>,
451-
);
454+
return Object.keys(newProps).reduce<SimpleChanges>((changes, key) => {
455+
changes[key] = new SimpleChange(isFirstChange ? null : oldProps[key], newProps[key], isFirstChange);
456+
return changes;
457+
}, {} as Record<string, any>);
452458
}
453459

454460
function update<SutType>(
@@ -464,10 +470,12 @@ function update<SutType>(
464470
const componentInstance = fixture.componentInstance as Record<string, any>;
465471
const simpleChanges: SimpleChanges = {};
466472

467-
for (const key of prevRenderedKeys) {
468-
if (!partialUpdate && !Object.prototype.hasOwnProperty.call(newValues, key)) {
469-
simpleChanges[key] = new SimpleChange(componentInstance[key], undefined, false);
470-
delete componentInstance[key];
473+
if (!partialUpdate) {
474+
for (const key of prevRenderedKeys) {
475+
if (!Object.prototype.hasOwnProperty.call(newValues, key)) {
476+
simpleChanges[key] = new SimpleChange(componentInstance[key], undefined, false);
477+
delete componentInstance[key];
478+
}
471479
}
472480
}
473481

@@ -647,15 +655,15 @@ function replaceFindWithFindAndDetectChanges<T extends Record<string, any>>(orig
647655
* Call detectChanges for all fixtures
648656
*/
649657
function detectChangesForMountedFixtures() {
650-
mountedFixtures.forEach((fixture) => {
658+
for (const fixture of mountedFixtures) {
651659
try {
652660
fixture.detectChanges();
653661
} catch (err: any) {
654662
if (!err.message.startsWith('ViewDestroyedError')) {
655663
throw err;
656664
}
657665
}
658-
});
666+
}
659667
}
660668

661669
/**

0 commit comments

Comments
 (0)