Skip to content

Commit 1d8fc02

Browse files
authored
Merge pull request #2096 from satanTime/issues/2087
fix(MockInstance): correctly accepts falsy values #2087
2 parents 3574bcb + 8900fc3 commit 1d8fc02

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

libs/ng-mocks/src/lib/mock-instance/mock-instance.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ const parseMockInstanceArgs = (args: any[]): MockInstanceArgs => {
4242
set.accessor = args[2];
4343
} else {
4444
set.value = args[0];
45-
if (typeof set.value !== 'function') {
46-
set.value = set.value?.init;
45+
if (set.value && typeof set.value === 'object') {
46+
set.value = set.value.init;
4747
}
4848
}
4949

@@ -177,9 +177,9 @@ export function MockInstance<T>(
177177
export function MockInstance<T>(declaration: Type<T> | AbstractType<T> | InjectionToken<T>, ...args: any[]) {
178178
funcImportExists(declaration, 'MockInstance');
179179

180-
const { key, value, accessor } = parseMockInstanceArgs(args);
180+
if (args.length) {
181+
const { key, value, accessor } = parseMockInstanceArgs(args);
181182

182-
if (value) {
183183
return mockInstanceConfig(declaration, key, value, accessor);
184184
}
185185

tests/issue-2087/test.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { CommonModule } from '@angular/common';
2+
import { Component, Directive, NgModule } from '@angular/core';
3+
import {
4+
MockBuilder,
5+
MockInstance,
6+
MockProvider,
7+
MockRender,
8+
} from 'ng-mocks';
9+
10+
@Directive({
11+
selector: 'target',
12+
})
13+
class MockDirective {
14+
public readonly boolean = false;
15+
public readonly number = 0;
16+
public readonly string = '';
17+
}
18+
19+
@Component({
20+
selector: 'target',
21+
template: ``,
22+
})
23+
class TargetComponent {
24+
public constructor(public readonly mock: MockDirective) {}
25+
}
26+
27+
@NgModule({
28+
declarations: [TargetComponent, MockDirective],
29+
imports: [CommonModule],
30+
})
31+
class TargetModule {}
32+
33+
// MockInstance doesn't provide falsy values.
34+
// @see https://github.com/ike18t/ng-mocks/issues/2087
35+
describe('issue-2087', () => {
36+
MockInstance.scope();
37+
beforeEach(() => MockBuilder(TargetComponent, TargetModule));
38+
39+
const tests: Array<[keyof MockDirective, any, any]> = [
40+
['string', '', 'test'],
41+
['number', 0, 1],
42+
['boolean', false, true],
43+
];
44+
45+
tests.forEach(([kind, falsy, truthy]) =>
46+
describe(kind, () => {
47+
it(`works for falsy`, () => {
48+
MockInstance(MockDirective, kind, falsy);
49+
const fixture = MockRender(TargetComponent);
50+
expect(fixture.componentInstance.mock[kind]).toEqual(falsy);
51+
});
52+
53+
it(`works for truthy`, () => {
54+
MockInstance(MockDirective, kind, truthy);
55+
const fixture = MockRender(TargetComponent);
56+
expect(fixture.componentInstance.mock[kind]).toEqual(truthy);
57+
});
58+
}),
59+
);
60+
});

0 commit comments

Comments
 (0)