Skip to content

Commit

Permalink
feat: add provideMockWithValues (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwestphal authored Jul 15, 2021
1 parent f6d559d commit fa34bdd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
"core-js": "^3.6.5",
"rxjs": "^6.5.5",
"tslib": "^2.0.0",
"zone.js": "~0.11.4"
"zone.js": "^0.11.4"
},
"devDependencies": {
"@angular-devkit/build-angular": "12.1.0",
"@angular-eslint/eslint-plugin": "~12.2.0",
"@angular-eslint/eslint-plugin-template": "~12.2.0",
"@angular-eslint/template-parser": "~12.2.0",
"@angular-eslint/eslint-plugin": "^12.2.0",
"@angular-eslint/eslint-plugin-template": "^12.2.0",
"@angular-eslint/template-parser": "^12.2.0",
"@angular/cli": "12.1.0",
"@angular/compiler-cli": "12.1.1",
"@angular/language-service": "12.1.1",
Expand All @@ -65,7 +65,7 @@
"@nrwl/workspace": "12.5.1",
"@testing-library/jasmine-dom": "^1.2.0",
"@testing-library/jest-dom": "^5.11.10",
"@types/jasmine": "~3.5.0",
"@types/jasmine": "^3.5.0",
"@types/jest": "^26.0.23",
"@types/node": "14.14.37",
"@typescript-eslint/eslint-plugin": "4.28.2",
Expand All @@ -79,13 +79,13 @@
"eslint-plugin-jest-dom": "3.9.0",
"eslint-plugin-testing-library": "4.9.0",
"husky": "^6.0.0",
"jasmine-core": "~3.7.0",
"jasmine-spec-reporter": "~5.0.0",
"jasmine-core": "^3.7.0",
"jasmine-spec-reporter": "^5.0.0",
"jest": "^27.0.6",
"jest-preset-angular": "9.0.4",
"karma": "~6.3.4",
"karma-chrome-launcher": "~3.1.0",
"karma-jasmine": "~4.0.0",
"karma": "^6.3.4",
"karma-chrome-launcher": "^3.1.0",
"karma-jasmine": "^4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"lint-staged": "^10.2.11",
"ng-packagr": "12.0.0",
Expand Down
7 changes: 7 additions & 0 deletions projects/jest-utils/src/lib/create-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ export function provideMock<T>(type: Type<T>): Provider {
useValue: createMock(type),
};
}

export function provideMockWithValues<T, K extends keyof T>(type: Type<T>, values: Partial<Record<K, T[K]>>): Provider {
return {
provide: type,
useValue: createMockWithValues(type, values),
};
}
23 changes: 22 additions & 1 deletion projects/jest-utils/tests/create-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { fireEvent, render, screen } from '@testing-library/angular';

import { createMock, provideMock, Mock } from '../src/public_api';
import { createMock, provideMock, provideMockWithValues, Mock } from '../src/public_api';

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

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

concat() {
return this.foo + this.bar;
}
}

@Component({
Expand Down Expand Up @@ -39,6 +43,23 @@ test('provides a mock service', async () => {
expect(service.print).toHaveBeenCalledTimes(1);
});

test('provides a mock service with values', async () => {
await render(FixtureComponent, {
providers: [provideMockWithValues(FixtureService, {
bar: 'value',
concat: jest.fn(() => 'a concatenated value')
})],
});

const service = TestBed.inject(FixtureService);

fireEvent.click(screen.getByText('Print'));

expect(service.bar).toEqual('value');
expect(service.concat()).toEqual('a concatenated value');
expect(service.print).toHaveBeenCalled();
});

test('is possible to write a mock implementation', async () => {
await render(FixtureComponent, {
providers: [provideMock(FixtureService)],
Expand Down

0 comments on commit fa34bdd

Please sign in to comment.