Skip to content

Commit 570af04

Browse files
committed
fix: override providers for createComponentFactory
1 parent 9fdd98f commit 570af04

File tree

2 files changed

+77
-19
lines changed

2 files changed

+77
-19
lines changed

projects/spectator/src/lib/spectator/create-factory.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Provider, Type } from '@angular/core';
1+
import { Type } from '@angular/core';
22
import { TestBed, waitForAsync } from '@angular/core/testing';
33
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
44

@@ -7,6 +7,7 @@ import { setProps } from '../internals/query';
77
import * as customMatchers from '../matchers';
88
import { addMatchers } from '../core';
99
import { isType } from '../types';
10+
import { ModuleMetadata } from '../base/initial-module';
1011

1112
import { initialSpectatorModule } from './initial-module';
1213
import { getSpectatorDefaultOptions, SpectatorOptions } from './options';
@@ -73,30 +74,19 @@ export function createComponentFactory<C>(typeOrOptions: Type<C> | SpectatorOpti
7374

7475
const moduleMetadata = initialSpectatorModule<C>(options);
7576

76-
beforeEach(
77-
waitForAsync(() => {
78-
addMatchers(customMatchers);
79-
TestBed.configureTestingModule(moduleMetadata).overrideModule(BrowserDynamicTestingModule, {
80-
set: {
81-
entryComponents: moduleMetadata.entryComponents
82-
}
83-
});
84-
85-
overrideModules(options);
86-
87-
overrideComponentIfProviderOverridesSpecified(options);
88-
89-
TestBed.compileComponents();
90-
})
91-
);
77+
beforeEach(waitForAsync(() => {
78+
configureAndCompileTestingModule(moduleMetadata, options);
79+
}));
9280

9381
return (overrides?: SpectatorOverrides<C>) => {
9482
const defaults: SpectatorOverrides<C> = { props: {}, detectChanges: true, providers: [] };
9583
const { detectChanges, props, providers } = { ...defaults, ...overrides };
9684

9785
if (providers && providers.length) {
98-
providers.forEach((provider: Provider) => {
99-
TestBed.overrideProvider((provider as any).provide, provider as any);
86+
TestBed.resetTestingModule();
87+
initializeTestingModule({
88+
...options,
89+
providers
10090
});
10191
}
10292

@@ -118,3 +108,28 @@ function createSpectator<C>(options: Required<SpectatorOptions<C>>, props?: Part
118108

119109
return new Spectator(fixture, debugElement, component, debugElement.nativeElement);
120110
}
111+
112+
function initializeTestingModule<C>(typeOrOptions: Type<C> | SpectatorOptions<C>): void {
113+
const options = isType(typeOrOptions)
114+
? getSpectatorDefaultOptions<C>({ component: typeOrOptions })
115+
: getSpectatorDefaultOptions(typeOrOptions);
116+
117+
const moduleMetadata = initialSpectatorModule<C>(options);
118+
119+
configureAndCompileTestingModule(moduleMetadata, options);
120+
}
121+
122+
function configureAndCompileTestingModule<C>(moduleMetadata: ModuleMetadata, options: Required<SpectatorOptions<C>>): void {
123+
addMatchers(customMatchers);
124+
TestBed.configureTestingModule(moduleMetadata).overrideModule(BrowserDynamicTestingModule, {
125+
set: {
126+
entryComponents: moduleMetadata.entryComponents
127+
}
128+
});
129+
130+
overrideModules(options);
131+
132+
overrideComponentIfProviderOverridesSpecified(options);
133+
134+
TestBed.compileComponents();
135+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { createComponentFactory, Spectator } from '@ngneat/spectator';
2+
import { Component, InjectionToken } from '@angular/core';
3+
4+
@Component({
5+
selector: 'lib-test',
6+
template: '<div>test</div>'
7+
})
8+
class TestComponent {}
9+
10+
class StateMock {}
11+
12+
class AnotherStateMock {}
13+
14+
export const STATE = new InjectionToken<any>('Angular State');
15+
16+
describe('Override providers', () => {
17+
let spectator: Spectator<TestComponent>;
18+
19+
const createComponent = createComponentFactory({
20+
component: TestComponent,
21+
providers: [{ provide: STATE, useClass: AnotherStateMock }]
22+
});
23+
24+
beforeEach(() => (spectator = createComponent()));
25+
26+
it('should create', () => {
27+
expect(spectator).toBeTruthy();
28+
});
29+
30+
it('should get the correct instance of STATE', () => {
31+
const service = spectator.inject(STATE);
32+
33+
expect(service instanceof AnotherStateMock).toBeTrue();
34+
});
35+
36+
it('should get the mocked instance of STATE injection token', () => {
37+
spectator = createComponent({ providers: [{ provide: STATE, useClass: StateMock }] });
38+
39+
const service = spectator.inject(STATE);
40+
41+
expect(service instanceof StateMock).toBeTrue();
42+
});
43+
});

0 commit comments

Comments
 (0)