mirror of
https://github.com/bitwarden/jslib
synced 2025-12-14 07:13:35 +00:00
Showcase how jest-mock-extended works
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
|
import { any, mock, mockDeep, MockProxy } from "jest-mock-extended";
|
||||||
|
|
||||||
import { ApiService } from "jslib-common/abstractions/api.service";
|
|
||||||
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
||||||
import { FileUploadService } from "jslib-common/abstractions/fileUpload.service";
|
import { FileUploadService } from "jslib-common/abstractions/fileUpload.service";
|
||||||
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
||||||
@@ -13,35 +12,37 @@ import { Cipher } from "jslib-common/models/domain/cipher";
|
|||||||
import { EncArrayBuffer } from "jslib-common/models/domain/encArrayBuffer";
|
import { EncArrayBuffer } from "jslib-common/models/domain/encArrayBuffer";
|
||||||
import { EncString } from "jslib-common/models/domain/encString";
|
import { EncString } from "jslib-common/models/domain/encString";
|
||||||
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
|
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
|
||||||
|
import { ApiService } from "jslib-common/services/api.service";
|
||||||
import { CipherService } from "jslib-common/services/cipher.service";
|
import { CipherService } from "jslib-common/services/cipher.service";
|
||||||
|
|
||||||
const ENCRYPTED_TEXT = "This data has been encrypted";
|
const ENCRYPTED_TEXT = "This data has been encrypted";
|
||||||
const ENCRYPTED_BYTES = new EncArrayBuffer(Utils.fromUtf8ToArray(ENCRYPTED_TEXT).buffer);
|
const ENCRYPTED_BYTES = new EncArrayBuffer(Utils.fromUtf8ToArray(ENCRYPTED_TEXT).buffer);
|
||||||
|
|
||||||
describe("Cipher Service", () => {
|
describe("Cipher Service", () => {
|
||||||
let cryptoService: SubstituteOf<CryptoService>;
|
let cryptoService: MockProxy<CryptoService>;
|
||||||
let stateService: SubstituteOf<StateService>;
|
let stateService: MockProxy<StateService>;
|
||||||
let settingsService: SubstituteOf<SettingsService>;
|
let settingsService: MockProxy<SettingsService>;
|
||||||
let apiService: SubstituteOf<ApiService>;
|
let apiService: MockProxy<ApiService>;
|
||||||
let fileUploadService: SubstituteOf<FileUploadService>;
|
let fileUploadService: MockProxy<FileUploadService>;
|
||||||
let i18nService: SubstituteOf<I18nService>;
|
let i18nService: MockProxy<I18nService>;
|
||||||
let searchService: SubstituteOf<SearchService>;
|
let searchService: MockProxy<SearchService>;
|
||||||
let logService: SubstituteOf<LogService>;
|
let logService: MockProxy<LogService>;
|
||||||
|
|
||||||
let cipherService: CipherService;
|
let cipherService: CipherService;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
cryptoService = Substitute.for<CryptoService>();
|
cryptoService = mock<CryptoService>();
|
||||||
stateService = Substitute.for<StateService>();
|
stateService = mock<StateService>();
|
||||||
settingsService = Substitute.for<SettingsService>();
|
settingsService = mock<SettingsService>();
|
||||||
apiService = Substitute.for<ApiService>();
|
apiService = mock<ApiService>();
|
||||||
fileUploadService = Substitute.for<FileUploadService>();
|
fileUploadService = mock<FileUploadService>();
|
||||||
i18nService = Substitute.for<I18nService>();
|
i18nService = mock<I18nService>();
|
||||||
searchService = Substitute.for<SearchService>();
|
searchService = mock<SearchService>();
|
||||||
logService = Substitute.for<LogService>();
|
logService = mock<LogService>();
|
||||||
|
|
||||||
cryptoService.encryptToBytes(Arg.any(), Arg.any()).resolves(ENCRYPTED_BYTES);
|
cryptoService.makeEncKey.mockResolvedValue([jest.fn(), jest.fn()] as any);
|
||||||
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(new EncString(ENCRYPTED_TEXT));
|
cryptoService.encryptToBytes.mockResolvedValue(ENCRYPTED_BYTES);
|
||||||
|
cryptoService.encrypt.mockResolvedValue(new EncString(ENCRYPTED_TEXT));
|
||||||
|
|
||||||
cipherService = new CipherService(
|
cipherService = new CipherService(
|
||||||
cryptoService,
|
cryptoService,
|
||||||
@@ -58,12 +59,17 @@ describe("Cipher Service", () => {
|
|||||||
it("attachments upload encrypted file contents", async () => {
|
it("attachments upload encrypted file contents", async () => {
|
||||||
const fileName = "filename";
|
const fileName = "filename";
|
||||||
const fileData = new Uint8Array(10).buffer;
|
const fileData = new Uint8Array(10).buffer;
|
||||||
cryptoService.getOrgKey(Arg.any()).resolves(new SymmetricCryptoKey(new Uint8Array(32).buffer));
|
cryptoService.getOrgKey.mockResolvedValue(new SymmetricCryptoKey(new Uint8Array(32).buffer));
|
||||||
|
apiService.postCipherAttachment.mockResolvedValue("123" as any);
|
||||||
|
|
||||||
await cipherService.saveAttachmentRawWithServer(new Cipher(), fileName, fileData);
|
await cipherService.saveAttachmentRawWithServer(new Cipher(), fileName, fileData);
|
||||||
|
|
||||||
fileUploadService
|
expect(fileUploadService.uploadCipherAttachment).toHaveBeenCalledTimes(1);
|
||||||
.received(1)
|
expect(fileUploadService.uploadCipherAttachment).toHaveBeenCalledWith(
|
||||||
.uploadCipherAttachment(Arg.any(), Arg.any(), new EncString(ENCRYPTED_TEXT), ENCRYPTED_BYTES);
|
any(),
|
||||||
|
any(),
|
||||||
|
new EncString(ENCRYPTED_TEXT),
|
||||||
|
ENCRYPTED_BYTES
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
24532
package-lock.json
generated
24532
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -23,6 +23,8 @@
|
|||||||
"prepare": "husky install"
|
"prepare": "husky install"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@angular-devkit/build-angular": "^12.2.13",
|
||||||
|
"@angular/platform-browser-dynamic": "^12.2.13",
|
||||||
"@fluffy-spoon/substitute": "^1.202.0",
|
"@fluffy-spoon/substitute": "^1.202.0",
|
||||||
"@types/jest": "^27.4.1",
|
"@types/jest": "^27.4.1",
|
||||||
"@types/node": "^16.11.12",
|
"@types/node": "^16.11.12",
|
||||||
@@ -37,6 +39,7 @@
|
|||||||
"form-data": "4.0.0",
|
"form-data": "4.0.0",
|
||||||
"husky": "^7.0.4",
|
"husky": "^7.0.4",
|
||||||
"jest": "^27.5.1",
|
"jest": "^27.5.1",
|
||||||
|
"jest-mock-extended": "^2.0.4",
|
||||||
"jest-preset-angular": "^11.1.1",
|
"jest-preset-angular": "^11.1.1",
|
||||||
"jsdom": "^16.5.3",
|
"jsdom": "^16.5.3",
|
||||||
"lint-staged": "^12.1.2",
|
"lint-staged": "^12.1.2",
|
||||||
@@ -51,7 +54,7 @@
|
|||||||
"typemoq": "^2.1.0",
|
"typemoq": "^2.1.0",
|
||||||
"typescript": "4.3.5",
|
"typescript": "4.3.5",
|
||||||
"typescript-transform-paths": "^2.2.3",
|
"typescript-transform-paths": "^2.2.3",
|
||||||
"webpack": "^4.46.0"
|
"zone.js": "0.11.4"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@bitwarden/jslib-angular": "file:angular",
|
"@bitwarden/jslib-angular": "file:angular",
|
||||||
|
|||||||
Reference in New Issue
Block a user