migrate platform toasts to CL toastService (#10666)

This commit is contained in:
Jordan Aasen 2024-08-22 10:12:27 -07:00 committed by GitHub
parent d2d49b254d
commit 898338ff9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 14 deletions

View File

@ -8,8 +8,7 @@ import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { I18nMockService } from "@bitwarden/components/src";
import { I18nMockService, ToastService } from "@bitwarden/components/src";
import { canAccessFeature } from "./feature-flag.guard";
@ -22,11 +21,11 @@ describe("canAccessFeature", () => {
const redirectRoute = "redirect";
let mockConfigService: MockProxy<ConfigService>;
let mockPlatformUtilsService: MockProxy<PlatformUtilsService>;
let mockToastService: MockProxy<ToastService>;
const setup = (featureGuard: CanActivateFn, flagValue: any) => {
mockConfigService = mock<ConfigService>();
mockPlatformUtilsService = mock<PlatformUtilsService>();
mockToastService = mock<ToastService>();
// Mock the correct getter based on the type of flagValue; also mock default values if one is not provided
if (typeof flagValue === "boolean") {
@ -57,7 +56,7 @@ describe("canAccessFeature", () => {
],
providers: [
{ provide: ConfigService, useValue: mockConfigService },
{ provide: PlatformUtilsService, useValue: mockPlatformUtilsService },
{ provide: ToastService, useValue: mockToastService },
{ provide: LogService, useValue: mock<LogService>() },
{
provide: I18nService,
@ -117,11 +116,11 @@ describe("canAccessFeature", () => {
await router.navigate([featureRoute]);
expect(mockPlatformUtilsService.showToast).toHaveBeenCalledWith(
"error",
null,
"Access Denied!",
);
expect(mockToastService.showToast).toHaveBeenCalledWith({
variant: "error",
title: null,
message: "Access Denied!",
});
});
it("does not show an error toast when the feature flag is enabled", async () => {
@ -129,7 +128,7 @@ describe("canAccessFeature", () => {
await router.navigate([featureRoute]);
expect(mockPlatformUtilsService.showToast).not.toHaveBeenCalled();
expect(mockToastService.showToast).not.toHaveBeenCalled();
});
it("redirects to the specified redirect url when the feature flag is disabled", async () => {

View File

@ -5,7 +5,7 @@ import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { ToastService } from "@bitwarden/components";
// Replace this with a type safe lookup of the feature flag values in PM-2282
type FlagValue = boolean | number | string;
@ -24,7 +24,7 @@ export const canAccessFeature = (
): CanActivateFn => {
return async () => {
const configService = inject(ConfigService);
const platformUtilsService = inject(PlatformUtilsService);
const toastService = inject(ToastService);
const router = inject(Router);
const i18nService = inject(I18nService);
const logService = inject(LogService);
@ -36,7 +36,11 @@ export const canAccessFeature = (
return true;
}
platformUtilsService.showToast("error", null, i18nService.t("accessDenied"));
toastService.showToast({
variant: "error",
title: null,
message: i18nService.t("accessDenied"),
});
if (redirectUrlOnDisabled != null) {
return router.createUrlTree([redirectUrlOnDisabled]);