Skip to content

Commit

Permalink
Merge branch 'main' into km/fix-badge-mv2
Browse files Browse the repository at this point in the history
  • Loading branch information
quexten authored Jan 15, 2025
2 parents 6deb347 + f6f4bc9 commit b4b7900
Show file tree
Hide file tree
Showing 29 changed files with 169 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@
"eslint-plugin-storybook",
"eslint-plugin-tailwindcss",
"husky",
"jest-extended",
"jest-junit",
"jest-mock-extended",
"jest-preset-angular",
"jest-diff",
"lint-staged",
"ts-jest"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class BackgroundBrowserBiometricsService extends BiometricsService {
if (await this.keyService.validateUserKey(userKey, userId)) {
await this.biometricStateService.setBiometricUnlockEnabled(true);
await this.biometricStateService.setFingerprintValidated(true);
this.keyService.setUserKey(userKey, userId);
await this.keyService.setUserKey(userKey, userId);
// to update badge and other things
this.messagingService.send("switchAccount", { userId });

Check warning on line 104 in apps/browser/src/key-management/biometrics/background-browser-biometrics.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/key-management/biometrics/background-browser-biometrics.service.ts#L104

Added line #L104 was not covered by tests
return userKey;
Expand All @@ -119,7 +119,7 @@ export class BackgroundBrowserBiometricsService extends BiometricsService {
if (await this.keyService.validateUserKey(userKey, userId)) {
await this.biometricStateService.setBiometricUnlockEnabled(true);
await this.biometricStateService.setFingerprintValidated(true);
this.keyService.setUserKey(userKey, userId);
await this.keyService.setUserKey(userKey, userId);
// to update badge and other things
this.messagingService.send("switchAccount", { userId });

Check warning on line 124 in apps/browser/src/key-management/biometrics/background-browser-biometrics.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/key-management/biometrics/background-browser-biometrics.service.ts#L124

Added line #L124 was not covered by tests
return userKey;
Expand Down
4 changes: 2 additions & 2 deletions apps/browser/src/popup/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ export class AppComponent implements OnInit, OnDestroy {
this.toastService._showToast(msg);
} else if (msg.command === "reloadProcess") {
if (this.platformUtilsService.isSafari()) {
window.setTimeout(() => {
this.biometricStateService.updateLastProcessReload();
window.setTimeout(async () => {
await this.biometricStateService.updateLastProcessReload();
window.location.reload();
}, 2000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe("VaultHeaderV2Component", () => {
it("defaults the initial state to true", (done) => {
// The initial value of the `state$` variable above is undefined
component["initialDisclosureVisibility$"].subscribe((initialVisibility) => {
expect(initialVisibility).toBeTrue();
expect(initialVisibility).toBe(true);
done();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,31 +179,31 @@ describe("ViewV2Component", () => {

flush(); // Resolve all promises

expect(doAutofill).toHaveBeenCalledOnce();
expect(doAutofill).toHaveBeenCalledTimes(1);
}));

it('invokes `copy` when action="copy-username"', fakeAsync(() => {
params$.next({ action: COPY_USERNAME_ID });

flush(); // Resolve all promises

expect(copy).toHaveBeenCalledOnce();
expect(copy).toHaveBeenCalledTimes(1);
}));

it('invokes `copy` when action="copy-password"', fakeAsync(() => {
params$.next({ action: COPY_PASSWORD_ID });

flush(); // Resolve all promises

expect(copy).toHaveBeenCalledOnce();
expect(copy).toHaveBeenCalledTimes(1);
}));

it('invokes `copy` when action="copy-totp"', fakeAsync(() => {
params$.next({ action: COPY_VERIFICATION_CODE_ID });

flush(); // Resolve all promises

expect(copy).toHaveBeenCalledOnce();
expect(copy).toHaveBeenCalledTimes(1);
}));

it("closes the popout after a load action", fakeAsync(() => {
Expand All @@ -218,9 +218,9 @@ describe("ViewV2Component", () => {

flush(); // Resolve all promises

expect(doAutofill).toHaveBeenCalledOnce();
expect(doAutofill).toHaveBeenCalledTimes(1);
expect(focusSpy).toHaveBeenCalledWith(99);
expect(closeSpy).toHaveBeenCalledOnce();
expect(closeSpy).toHaveBeenCalledTimes(1);
}));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,15 @@ describe("VaultPopupListFiltersService", () => {
state$.next(true);

service.filterVisibilityState$.subscribe((filterVisibility) => {
expect(filterVisibility).toBeTrue();
expect(filterVisibility).toBe(true);
done();
});
});

it("updates stored filter state", async () => {
await service.updateFilterVisibility(false);

expect(update).toHaveBeenCalledOnce();
expect(update).toHaveBeenCalledTimes(1);
// Get callback passed to `update`
const updateCallback = update.mock.calls[0][0];
expect(updateCallback()).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,11 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
}

private refreshSalesTax(): void {
if (!this.taxInformation.country || !this.taxInformation.postalCode) {
if (
this.taxInformation === undefined ||
!this.taxInformation.country ||
!this.taxInformation.postalCode
) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("DefaultvNextCollectionService", () => {

// Assert emitted values
expect(result.length).toBe(2);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{
id: collection1.id,
name: "DEC_NAME_" + collection1.id,
Expand Down Expand Up @@ -167,7 +167,7 @@ describe("DefaultvNextCollectionService", () => {
const result = await firstValueFrom(collectionService.encryptedCollections$(userId));

expect(result.length).toBe(2);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{
id: collection1.id,
name: makeEncString("ENC_NAME_" + collection1.id),
Expand Down Expand Up @@ -205,7 +205,7 @@ describe("DefaultvNextCollectionService", () => {

const result = await firstValueFrom(collectionService.encryptedCollections$(userId));
expect(result.length).toBe(3);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{
id: collection1.id,
name: makeEncString("UPDATED_ENC_NAME_" + collection1.id),
Expand All @@ -230,7 +230,7 @@ describe("DefaultvNextCollectionService", () => {

const result = await firstValueFrom(collectionService.encryptedCollections$(userId));
expect(result.length).toBe(1);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{
id: collection1.id,
name: makeEncString("ENC_NAME_" + collection1.id),
Expand All @@ -253,7 +253,7 @@ describe("DefaultvNextCollectionService", () => {

const result = await firstValueFrom(collectionService.encryptedCollections$(userId));
expect(result.length).toBe(1);
expect(result).toIncludeAllPartialMembers([
expect(result).toContainPartialObjects([
{
id: newCollection3.id,
name: makeEncString("ENC_NAME_" + newCollection3.id),
Expand Down
4 changes: 4 additions & 0 deletions libs/admin-console/test.setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { webcrypto } from "crypto";

import { addCustomMatchers } from "@bitwarden/common/spec";
import "jest-preset-angular/setup-jest";

addCustomMatchers();

Object.defineProperty(window, "CSS", { value: null });
Object.defineProperty(window, "getComputedStyle", {
value: () => {
Expand Down
2 changes: 1 addition & 1 deletion libs/admin-console/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"@bitwarden/key-management": ["../key-management/src"]
}
},
"include": ["src", "spec"],
"include": ["src", "spec", "../../libs/common/custom-matchers.d.ts"],
"exclude": ["node_modules", "dist"]
}
12 changes: 7 additions & 5 deletions libs/common/spec/matchers/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import * as matchers from "jest-extended";

import { toBeFulfilled, toBeResolved, toBeRejected } from "./promise-fulfilled";
import { toAlmostEqual } from "./to-almost-equal";
import { toContainPartialObjects } from "./to-contain-partial-objects";
import { toEqualBuffer } from "./to-equal-buffer";

export * from "./to-equal-buffer";
export * from "./to-almost-equal";
export * from "./promise-fulfilled";

// add all jest-extended matchers
expect.extend(matchers);

export function addCustomMatchers() {
expect.extend({
toEqualBuffer: toEqualBuffer,
toAlmostEqual: toAlmostEqual,
toBeFulfilled: toBeFulfilled,
toBeResolved: toBeResolved,
toBeRejected: toBeRejected,
toContainPartialObjects,
});
}

Expand Down Expand Up @@ -59,4 +56,9 @@ export interface CustomMatchers<R = unknown> {
* @returns CustomMatcherResult indicating whether or not the test passed
*/
toBeRejected(withinMs?: number): Promise<R>;
/**
* Matches if the received array contains all the expected objects using partial matching (expect.objectContaining).
* @param expected An array of partial objects that should be contained in the received array.
*/
toContainPartialObjects<T>(expected: Array<T>): R;
}
77 changes: 77 additions & 0 deletions libs/common/spec/matchers/to-contain-partial-objects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
describe("toContainPartialObjects", () => {
describe("matches", () => {
it("if the array only contains the partial objects", () => {
const actual = [
{
id: 1,
name: "foo",
},
{
id: 2,
name: "bar",
},
];

const expected = [{ id: 1 }, { id: 2 }];

expect(actual).toContainPartialObjects(expected);
});

it("if the array contains the partial objects and other objects", () => {
const actual = [
{
id: 1,
name: "foo",
},
{
id: 2,
name: "bar",
},
{
id: 3,
name: "baz",
},
];

const expected = [{ id: 1 }, { id: 2 }];

expect(actual).toContainPartialObjects(expected);
});
});

describe("doesn't match", () => {
it("if the array does not contain any partial objects", () => {
const actual = [
{
id: 1,
name: "foo",
},
{
id: 2,
name: "bar",
},
];

const expected = [{ id: 1, name: "Foo" }];

expect(actual).not.toContainPartialObjects(expected);
});

it("if the array contains some but not all partial objects", () => {
const actual = [
{
id: 1,
name: "foo",
},
{
id: 2,
name: "bar",
},
];

const expected = [{ id: 2 }, { id: 3 }];

expect(actual).not.toContainPartialObjects(expected);
});
});
});
31 changes: 31 additions & 0 deletions libs/common/spec/matchers/to-contain-partial-objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { EOL } from "os";

import { diff } from "jest-diff";

export const toContainPartialObjects: jest.CustomMatcher = function (
received: Array<any>,
expected: Array<any>,
) {
const matched = this.equals(
received,
expect.arrayContaining(expected.map((e) => expect.objectContaining(e))),
);

if (matched) {
return {
message: () =>
"Expected the received array NOT to include partial matches for all expected objects." +
EOL +
diff(expected, received),
pass: true,
};
}

return {
message: () =>
"Expected the received array to contain partial matches for all expected objects." +
EOL +
diff(expected, received),
pass: false,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe("KeyServiceLegacyEncryptorProvider", () => {

singleUserId$.complete();

expect(completed).toBeTrue();
expect(completed).toBe(true);
});

it("completes when `userKey$` emits a falsy value after emitting a truthy value", () => {
Expand All @@ -199,7 +199,7 @@ describe("KeyServiceLegacyEncryptorProvider", () => {

userKey$.next(null);

expect(completed).toBeTrue();
expect(completed).toBe(true);
});

it("completes once `dependencies.singleUserId$` emits and `userKey$` completes", () => {
Expand All @@ -214,7 +214,7 @@ describe("KeyServiceLegacyEncryptorProvider", () => {

userKey$.complete();

expect(completed).toBeTrue();
expect(completed).toBe(true);
});
});

Expand Down Expand Up @@ -445,7 +445,7 @@ describe("KeyServiceLegacyEncryptorProvider", () => {

singleOrganizationId$.complete();

expect(completed).toBeTrue();
expect(completed).toBe(true);
});

it("completes when `orgKeys$` emits a falsy value after emitting a truthy value", () => {
Expand All @@ -466,7 +466,7 @@ describe("KeyServiceLegacyEncryptorProvider", () => {
orgKey$.next(OrgRecords);
orgKey$.next(null);

expect(completed).toBeTrue();
expect(completed).toBe(true);
});

it("completes once `dependencies.singleOrganizationId$` emits and `userKey$` completes", () => {
Expand All @@ -486,7 +486,7 @@ describe("KeyServiceLegacyEncryptorProvider", () => {

orgKey$.complete();

expect(completed).toBeTrue();
expect(completed).toBe(true);
});
});
});
Loading

0 comments on commit b4b7900

Please sign in to comment.