Skip to content

Commit

Permalink
fix(MockBuilder): respects initial config of declarations #3265
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Aug 7, 2022
1 parent 55df70f commit 1bea651
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion libs/ng-mocks/src/lib/mock-builder/mock-builder.promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ export class MockBuilderPromise implements IMockBuilder {
}

private setConfigDef(def: any, config: any): void {
this.configDef.set(def, config ?? this.configDefault);
if (config || !this.configDef.has(def)) {
this.configDef.set(def, config ?? this.configDefault);
}
}

private setDefValue(def: any, mock: any): void {
Expand Down
43 changes: 43 additions & 0 deletions tests/issue-3265/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Injectable } from '@angular/core';

import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Injectable()
class SomeService {
someMethod(x: string) {
return x;
}
}

@Injectable()
class ServiceToTest {
constructor(public someService: SomeService) {}

foo(x: string) {
return this.someService.someMethod(x);
}
}

// @see https://github.com/help-me-mom/ng-mocks/issues/3265
describe('issue-3265', () => {
beforeEach(() =>
MockBuilder(ServiceToTest, SomeService).mock(SomeService, {
someMethod:
typeof jest === 'undefined'
? jasmine.createSpy().and.callFake((x: string) => x)
: // in case of jest
jest.fn((x: string) => x),
}),
);

it('should do something', () => {
const serviceToTest =
MockRender(ServiceToTest).point.componentInstance;
const someServiceSpy = ngMocks.get(SomeService);

const result = serviceToTest.foo('hello world');

expect(result).toBe('hello world');
expect(someServiceSpy.someMethod).toHaveBeenCalled();
});
});

0 comments on commit 1bea651

Please sign in to comment.