From fa34bdd60132f598fe3a1f182a254a7f1b644d0d Mon Sep 17 00:00:00 2001 From: Michael Westphal Date: Thu, 15 Jul 2021 08:57:15 -0500 Subject: [PATCH] feat: add provideMockWithValues (#238) --- package.json | 20 ++++++++-------- projects/jest-utils/src/lib/create-mock.ts | 7 ++++++ projects/jest-utils/tests/create-mock.spec.ts | 23 ++++++++++++++++++- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 77734042..31a14a62 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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", diff --git a/projects/jest-utils/src/lib/create-mock.ts b/projects/jest-utils/src/lib/create-mock.ts index 7b9c1ecd..2796d972 100644 --- a/projects/jest-utils/src/lib/create-mock.ts +++ b/projects/jest-utils/src/lib/create-mock.ts @@ -45,3 +45,10 @@ export function provideMock(type: Type): Provider { useValue: createMock(type), }; } + +export function provideMockWithValues(type: Type, values: Partial>): Provider { + return { + provide: type, + useValue: createMockWithValues(type, values), + }; +} diff --git a/projects/jest-utils/tests/create-mock.spec.ts b/projects/jest-utils/tests/create-mock.spec.ts index 085739cd..ded7ab1c 100644 --- a/projects/jest-utils/tests/create-mock.spec.ts +++ b/projects/jest-utils/tests/create-mock.spec.ts @@ -2,7 +2,7 @@ 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) {} @@ -10,6 +10,10 @@ class FixtureService { print() { console.log(this.foo, this.bar); } + + concat() { + return this.foo + this.bar; + } } @Component({ @@ -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)],