mirror of
https://github.com/bitwarden/clients.git
synced 2026-11-16 07:03:29 +00:00
Pin story dates and share the story clock helpers (#24424)
* Pin the custom range dialog stories to a fixed date The stories built their bounds from the real clock, so Chromatic diffed them every day. Build them from STORY_NOW and start the dialog's clock there, like the other PAM stories. Add a Storybook rule covering fixed dates in stories. * Allow liveFromNow in the Storybook dates rule Its blanket ban on Date.now() contradicted the liveFromNow helper the same rule recommends. * Move the story clock helpers to @bitwarden/storybook STORY_NOW, fromNow, liveFromNow, startClockAtStoryNow and the duration constants lived in PAM's story fixtures, so other teams' stories couldn't use them. Move them to the shared Storybook lib, point the PAM stories and the dates rule at it, and replace decide-dialog's local copy of the fixed date.
This commit is contained in:
32
.claude/rules/storybook-dates.md
Normal file
32
.claude/rules/storybook-dates.md
Normal file
@@ -0,0 +1,32 @@
|
||||
---
|
||||
paths:
|
||||
- "**/*.stories.ts"
|
||||
---
|
||||
|
||||
# Storybook: stories that show dates
|
||||
|
||||
Chromatic flags anything built from the real clock as a diff on every run.
|
||||
|
||||
- Build story dates from a fixed constant, never bare `Date.now()` or `new Date()`:
|
||||
|
||||
```ts
|
||||
import { DAY, STORY_NOW } from "@bitwarden/storybook";
|
||||
|
||||
const daysAgo = (days: number) => new Date(STORY_NOW.getTime() - days * DAY);
|
||||
```
|
||||
|
||||
The exception is a component that ticks its own clock. Build its offsets with `liveFromNow`
|
||||
inside a provider factory, so they're read at render time, after `startClockAtStoryNow` has moved
|
||||
the clock.
|
||||
|
||||
- If the component reads `Date.now()` itself (history windows, countdowns, "x minutes ago"), set
|
||||
`beforeEach: startClockAtStoryNow` on the default `Meta` export. It moves the clock without
|
||||
freezing it, because countdowns and rxjs debounces stall on a frozen clock.
|
||||
- You still need the constant after moving the clock. `args` and decorators are evaluated when the
|
||||
module loads, before `beforeEach` runs, so `Date.now()` there still reads the real time.
|
||||
- Moving the clock doesn't change `new Date()` with no arguments, so output built from it still
|
||||
drifts.
|
||||
- Relative output ("2h left") from `Date.now() + offset` is stable and needs neither.
|
||||
|
||||
Import these helpers (`STORY_NOW`, `fromNow`, `liveFromNow`, `startClockAtStoryNow`, `MINUTE`,
|
||||
`HOUR`, `DAY`) from `@bitwarden/storybook`.
|
||||
@@ -10,18 +10,20 @@ import { AccountService } from "@bitwarden/common/auth/abstractions/account.serv
|
||||
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
|
||||
import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service";
|
||||
import { DialogService } from "@bitwarden/components";
|
||||
import { enabledFlags } from "@bitwarden/storybook";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import { AccessNameResolverService } from "../access-requests/access-name-resolver.service";
|
||||
import {
|
||||
enabledFlags,
|
||||
DAY,
|
||||
HOUR,
|
||||
MINUTE,
|
||||
fromNow,
|
||||
startClockAtStoryNow,
|
||||
} from "@bitwarden/storybook";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import { AccessNameResolverService } from "../access-requests/access-name-resolver.service";
|
||||
import {
|
||||
provideStoryChangeDetection,
|
||||
provideStoryLogService,
|
||||
startClockAtStoryNow,
|
||||
storyNames,
|
||||
} from "../testing/story-fixtures";
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { importProvidersFrom } from "@angular/core";
|
||||
import { Meta, StoryObj, applicationConfig, moduleMetadata } from "@storybook/angular";
|
||||
|
||||
import { DIALOG_DATA, DialogRef } from "@bitwarden/components";
|
||||
import { DAY, STORY_NOW, startClockAtStoryNow } from "@bitwarden/storybook";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import { AUDIT_HISTORY_DAYS, auditRangeValue } from "../access-audit-row";
|
||||
@@ -11,11 +12,9 @@ import {
|
||||
CustomRangeDialogParams,
|
||||
} from "./custom-range-dialog.component";
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
/** Relative to now, since the dialog judges the history window against the clock. */
|
||||
/** Relative to {@link STORY_NOW}, which the dialog's clock starts at. */
|
||||
function daysAgo(days: number): string {
|
||||
return auditRangeValue(new Date(Date.now() - days * DAY_MS));
|
||||
return auditRangeValue(new Date(STORY_NOW.getTime() - days * DAY));
|
||||
}
|
||||
|
||||
/** Provided per story, since the dialog reads `DIALOG_DATA` once at construction. */
|
||||
@@ -32,6 +31,7 @@ function withParams(params: CustomRangeDialogParams) {
|
||||
export default {
|
||||
title: "Web/PAM/Access Audit/Custom Range Dialog",
|
||||
component: CustomRangeDialogComponent,
|
||||
beforeEach: startClockAtStoryNow,
|
||||
decorators: [
|
||||
applicationConfig({
|
||||
providers: [importProvidersFrom(PreloadedEnglishI18nModule)],
|
||||
|
||||
@@ -7,20 +7,15 @@ import { DomainSettingsService } from "@bitwarden/common/autofill/services/domai
|
||||
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
|
||||
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
|
||||
import { DIALOG_DATA, DialogRef, DialogService, ToastService } from "@bitwarden/components";
|
||||
import { DAY, HOUR, MINUTE, fromNow, startClockAtStoryNow, STORY_NOW } from "@bitwarden/storybook";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import { ApprovalRow, toApprovalRow } from "../../approvals/approval-row";
|
||||
import {
|
||||
DAY,
|
||||
HOUR,
|
||||
MINUTE,
|
||||
accessRequest,
|
||||
decision,
|
||||
fromNow,
|
||||
provideStoryChangeDetection,
|
||||
provideStoryLogService,
|
||||
startClockAtStoryNow,
|
||||
STORY_NOW,
|
||||
storyNames,
|
||||
} from "../../testing/story-fixtures";
|
||||
|
||||
|
||||
@@ -9,20 +9,16 @@ import { DomainSettingsService } from "@bitwarden/common/autofill/services/domai
|
||||
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
|
||||
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
|
||||
import { DialogService, ToastService } from "@bitwarden/components";
|
||||
import { HOUR, MINUTE, STORY_NOW, fromNow, startClockAtStoryNow } from "@bitwarden/storybook";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import { ApprovalRow, toApprovalRow } from "../approvals/approval-row";
|
||||
import { ApproverInboxService } from "../approvals/approver-inbox.service";
|
||||
import { ManagedLeaseRow, toManagedLeaseRow } from "../approvals/managed-lease-row";
|
||||
import {
|
||||
HOUR,
|
||||
MINUTE,
|
||||
STORY_NOW,
|
||||
accessRequest,
|
||||
fromNow,
|
||||
provideStoryChangeDetection,
|
||||
provideStoryLogService,
|
||||
startClockAtStoryNow,
|
||||
storyNames,
|
||||
} from "../testing/story-fixtures";
|
||||
|
||||
|
||||
@@ -9,20 +9,16 @@ import { ConfigService } from "@bitwarden/common/platform/abstractions/config/co
|
||||
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
|
||||
import { SyncService } from "@bitwarden/common/platform/sync";
|
||||
import { DialogService, ToastService } from "@bitwarden/components";
|
||||
import { DAY, HOUR, MINUTE, liveFromNow, startClockAtStoryNow } from "@bitwarden/storybook";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import { ApprovalPrivilegeService } from "../approvals/approval-privilege.service";
|
||||
import { ApproverInboxService } from "../approvals/approver-inbox.service";
|
||||
import {
|
||||
DAY,
|
||||
HOUR,
|
||||
MINUTE,
|
||||
accessRequest,
|
||||
decision,
|
||||
liveFromNow,
|
||||
provideStoryChangeDetection,
|
||||
provideStoryLogService,
|
||||
startClockAtStoryNow,
|
||||
storyNames,
|
||||
} from "../testing/story-fixtures";
|
||||
|
||||
|
||||
@@ -8,18 +8,14 @@ import { DomainSettingsService } from "@bitwarden/common/autofill/services/domai
|
||||
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
|
||||
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
|
||||
import { DialogService, ToastService } from "@bitwarden/components";
|
||||
import { DAY, HOUR, MINUTE, liveFromNow, startClockAtStoryNow } from "@bitwarden/storybook";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import {
|
||||
DAY,
|
||||
HOUR,
|
||||
MINUTE,
|
||||
accessLease,
|
||||
accessRequest,
|
||||
liveFromNow,
|
||||
provideStoryChangeDetection,
|
||||
provideStoryLogService,
|
||||
startClockAtStoryNow,
|
||||
storyNames,
|
||||
} from "../testing/story-fixtures";
|
||||
|
||||
|
||||
@@ -11,10 +11,10 @@ import {
|
||||
} from "@bitwarden/bit-pam";
|
||||
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
||||
import { DialogService, ToastService } from "@bitwarden/components";
|
||||
import { startClockAtStoryNow } from "@bitwarden/storybook";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import { GovernedCollectionsService } from "../services/governed-collections.service";
|
||||
import { startClockAtStoryNow } from "../testing/story-fixtures";
|
||||
|
||||
import { AccessRulesComponent } from "./access-rules.component";
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Meta, StoryObj, applicationConfig, moduleMetadata } from "@storybook/an
|
||||
|
||||
import type { AccessRequestView } from "@bitwarden/bit-pam";
|
||||
import { DIALOG_DATA, DialogRef } from "@bitwarden/components";
|
||||
import { STORY_NOW } from "@bitwarden/storybook";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import { emptyResolvedNames } from "../../access-requests/access-name-resolver.service";
|
||||
@@ -10,9 +11,6 @@ import { ApprovalRow, toApprovalRow } from "../approval-row";
|
||||
|
||||
import { DecideDialogComponent, DecideDialogParams } from "./decide-dialog.component";
|
||||
|
||||
/** Fixed so the window and "submitted N ago" labels don't drift with the clock. */
|
||||
const NOW = new Date("2026-08-17T12:00:00.000Z");
|
||||
|
||||
/** Built through `toApprovalRow` so the summary matches the inbox row's precomputed labels. */
|
||||
function approvalRow(
|
||||
overrides: Record<string, unknown> = {},
|
||||
@@ -44,7 +42,7 @@ function approvalRow(
|
||||
collectionNameById: collectionName ? new Map([["col-1", collectionName]]) : new Map(),
|
||||
organizationNameById: organizationName ? new Map([["org-1", organizationName]]) : new Map(),
|
||||
},
|
||||
NOW,
|
||||
STORY_NOW,
|
||||
true,
|
||||
"en-US",
|
||||
);
|
||||
|
||||
@@ -20,17 +20,14 @@ import { ConfigService } from "@bitwarden/common/platform/abstractions/config/co
|
||||
import { RevealedCipherService } from "@bitwarden/common/vault/abstractions/revealed-cipher.service";
|
||||
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
|
||||
import { DialogService, ToastService } from "@bitwarden/components";
|
||||
import { HOUR, MINUTE, liveFromNow, startClockAtStoryNow } from "@bitwarden/storybook";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import {
|
||||
HOUR,
|
||||
MINUTE,
|
||||
accessLease,
|
||||
accessRequest,
|
||||
liveFromNow,
|
||||
provideStoryChangeDetection,
|
||||
provideStoryLogService,
|
||||
startClockAtStoryNow,
|
||||
} from "../testing/story-fixtures";
|
||||
|
||||
const ORGANIZATION_ID = "org-1";
|
||||
|
||||
@@ -39,18 +39,14 @@ import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
|
||||
import { LoginUriView } from "@bitwarden/common/vault/models/view/login-uri.view";
|
||||
import { TaskService } from "@bitwarden/common/vault/tasks";
|
||||
import { DialogService, ToastService } from "@bitwarden/components";
|
||||
import { DAY, HOUR, fromNow, liveFromNow, startClockAtStoryNow } from "@bitwarden/storybook";
|
||||
import { CipherViewComponent, CIPHER_VIEW_BANNER, ITEM_DETAILS_BADGE } from "@bitwarden/vault";
|
||||
import { PreloadedEnglishI18nModule } from "@bitwarden/web-vault/app/core/tests";
|
||||
|
||||
import {
|
||||
DAY,
|
||||
HOUR,
|
||||
accessRequest,
|
||||
fromNow,
|
||||
liveFromNow,
|
||||
provideStoryChangeDetection,
|
||||
provideStoryLogService,
|
||||
startClockAtStoryNow,
|
||||
} from "../testing/story-fixtures";
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,48 +12,12 @@ import { LogService } from "@bitwarden/common/platform/abstractions/log.service"
|
||||
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
|
||||
import { Measurement } from "@bitwarden/logging";
|
||||
import { HOUR, MINUTE, fromNow } from "@bitwarden/storybook";
|
||||
import { LockService } from "@bitwarden/unlock";
|
||||
import { ProductSwitcherService } from "@bitwarden/web-vault/app/layouts/product-switcher/shared/product-switcher.service";
|
||||
|
||||
import type { ResolvedNames } from "../access-requests/access-name-resolver.service";
|
||||
|
||||
/**
|
||||
* Fixed, so story timestamps don't drift into a visual-regression diff. Build rows through the real
|
||||
* row builders, since a hand-written row can disagree with them.
|
||||
*/
|
||||
export const STORY_NOW = new Date("2026-08-17T12:00:00.000Z");
|
||||
|
||||
/** An offset from {@link STORY_NOW}, for labels built by a builder that takes `now`. */
|
||||
export function fromNow(ms: number): string {
|
||||
return new Date(STORY_NOW.getTime() + ms).toISOString();
|
||||
}
|
||||
|
||||
/**
|
||||
* An offset from `Date.now()`, for surfaces that tick their own clock. Call inside a story's
|
||||
* provider factory so it stays fresh on every render, and start the clock with
|
||||
* {@link startClockAtStoryNow} so the rendered times don't drift.
|
||||
*/
|
||||
export function liveFromNow(ms: number): string {
|
||||
return new Date(Date.now() + ms).toISOString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A story `beforeEach` that moves `Date.now()` to {@link STORY_NOW}. The clock keeps running, since
|
||||
* countdowns and rxjs debounces stall on a frozen one.
|
||||
*/
|
||||
export function startClockAtStoryNow(): () => void {
|
||||
const realNow = Date.now;
|
||||
const offset = STORY_NOW.getTime() - realNow();
|
||||
Date.now = () => realNow() + offset;
|
||||
return () => {
|
||||
Date.now = realNow;
|
||||
};
|
||||
}
|
||||
|
||||
export const MINUTE = 60 * 1000;
|
||||
export const HOUR = 60 * MINUTE;
|
||||
export const DAY = 24 * HOUR;
|
||||
|
||||
/** A pending request. Overrides widen through `unknown`, since the SDK brands most of these ids. */
|
||||
export function accessRequest(overrides: Record<string, unknown> = {}): AccessRequestView {
|
||||
return {
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from "./addons/feature-flags";
|
||||
export * from "./utils/format-args-for-code-snippet";
|
||||
export * from "./utils/story-clock";
|
||||
|
||||
35
libs/storybook/src/utils/story-clock.spec.ts
Normal file
35
libs/storybook/src/utils/story-clock.spec.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { HOUR, STORY_NOW, fromNow, liveFromNow, startClockAtStoryNow } from "./story-clock";
|
||||
|
||||
describe("story clock", () => {
|
||||
let real: number;
|
||||
|
||||
beforeEach(() => {
|
||||
real = 1_000_000;
|
||||
jest.spyOn(Date, "now").mockImplementation(() => real);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("offsets fromNow against STORY_NOW, whatever the real clock reads", () => {
|
||||
expect(fromNow(HOUR)).toBe("2026-08-17T13:00:00.000Z");
|
||||
});
|
||||
|
||||
it("starts Date.now at STORY_NOW and keeps it running", () => {
|
||||
const restore = startClockAtStoryNow();
|
||||
expect(Date.now()).toBe(STORY_NOW.getTime());
|
||||
|
||||
real += 5000;
|
||||
expect(Date.now()).toBe(STORY_NOW.getTime() + 5000);
|
||||
|
||||
restore();
|
||||
expect(Date.now()).toBe(real);
|
||||
});
|
||||
|
||||
it("offsets liveFromNow against the moved clock", () => {
|
||||
startClockAtStoryNow();
|
||||
real += 5000;
|
||||
expect(liveFromNow(-HOUR)).toBe("2026-08-17T11:00:05.000Z");
|
||||
});
|
||||
});
|
||||
33
libs/storybook/src/utils/story-clock.ts
Normal file
33
libs/storybook/src/utils/story-clock.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
export const MINUTE = 60 * 1000;
|
||||
export const HOUR = 60 * MINUTE;
|
||||
export const DAY = 24 * HOUR;
|
||||
|
||||
/** Fixed, so story timestamps don't drift into a visual-regression diff. */
|
||||
export const STORY_NOW = new Date("2026-08-17T12:00:00.000Z");
|
||||
|
||||
/** An offset from {@link STORY_NOW}, for labels built by a builder that takes `now`. */
|
||||
export function fromNow(ms: number): string {
|
||||
return new Date(STORY_NOW.getTime() + ms).toISOString();
|
||||
}
|
||||
|
||||
/**
|
||||
* An offset from `Date.now()`, for surfaces that tick their own clock. Call inside a story's
|
||||
* provider factory so it stays fresh on every render, and start the clock with
|
||||
* {@link startClockAtStoryNow} so the rendered times don't drift.
|
||||
*/
|
||||
export function liveFromNow(ms: number): string {
|
||||
return new Date(Date.now() + ms).toISOString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A story `beforeEach` that moves `Date.now()` to {@link STORY_NOW}. The clock keeps running, since
|
||||
* countdowns and rxjs debounces stall on a frozen one.
|
||||
*/
|
||||
export function startClockAtStoryNow(): () => void {
|
||||
const realNow = Date.now;
|
||||
const offset = STORY_NOW.getTime() - realNow();
|
||||
Date.now = () => realNow() + offset;
|
||||
return () => {
|
||||
Date.now = realNow;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user