Skip to content

Commit

Permalink
fix: skipping mocking of EventManager and DomSharedStylesHost
Browse files Browse the repository at this point in the history
closes #162
  • Loading branch information
satanTime committed Jul 12, 2020
1 parent 7f0fd73 commit 84b2720
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 12 deletions.
9 changes: 5 additions & 4 deletions lib/mock-helper/mock-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export const ngMocks: {
output<T = any>(debugNode: MockedDebugNode, output: string): EventEmitter<T>;
output<T = any, D = undefined>(debugNode: MockedDebugNode, output: string, notFoundValue: D): D | EventEmitter<T>;

stub<I extends object, O extends object>(instance: I, overrides: O): I & O;
stub<T = MockedFunction>(instance: any, name: string, style?: 'get' | 'set'): T;
stub<I extends object, O extends object>(instance: I, overrides: O): I & O;
} = {
find: (...args: any[]) => {
const el: MockedDebugElement = args[0];
Expand Down Expand Up @@ -315,8 +315,9 @@ export const ngMocks: {
},

flushTestBed(): void {
(getTestBed() as any)._instantiated = false;
(getTestBed() as any)._moduleFactory = undefined;
(getTestBed() as any)._testModuleRef = null;
const testBed: any = getTestBed();
testBed._instantiated = false;
testBed._moduleFactory = undefined;
testBed._testModuleRef = null;
},
};
2 changes: 1 addition & 1 deletion lib/mock-module/mock-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { MockService, mockServiceHelper } from '../mock-service';

export type MockedModule<T> = T & Mock & {};

const neverMockProvidedFunction = ['DomRendererFactory2', 'RendererFactory2'];
const neverMockProvidedFunction = ['DomRendererFactory2', 'DomSharedStylesHost', 'EventManager', 'RendererFactory2'];

/**
* Can be changed any time.
Expand Down
16 changes: 9 additions & 7 deletions lib/mock-service/mock-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,23 @@ let customMockFunction: ((mockName: string) => MockedFunction) | undefined;

const mockServiceHelperPrototype = {
mockFunction: (mockName: string, original: boolean = false): MockedFunction => {
let func: any;
if (customMockFunction && !original) {
return customMockFunction(mockName);
func = customMockFunction(mockName);
} else {
func = (val: any) => {
if (setValue) {
setValue(val);
}
return value;
};
}

// magic to make getters / setters working

let value: any;
let setValue: any;

const func: any = (val: any) => {
if (setValue) {
setValue(val);
}
return value;
};
func.__ngMocks = true;
func.__ngMocksSet = (newSetValue: any) => (setValue = newSetValue);
func.__ngMocksGet = (newValue: any) => (value = newValue);
Expand Down
79 changes: 79 additions & 0 deletions tests/issue-162/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Component, NgModule, RendererFactory2 } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { BrowserModule, EventManager } from '@angular/platform-browser';
import { MockBuilder, ngMocks } from 'ng-mocks';
import { MockRender } from 'ng-mocks/dist/lib/mock-render/mock-render';

// fix to support both jasmine and jest in the test
declare const jest: any;
declare const jasmine: any;

@Component({
selector: 'app-root',
template: `<a (click)="title = 'test'">click</a>`,
})
export class AppComponent {
title = 'ng-routing-test';
}

@Component({
selector: 'app-mock',
template: `<a (click)="title = 'test'">click</a>`,
})
export class MockComponent {
title = 'ng-routing-test';
}

@NgModule({
bootstrap: [AppComponent],
declarations: [AppComponent, MockComponent],
exports: [MockComponent],
imports: [BrowserModule],
})
export class AppModule {}

describe('issue-162', () => {
beforeEach(() => MockBuilder(AppComponent, AppModule));

it(`verifies that EventManager was not mocked`, () => {
MockRender(AppComponent);
TestBed.resetTestingModule();
const target: EventManager = TestBed.get(EventManager);
expect(target.addEventListener).toEqual(jasmine.any(Function));
expect((target.addEventListener as any).__ngMocks).toBeUndefined();
TestBed.resetTestingModule();
});

it(`verifies that RendererFactory2 was not mocked`, () => {
MockRender(AppComponent);
TestBed.resetTestingModule();
const target: RendererFactory2 = TestBed.get(RendererFactory2);
expect(target.createRenderer).toEqual(jasmine.any(Function));
expect((target.createRenderer as any).__ngMocks).toBeUndefined();
});

it(`verifies that spyOnProperty works`, () => {
const fixture = MockRender(MockComponent);

// mocking the property via auto spy.
ngMocks.stub(fixture.point.componentInstance, 'title', 'get');
ngMocks.stub(fixture.point.componentInstance, 'title', 'set');
let getSpy: any;
let setSpy: any;

// creating spies
if (typeof jest !== 'undefined') {
getSpy = jest.spyOn(fixture.point.componentInstance, 'title', 'get');
setSpy = jest.spyOn(fixture.point.componentInstance, 'title', 'set');
getSpy.mockReturnValue('spy');
} else if (typeof jasmine !== 'undefined') {
getSpy = spyOnProperty(fixture.point.componentInstance, 'title', 'get');
setSpy = spyOnProperty(fixture.point.componentInstance, 'title', 'set');
getSpy.and.returnValue('spy');
}

expect(fixture.point.componentInstance.title).toEqual('spy');
fixture.point.componentInstance.title = 'updated';
expect(setSpy).toHaveBeenCalledWith('updated');
});
});

0 comments on commit 84b2720

Please sign in to comment.