Skip to content

Commit

Permalink
fix: flatten component providers for consistency to the framework (#507)
Browse files Browse the repository at this point in the history
Closes #506
  • Loading branch information
lacolaco authored Dec 14, 2024
1 parent fdcf5fa commit d59e4f2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ export async function render<SutType, WrapperType = SutType>(

await TestBed.compileComponents();

for (const { provide, ...provider } of componentProviders) {
// Angular supports nested arrays of providers, so we need to flatten them to emulate the same behavior.
for (const { provide, ...provider } of componentProviders.flat(Infinity)) {
TestBed.overrideProvider(provide, provider);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import { Injectable, Provider } from '@angular/core';
import { Component } from '@angular/core';
import { render, screen } from '../../src/public_api';

Expand Down Expand Up @@ -42,6 +42,24 @@ test('shows the provided service value with template syntax', async () => {
expect(screen.getByText('bar')).toBeInTheDocument();
});

test('flatten the nested array of component providers', async () => {
const provideService = (): Provider => [
{
provide: Service,
useValue: {
foo() {
return 'bar';
},
},
},
];
await render(FixtureComponent, {
componentProviders: [provideService()],
});

expect(screen.getByText('bar')).toBeInTheDocument();
});

@Injectable()
class Service {
foo() {
Expand Down

0 comments on commit d59e4f2

Please sign in to comment.