Skip to content

Commit

Permalink
test: storybook decorators (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeincontext authored Dec 12, 2024
1 parent 7c24485 commit 35e25fc
Show file tree
Hide file tree
Showing 36 changed files with 418 additions and 379 deletions.
35 changes: 35 additions & 0 deletions apps/nextjs/.storybook/decorators/AnalyticsDecorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from "react";

import type { Decorator } from "@storybook/react";
import { fn } from "@storybook/test";

import {
analyticsContext,
type AnalyticsContext,
} from "../../src/components/ContextProviders/AnalyticsProvider";

declare module "@storybook/csf" {
interface Parameters {
analyticsContext?: Partial<AnalyticsContext>;
}
}

export const AnalyticsDecorator: Decorator = (Story, { parameters }) => {
return (
<analyticsContext.Provider
value={
{
track: fn(),
trackEvent: fn(),
identify: fn(),
reset: fn(),
page: fn(),
posthogAiBetaClient: {},
...parameters.analyticsContext,
} as unknown as AnalyticsContext
}
>
<Story />
</analyticsContext.Provider>
);
};
20 changes: 20 additions & 0 deletions apps/nextjs/.storybook/decorators/ChatDecorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";

import type { Decorator } from "@storybook/react";

import {
ChatContext,
type ChatContextProps,
} from "../../src/components/ContextProviders/ChatProvider";

declare module "@storybook/csf" {
interface Parameters {
chatContext?: Partial<ChatContextProps>;
}
}

export const ChatDecorator: Decorator = (Story, { parameters }) => (
<ChatContext.Provider value={parameters.chatContext as ChatContextProps}>
<Story />
</ChatContext.Provider>
);
62 changes: 62 additions & 0 deletions apps/nextjs/.storybook/decorators/DemoDecorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from "react";

import type { Decorator } from "@storybook/react";
import invariant from "tiny-invariant";

import {
DemoContext,
type DemoContextProps,
} from "@/components/ContextProviders/Demo";

declare module "@storybook/csf" {
interface Parameters {
demoContext?: DemoContextProps;
}
}

export const DemoDecorator: Decorator = (Story, { parameters }) => {
const value = parameters.demoContext;
invariant(
value,
"DemoDecorator requires a DemoContext. Please call ...demoParams() in the parameters",
);

return (
<DemoContext.Provider value={value}>
<Story />
</DemoContext.Provider>
);
};

const demoBase: DemoContextProps["demo"] = {
appSessionsRemaining: 2,
appSessionsPerMonth: 3,
contactHref: "https://share.hsforms.com/1R9ulYSNPQgqElEHde3KdhAbvumd",
};

type DemoParams = {
isDemoUser: boolean;
demo?: Partial<DemoContextProps["demo"]>;
isSharingEnabled?: boolean;
};
export const demoParams = (
args: DemoParams,
): { demoContext: DemoContextProps } => {
const isSharingEnabled = args.isSharingEnabled ?? true;

const context: DemoContextProps = args.isDemoUser
? {
isDemoUser: true,
demo: { ...demoBase, ...args.demo },
isSharingEnabled,
}
: {
isDemoUser: false,
demo: undefined,
isSharingEnabled,
};

return {
demoContext: context,
};
};
30 changes: 30 additions & 0 deletions apps/nextjs/.storybook/decorators/DialogContentDecorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from "react";

import type { Decorator } from "@storybook/react";
import { fn } from "@storybook/test";

import type { DialogTypes } from "../../src/components/AppComponents/Chat/Chat/types";
import { DialogContext } from "../../src/components/AppComponents/DialogContext";

declare module "@storybook/csf" {
interface Parameters {
dialogWindow?: DialogTypes;
}
}

export const DialogContentDecorator: Decorator = (Story, { parameters }) => {
return (
<DialogContext.Provider
value={{
dialogWindow: parameters.dialogWindow ?? "",
setDialogWindow: fn(),
dialogProps: {},
setDialogProps: fn(),
openSidebar: false,
setOpenSidebar: fn(),
}}
>
<Story />
</DialogContext.Provider>
);
};
21 changes: 21 additions & 0 deletions apps/nextjs/.storybook/decorators/LessonPlanTrackingDecorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from "react";

import type { Decorator } from "@storybook/react";
import { fn } from "@storybook/test";

import { LessonPlanTrackingContext } from "../../src/lib/analytics/lessonPlanTrackingContext";

export const LessonPlanTrackingDecorator: Decorator = (Story) => (
<LessonPlanTrackingContext.Provider
value={{
onClickContinue: fn(),
onClickRetry: fn(),
onClickStartFromExample: fn(),
onClickStartFromFreeText: fn(),
onStreamFinished: fn(),
onSubmitText: fn(),
}}
>
<Story />
</LessonPlanTrackingContext.Provider>
);
3 changes: 2 additions & 1 deletion apps/nextjs/.storybook/decorators/RadixThemeDecorator.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from "react";

import { Theme } from "@radix-ui/themes";
import { Decorator } from "@storybook/react";

export const RadixThemeDecorator = (Story: React.ComponentType) => (
export const RadixThemeDecorator: Decorator = (Story) => (
<Theme>
<Story />
</Theme>
Expand Down
25 changes: 25 additions & 0 deletions apps/nextjs/.storybook/decorators/SidebarDecorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";

import type { Decorator } from "@storybook/react";
import { fn } from "@storybook/test";

import { SidebarContext } from "../../src/lib/hooks/use-sidebar";

declare module "@storybook/csf" {
interface Parameters {
// Please fill out as we add configuration
sidebarContext?: {};
}
}

export const SidebarDecorator: Decorator = (Story) => (
<SidebarContext.Provider
value={{
toggleSidebar: fn(),
isLoading: false,
isSidebarOpen: false,
}}
>
<Story />
</SidebarContext.Provider>
);
8 changes: 1 addition & 7 deletions apps/nextjs/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ const preview: Preview = {
loaders: [mswLoader],
};

// Providers not currently used
// - CookieConsentProvider
// - DemoProvider
// - LessonPlanTrackingProvider
// - DialogProvider
// - SidebarProvider
// - ChatModerationProvider
// NOTE: See ./decorators for more decorators available to use in stories

export const decorators: Decorator[] = [
RadixThemeDecorator,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { AilaPersistedChat } from "@oakai/aila/src/protocol/schema";
import type { Meta, StoryObj } from "@storybook/react";

import { chromaticParams } from "../../../../../.storybook/chromatic";
import { chromaticParams } from "@/storybook/chromatic";

import { DemoProvider } from "../../../../../src/components/ContextProviders/Demo";
import { DownloadContent } from "./DownloadView";

Expand Down
3 changes: 2 additions & 1 deletion apps/nextjs/src/app/aila/[id]/share/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { LooseLessonPlan } from "@oakai/aila/src/protocol/schema";
import type { Meta, StoryObj } from "@storybook/react";

import { chromaticParams } from "../../../../../.storybook/chromatic";
import { chromaticParams } from "@/storybook/chromatic";

import ShareChat from "./";

const meta: Meta<typeof ShareChat> = {
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/src/app/aila/help/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Meta, StoryObj } from "@storybook/react";

import { DemoProvider } from "@/components/ContextProviders/Demo";
import { chromaticParams } from "@/storybook/chromatic";

import { HelpContent } from ".";
import { chromaticParams } from "../../../../.storybook/chromatic";

const meta: Meta<typeof HelpContent> = {
title: "Pages/Chat/Help",
Expand Down
3 changes: 2 additions & 1 deletion apps/nextjs/src/app/faqs/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Meta, StoryObj } from "@storybook/react";

import { chromaticParams } from "@/storybook/chromatic";

import { FAQPageContent } from ".";
import { chromaticParams } from "../../../.storybook/chromatic";

const meta: Meta<typeof FAQPageContent> = {
title: "Pages/FAQs",
Expand Down
3 changes: 2 additions & 1 deletion apps/nextjs/src/app/home-page.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Meta, StoryObj } from "@storybook/react";

import { chromaticParams } from "../../.storybook/chromatic";
import { chromaticParams } from "@/storybook/chromatic";

import { HomePageContent } from "./home-page";

const meta: Meta<typeof HomePageContent> = {
Expand Down
3 changes: 2 additions & 1 deletion apps/nextjs/src/app/legal/[slug]/legal.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Meta, StoryObj } from "@storybook/react";

import { chromaticParams } from "../../../../.storybook/chromatic";
import { chromaticParams } from "@/storybook/chromatic";

import { LegalContent } from "./legal";

const meta: Meta<typeof LegalContent> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Meta, StoryObj } from "@storybook/react";

import { chromaticParams } from "../../../../.storybook/chromatic";
import { chromaticParams } from "@/storybook/chromatic";

import { AccountLocked } from "./account-locked";

const meta: Meta<typeof AccountLocked> = {
Expand Down
3 changes: 2 additions & 1 deletion apps/nextjs/src/app/prompts/prompts.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Meta, StoryObj } from "@storybook/react";

import { chromaticParams } from "../../../.storybook/chromatic";
import { chromaticParams } from "@/storybook/chromatic";

import { PromptsContent } from "./prompts";

const meta: Meta<typeof PromptsContent> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
import type { Meta, StoryObj } from "@storybook/react";

import type { ChatContextProps } from "@/components/ContextProviders/ChatProvider";
import { ChatContext } from "@/components/ContextProviders/ChatProvider";
import { ChatDecorator } from "@/storybook/decorators/ChatDecorator";

import LessonPlanDisplay from "./chat-lessonPlanDisplay";

const ChatDecorator: Story["decorators"] = (Story, { parameters }) => (
<ChatContext.Provider
value={
{
id: "123",
lastModeration: null,
messages: [],
lessonPlan: {
title: "About Frogs",
keyStage: "Key Stage 2",
subject: "Science",
topic: "Amphibians",
basedOn: "Frogs in Modern Britain",
learningOutcome:
"To understand the importance of frogs in British society and culture",
},
ailaStreamingStatus: "Idle",
...parameters.chatContext,
} as unknown as ChatContextProps
}
>
<Story />
</ChatContext.Provider>
);
const chatContext: Partial<ChatContextProps> = {
id: "123",
lastModeration: null,
messages: [],
lessonPlan: {
title: "About Frogs",
keyStage: "Key Stage 2",
subject: "Science",
topic: "Amphibians",
basedOn: { title: "Frogs in Modern Britain" },
learningOutcome:
"To understand the importance of frogs in British society and culture",
},
ailaStreamingStatus: "Idle",
};

const meta: Meta<typeof LessonPlanDisplay> = {
title: "Components/LessonPlan/LessonPlanDisplay",
Expand All @@ -47,14 +38,15 @@ type Story = StoryObj<typeof LessonPlanDisplay>;
export const Default: Story = {
args: {},
parameters: {
chatContext: {},
chatContext,
},
};

export const Loading: Story = {
args: {},
parameters: {
chatContext: {
...chatContext,
lessonPlan: {},
},
},
Expand All @@ -64,7 +56,9 @@ export const WithModeration: Story = {
args: {},
parameters: {
chatContext: {
...chatContext,
lastModeration: {
id: "123",
categories: ["l/strong-language"],
},
},
Expand Down
Loading

0 comments on commit 35e25fc

Please sign in to comment.