Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: flatten component providers for consistency to the framework #507

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading