From ed78f7e7cb2120bff7acd828201f4d06c09dee71 Mon Sep 17 00:00:00 2001 From: Robyn MacCallum Date: Thu, 15 Sep 2022 12:31:15 -0400 Subject: [PATCH] [SG-397] Configure jest for Desktop and add simple tests (#3467) * Configure jest for Desktop and add simple tests * Remove Jest from tsconfig types --- apps/desktop/jest.config.js | 14 ++++ apps/desktop/package.json | 5 +- .../src/app/vault/generator.component.spec.ts | 77 +++++++++++++++++++ apps/desktop/test.setup.ts | 23 ++++++ apps/desktop/tsconfig.spec.json | 4 + jest.config.js | 1 + 6 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 apps/desktop/jest.config.js create mode 100644 apps/desktop/src/app/vault/generator.component.spec.ts create mode 100644 apps/desktop/test.setup.ts create mode 100644 apps/desktop/tsconfig.spec.json diff --git a/apps/desktop/jest.config.js b/apps/desktop/jest.config.js new file mode 100644 index 0000000000..4f954afa9e --- /dev/null +++ b/apps/desktop/jest.config.js @@ -0,0 +1,14 @@ +const { pathsToModuleNameMapper } = require("ts-jest"); + +const { compilerOptions } = require("./tsconfig"); + +const sharedConfig = require("../../libs/shared/jest.config.base"); + +module.exports = { + ...sharedConfig, + preset: "jest-preset-angular", + setupFilesAfterEnv: ["/test.setup.ts"], + moduleNameMapper: pathsToModuleNameMapper(compilerOptions?.paths || {}, { + prefix: "/", + }), +}; diff --git a/apps/desktop/package.json b/apps/desktop/package.json index 58b31e7dbf..51ebdbd916 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -48,6 +48,9 @@ "publish:mac:mas": "npm run dist:mac:mas && npm run upload:mas", "publish:win": "npm run build && npm run clean:dist && electron-builder --win --x64 --arm64 --ia32 -p always -c.win.certificateSubjectName=\"8bit Solutions LLC\"", "publish:win:dev": "npm run build && npm run clean:dist && electron-builder --win --x64 --arm64 --ia32 -p always", - "upload:mas": "xcrun altool --upload-app --type osx --file \"$(find ./dist/mas-universal/Bitwarden*.pkg)\" --username $APPLE_ID_USERNAME --password $APPLE_ID_PASSWORD" + "upload:mas": "xcrun altool --upload-app --type osx --file \"$(find ./dist/mas-universal/Bitwarden*.pkg)\" --username $APPLE_ID_USERNAME --password $APPLE_ID_PASSWORD", + "test": "jest", + "test:watch": "jest --watch", + "test:watch:all": "jest --watchAll" } } diff --git a/apps/desktop/src/app/vault/generator.component.spec.ts b/apps/desktop/src/app/vault/generator.component.spec.ts new file mode 100644 index 0000000000..6c906e9b36 --- /dev/null +++ b/apps/desktop/src/app/vault/generator.component.spec.ts @@ -0,0 +1,77 @@ +import { NO_ERRORS_SCHEMA } from "@angular/core"; +import { ComponentFixture, TestBed } from "@angular/core/testing"; +import { ActivatedRoute } from "@angular/router"; +import { Substitute } from "@fluffy-spoon/substitute"; +import { mock, MockProxy } from "jest-mock-extended"; + +import { I18nPipe } from "@bitwarden/angular/pipes/i18n.pipe"; +import { I18nService } from "@bitwarden/common/abstractions/i18n.service"; +import { LogService } from "@bitwarden/common/abstractions/log.service"; +import { PasswordGenerationService } from "@bitwarden/common/abstractions/passwordGeneration.service"; +import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service"; +import { StateService } from "@bitwarden/common/abstractions/state.service"; +import { UsernameGenerationService } from "@bitwarden/common/abstractions/usernameGeneration.service"; + +import { GeneratorComponent } from "./generator.component"; + +describe("GeneratorComponent", () => { + let component: GeneratorComponent; + let fixture: ComponentFixture; + let platformUtilsServiceMock: MockProxy; + + beforeEach(() => { + platformUtilsServiceMock = mock(); + + TestBed.configureTestingModule({ + declarations: [GeneratorComponent, I18nPipe], + providers: [ + { + provide: PasswordGenerationService, + useClass: Substitute.for(), + }, + { + provide: UsernameGenerationService, + useClass: Substitute.for(), + }, + { + provide: StateService, + useClass: Substitute.for(), + }, + { + provide: PlatformUtilsService, + useValue: platformUtilsServiceMock, + }, + { + provide: I18nService, + useClass: Substitute.for(), + }, + { + provide: ActivatedRoute, + useClass: Substitute.for(), + }, + { + provide: LogService, + useClass: Substitute.for(), + }, + ], + schemas: [NO_ERRORS_SCHEMA], + }).compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(GeneratorComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it("should create", () => { + expect(component).toBeTruthy(); + }); + + describe("usernameTypesLearnMore()", () => { + it("should call platformUtilsService.launchUri() once", () => { + component.usernameTypesLearnMore(); + expect(platformUtilsServiceMock.launchUri).toHaveBeenCalledTimes(1); + }); + }); +}); diff --git a/apps/desktop/test.setup.ts b/apps/desktop/test.setup.ts new file mode 100644 index 0000000000..224262ec83 --- /dev/null +++ b/apps/desktop/test.setup.ts @@ -0,0 +1,23 @@ +import "jest-preset-angular/setup-jest"; + +Object.defineProperty(window, "CSS", { value: null }); +Object.defineProperty(window, "getComputedStyle", { + value: () => { + return { + display: "none", + appearance: ["-webkit-appearance"], + }; + }, +}); + +Object.defineProperty(document, "doctype", { + value: "", +}); +Object.defineProperty(document.body.style, "transform", { + value: () => { + return { + enumerable: true, + configurable: true, + }; + }, +}); diff --git a/apps/desktop/tsconfig.spec.json b/apps/desktop/tsconfig.spec.json new file mode 100644 index 0000000000..de184bd760 --- /dev/null +++ b/apps/desktop/tsconfig.spec.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "files": ["./test.setup.ts"] +} diff --git a/jest.config.js b/jest.config.js index 7a747974f5..535c9e00a3 100644 --- a/jest.config.js +++ b/jest.config.js @@ -12,6 +12,7 @@ module.exports = { projects: [ "/apps/browser/jest.config.js", "/apps/cli/jest.config.js", + "/apps/desktop/jest.config.js", "/apps/web/jest.config.js", "/bitwarden_license/bit-web/jest.config.js",