Skip to content

Commit

Permalink
fix(MockInstance): multi-token and multi-service help-me-mom#5585
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed May 13, 2023
1 parent b01c2ee commit 9c85dea
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/articles/api/MockInstance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions libs/ng-mocks/src/lib/mock-instance/mock-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export function MockInstance<T extends object, K extends keyof T, S extends T[K]
*/
export function MockInstance<T>(
declaration: InjectionToken<T>,
init?: (instance: T | undefined, injector: Injector | undefined) => Partial<T>,
init?: (instance: T | undefined, injector: Injector | undefined) => Partial<T> | Array<Partial<T>>,
): void;

/**
Expand All @@ -166,7 +166,7 @@ export function MockInstance<T>(
export function MockInstance<T>(
declaration: InjectionToken<T>,
config?: {
init?: (instance: T | undefined, injector: Injector | undefined) => Partial<T>;
init?: (instance: T | undefined, injector: Injector | undefined) => Partial<T> | Array<Partial<T>>;
},
): void;

Expand All @@ -190,7 +190,7 @@ export function MockInstance<T>(
*/
export function MockInstance<T>(
declaration: AnyType<T>,
init?: (instance: T, injector: Injector | undefined) => void | Partial<T>,
init?: (instance: T, injector: Injector | undefined) => void | Partial<T> | Array<Partial<T>>,
): void;

/**
Expand Down Expand Up @@ -219,7 +219,7 @@ export function MockInstance<T>(
export function MockInstance<T>(
declaration: AnyType<T>,
config?: {
init?: (instance: T, injector: Injector | undefined) => void | Partial<T>;
init?: (instance: T, injector: Injector | undefined) => void | Partial<T> | Array<Partial<T>>;
},
): void;

Expand Down
68 changes: 68 additions & 0 deletions tests/issue-5537/test.spec.ts
Original file line number Diff line number Diff line change
@@ -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<string>('TOKEN');

@Injectable()
class TargetService {
constructor(@Inject(TOKEN) public tokens: Array<string>) {}
}

@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']);
});
});
});

0 comments on commit 9c85dea

Please sign in to comment.