Skip to content

feat(jest): add createMock #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions projects/testing-library/src/jest-utils/create-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Type, Provider } from '@angular/core';

export type Mock<T> = T & { [K in keyof T]: T[K] & jest.Mock };

export function createMock<T>(type: Type<T>): Mock<T> {
const mock: any = {};

function mockFunctions(proto: any) {
if (!proto) {
return;
}

for (const prop of Object.getOwnPropertyNames(proto)) {
if (prop === 'constructor') {
continue;
}

const descriptor = Object.getOwnPropertyDescriptor(proto, prop);
if (typeof descriptor.value === 'function') {
mock[prop] = jest.fn();
}
}

mockFunctions(Object.getPrototypeOf(proto));
}

mockFunctions(type.prototype);

return mock;
}

export function provideMock<T>(type: Type<T>): Provider {
return {
provide: type,
useFactory: () => createMock(type),
};
}
1 change: 1 addition & 0 deletions projects/testing-library/src/jest-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './configure-test-suite';
export * from './create-mock';
52 changes: 52 additions & 0 deletions projects/testing-library/tests/jest-utils/create-mock.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { createMock, provideMock, Mock } from '../../src/jest-utils';
import { render } from '../../src/public_api';
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';

class FixtureService {
constructor(private foo: string, public bar: string) {}

print() {
console.log(this.foo, this.bar);
}
}

@Component({
selector: 'fixture',
template: `
<button (click)="print()">Print</button>
`,
})
export class FixtureComponent {
constructor(private service: FixtureService) {}

print() {
this.service.print();
}
}

it('mocks all functions', () => {
const mock = createMock(FixtureService);
expect(mock.print.mock).toBeDefined();
});

it('provides a mock service', async () => {
const { click, getByText } = await render(FixtureComponent, {
providers: [provideMock(FixtureService)],
});
const service = TestBed.get<FixtureService>(FixtureService);

click(getByText('Print'));
expect(service.print).toHaveBeenCalledTimes(1);
});

it('is possible to write a mock implementation', async done => {
const { click, getByText } = await render(FixtureComponent, {
providers: [provideMock(FixtureService)],
});

const service = TestBed.get<FixtureService>(FixtureService) as Mock<FixtureService>;
service.print.mockImplementation(() => done());

click(getByText('Print'));
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable } from '@angular/core';
import { Component } from '@angular/core';
import { render } from '../../src/public_api';
import { TestBed } from '@angular/core/testing';

// tslint:disable: no-use-before-declare
test('shows the service value', async () => {
Expand Down