From cda281419236425fda2c973739185767c9256487 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 21 Apr 2018 12:57:29 -0400 Subject: [PATCH] typemock lib for mocking services --- package-lock.json | 23 ++++++++++++ package.json | 1 + .../webCryptoFunction.service.spec.ts | 36 ++++--------------- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7b80b28d2e..0517016707 100644 --- a/package-lock.json +++ b/package-lock.json @@ -979,6 +979,12 @@ } } }, + "circular-json": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/circular-json/-/circular-json-0.3.3.tgz", + "integrity": "sha512-UZK3NBx2Mca+b5LsG7bY183pHWt5Y1xts4P3Pz7ENTwGVnJOUWbRb3ocjvX7hx9tq/yTAdclXm9sZ38gNuem4A==", + "dev": true + }, "class-utils": { "version": "0.3.6", "resolved": "https://registry.npmjs.org/class-utils/-/class-utils-0.3.6.tgz", @@ -6055,6 +6061,12 @@ "integrity": "sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=", "dev": true }, + "postinstall-build": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/postinstall-build/-/postinstall-build-5.0.1.tgz", + "integrity": "sha1-uRepB5smF42aJK9aXNjLSpkdEbk=", + "dev": true + }, "prelude-ls": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz", @@ -7828,6 +7840,17 @@ "mime-types": "2.1.18" } }, + "typemoq": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/typemoq/-/typemoq-2.1.0.tgz", + "integrity": "sha512-DtRNLb7x8yCTv/KHlwes+NI+aGb4Vl1iPC63Hhtcvk1DpxSAZzKWQv0RQFY0jX2Uqj0SDBNl8Na4e6MV6TNDgw==", + "dev": true, + "requires": { + "circular-json": "0.3.3", + "lodash": "4.17.4", + "postinstall-build": "5.0.1" + } + }, "typescript": { "version": "2.7.1", "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.7.1.tgz", diff --git a/package.json b/package.json index 856670e1ac..ea2447ebee 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "nodemon": "^1.17.3", "rimraf": "^2.6.2", "tslint": "^5.8.0", + "typemoq": "^2.1.0", "typescript": "^2.7.1" }, "dependencies": { diff --git a/spec/web/services/webCryptoFunction.service.spec.ts b/spec/web/services/webCryptoFunction.service.spec.ts index f41698ed58..386a56aebd 100644 --- a/spec/web/services/webCryptoFunction.service.spec.ts +++ b/spec/web/services/webCryptoFunction.service.spec.ts @@ -1,3 +1,5 @@ +import * as TypeMoq from 'typemoq'; + import { DeviceType } from '../../../src/enums/deviceType'; import { PlatformUtilsService } from '../../../src/abstractions/platformUtils.service'; @@ -265,8 +267,9 @@ function testHmac(edge: boolean, algorithm: 'sha1' | 'sha256' | 'sha512', mac: s } function getWebCryptoFunctionService(edge = false) { - const platformUtilsService = new BrowserPlatformUtilsService(edge); - return new WebCryptoFunctionService(window, platformUtilsService); + const platformUtilsMock = TypeMoq.Mock.ofType(PlatformUtilsServiceMock); + platformUtilsMock.setup((x) => x.isEdge()).returns(() => edge); + return new WebCryptoFunctionService(window, platformUtilsMock.object); } function makeStaticByteArray(length: number) { @@ -277,31 +280,6 @@ function makeStaticByteArray(length: number) { return arr; } -class BrowserPlatformUtilsService implements PlatformUtilsService { - identityClientId: string; - getDevice: () => DeviceType; - getDeviceString: () => string; - isFirefox: () => boolean; - isChrome: () => boolean; - isOpera: () => boolean; - isVivaldi: () => boolean; - isSafari: () => boolean; - isMacAppStore: () => boolean; - analyticsId: () => string; - getDomain: (uriString: string) => string; - isViewOpen: () => boolean; - launchUri: (uri: string, options?: any) => void; - saveFile: (win: Window, blobData: any, blobOptions: any, fileName: string) => void; - getApplicationVersion: () => string; - supportsU2f: (win: Window) => boolean; - showDialog: (text: string, title?: string, confirmText?: string, cancelText?: string, - type?: string) => Promise; - isDev: () => boolean; - copyToClipboard: (text: string, options?: any) => void; - - constructor(private edge: boolean) { } - - isEdge() { - return this.edge; - } +class PlatformUtilsServiceMock extends PlatformUtilsService { + isEdge = () => false; }