diff --git a/docs/articles/api/MockInstance.md b/docs/articles/api/MockInstance.md index eb001fec68..1c88c64b85 100644 --- a/docs/articles/api/MockInstance.md +++ b/docs/articles/api/MockInstance.md @@ -97,6 +97,12 @@ MockInstance(TOKEN, (instance, injector) => { MockInstance(TOKEN, () => true); ``` +If you need to mock a multi-value token, simply return an array: + +```ts +MockInstance(TOKEN, () => [multiValue1, multiValue2, multiValue3]); +``` + ## Customization scopes Time to time, we need to apply a set of customizations for a suite or a test. diff --git a/libs/ng-mocks/src/lib/mock-instance/mock-instance.ts b/libs/ng-mocks/src/lib/mock-instance/mock-instance.ts index c52808fec1..1001830fc2 100644 --- a/libs/ng-mocks/src/lib/mock-instance/mock-instance.ts +++ b/libs/ng-mocks/src/lib/mock-instance/mock-instance.ts @@ -148,7 +148,7 @@ export function MockInstance( declaration: InjectionToken, - init?: (instance: T | undefined, injector: Injector | undefined) => Partial, + init?: (instance: T | undefined, injector: Injector | undefined) => Partial | Array>, ): void; /** @@ -166,7 +166,7 @@ export function MockInstance( export function MockInstance( declaration: InjectionToken, config?: { - init?: (instance: T | undefined, injector: Injector | undefined) => Partial; + init?: (instance: T | undefined, injector: Injector | undefined) => Partial | Array>; }, ): void; @@ -190,7 +190,7 @@ export function MockInstance( */ export function MockInstance( declaration: AnyType, - init?: (instance: T, injector: Injector | undefined) => void | Partial, + init?: (instance: T, injector: Injector | undefined) => void | Partial | Array>, ): void; /** @@ -219,7 +219,7 @@ export function MockInstance( export function MockInstance( declaration: AnyType, config?: { - init?: (instance: T, injector: Injector | undefined) => void | Partial; + init?: (instance: T, injector: Injector | undefined) => void | Partial | Array>; }, ): void; diff --git a/tests/issue-5537/test.spec.ts b/tests/issue-5537/test.spec.ts new file mode 100644 index 0000000000..dee706e47b --- /dev/null +++ b/tests/issue-5537/test.spec.ts @@ -0,0 +1,68 @@ +import { + Inject, + Injectable, + InjectionToken, + NgModule, +} from '@angular/core'; +import { TestBed } from '@angular/core/testing'; + +import { + MockBuilder, + MockInstance, + MockRender, + ngMocks, +} from 'ng-mocks'; + +const TOKEN = new InjectionToken('TOKEN'); + +@Injectable() +class TargetService { + constructor(@Inject(TOKEN) public tokens: Array) {} +} + +@NgModule({ + providers: [ + TargetService, + { + provide: TOKEN, + multi: true, + useValue: '1', + }, + { + provide: TOKEN, + multi: true, + useValue: '2', + }, + ], +}) +class TargetModule {} + +// @see https://github.com/help-me-mom/ng-mocks/discussions/5537 +describe('issue-5537', () => { + describe('real', () => { + beforeEach(() => + TestBed.configureTestingModule({ + imports: [TargetModule], + }).compileComponents(), + ); + + it('creates multi token', () => { + const service = ngMocks.get(TargetService); + expect(service.tokens).toEqual(['1', '2']); + }); + }); + + describe('mock', () => { + MockInstance.scope(); + + beforeEach(() => MockBuilder(TargetService, TargetModule)); + + it('creates multi token', () => { + MockInstance(TOKEN, () => ['3', '4']); + + const service = + MockRender(TargetService).point.componentInstance; + expect(service.tokens).toEqual(['3', '4']); + }); + }); +});