Skip to content

Commit

Permalink
test: add stories for sections
Browse files Browse the repository at this point in the history
  • Loading branch information
codeincontext committed Nov 21, 2024
1 parent 61ae4a1 commit badf85e
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 186 deletions.
1 change: 0 additions & 1 deletion apps/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"@radix-ui/themes": "^1.0.0",
"@sanity/client": "^6.21.3",
"@sentry/nextjs": "^8.35.0",
"@storybook/testing-react": "^2.0.1",
"@tanstack/react-query": "^4.16.1",
"@testing-library/jest-dom": "^6.4.8",
"@testing-library/react": "^16.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import type { Meta, StoryObj } from "@storybook/react";
import { userEvent, within } from "@storybook/test";

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

import DropDownSection from "./";

const MAX_INT32 = 2 ** 31 - 1;

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 meta: Meta<typeof DropDownSection> = {
title: "Components/LessonPlan/DropDownSection",
component: DropDownSection,
tags: ["autodocs"],
args: {
objectKey: "learningOutcome",
// objectKey,
// sectionRefs,
value:
"I can explain the reasons why frogs are so important to British society and culture",
// userHasCancelledAutoScroll,
// showLessonMobile,
documentContainerRef: { current: null },
streamingTimeout: 0,
},
decorators: [ChatDecorator],
};

export default meta;
type Story = StoryObj<typeof DropDownSection>;

export const Default: Story = {
args: {},
};

export const Markdown: Story = {
args: {
value: `# Title 1
## Title 2
### Title 3
- **Bold**
- *Italic*
- Normal`,
},
};

export const Streaming: Story = {
args: { streamingTimeout: MAX_INT32 },
};

export const Closed: Story = {
parameters: {
docs: {
// NOTE: This should run the play function in the docs page, but seems broken
story: { autoplay: true },
},
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const toggleButton = await canvas.findByRole("button", { name: "toggle" });
await userEvent.click(toggleButton);
},
};

export const AdditionalMaterials: Story = {
args: {
objectKey: "additionalMaterials",
value: "None",
},
};

export const Modify: Story = {
parameters: {
docs: {
// NOTE: This should run the play function in the docs page, but seems broken
story: { autoplay: true },
},
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const modifyButton = await canvas.findByRole("button", { name: "Modify" });
await userEvent.click(modifyButton);
},
};

export const ModifyAdditionalMaterials: Story = {
parameters: {
docs: {
// NOTE: This should run the play function in the docs page, but seems broken
story: { autoplay: true },
},
},
args: {
objectKey: "additionalMaterials",
value: "None",
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const modifyButton = await canvas.findByRole("button", {
name: "Add additional materials",
});
await userEvent.click(modifyButton);
},
};

export const Flag: Story = {
parameters: {
docs: {
// NOTE: This should run the play function in the docs page, but seems broken
story: { autoplay: true },
},
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const flagButton = await canvas.findByRole("button", { name: "Flag" });
await userEvent.click(flagButton);
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,28 @@ import { scrollToRef } from "@/utils/scrollToRef";
import Skeleton from "../../common/Skeleton";
import ChatSection from "./chat-section";

const DropDownSection = ({
objectKey,
sectionRefs,
value,
documentContainerRef,
userHasCancelledAutoScroll,
showLessonMobile,
}: {
const HALF_SECOND = 500;

type DropDownSectionProps = {
objectKey: string;
sectionRefs: Record<string, React.MutableRefObject<HTMLDivElement | null>>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any;
documentContainerRef: React.MutableRefObject<HTMLDivElement | null>;
userHasCancelledAutoScroll: boolean;
showLessonMobile: boolean;
}) => {
streamingTimeout?: number;
};

const DropDownSection = ({
objectKey,
sectionRefs,
value,
documentContainerRef,
userHasCancelledAutoScroll,
showLessonMobile,
streamingTimeout = HALF_SECOND,
}: DropDownSectionProps) => {
const sectionRef = useRef(null);
if (sectionRefs) sectionRefs[objectKey] = sectionRef;
const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -68,7 +74,7 @@ const DropDownSection = ({
const timer = setTimeout(() => {
setStatus("isLoaded");
setPrevValue(value);
}, 500); // 0.5 seconds delay
}, streamingTimeout);

return () => clearTimeout(timer);
} else {
Expand Down Expand Up @@ -101,7 +107,7 @@ const DropDownSection = ({
{status === "isLoaded" && <Icon icon="tick" size="sm" />}
</OakBox>

<FullWidthButton onClick={() => setIsOpen(!isOpen)}>
<FullWidthButton onClick={() => setIsOpen(!isOpen)} aria-label="toggle">
<OakFlex $width="100%" $justifyContent="space-between">
<OakP $font="heading-6">{sectionTitle(objectKey)}</OakP>
<Icon icon={isOpen ? "chevron-up" : "chevron-down"} size="sm" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Meta, StoryObj } from "@storybook/react";

import type { ChatContextProps } from "@/components/ContextProviders/ChatProvider";
import { ChatContext } from "@/components/ContextProviders/ChatProvider";
import { DemoProvider } from "@/components/ContextProviders/Demo";
import { DemoContext } from "@/components/ContextProviders/Demo";

import { MobileExportButtons } from "./MobileExportButtons";

Expand All @@ -19,10 +19,16 @@ const ChatDecorator: Story["decorators"] = (Story, { parameters }) => (
</ChatContext.Provider>
);

const DemoDecorator: Story["decorators"] = (Story) => (
<DemoProvider>
const DemoDecorator: Story["decorators"] = (Story, { parameters }) => (
<DemoContext.Provider
value={{
isDemoUser: false,
isSharingEnabled: true,
...parameters.demoContext,
}}
>
<Story />
</DemoProvider>
</DemoContext.Provider>
);

const meta: Meta<typeof MobileExportButtons> = {
Expand All @@ -40,5 +46,11 @@ type Story = StoryObj<typeof MobileExportButtons>;

export const Default: Story = {};

// TODO
export const SharingDisabled: Story = {};
export const SharingDisabled: Story = {
parameters: {
demoContext: {
isDemoUser: true,
isSharingEnabled: false,
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Meta, StoryObj } from "@storybook/react";

import type { ChatContextProps } from "@/components/ContextProviders/ChatProvider";
import { ChatContext } from "@/components/ContextProviders/ChatProvider";
import { DemoProvider } from "@/components/ContextProviders/Demo";
import { DemoContext } from "@/components/ContextProviders/Demo";

import ExportButtons from "./";

Expand All @@ -21,10 +21,16 @@ const ChatDecorator: Story["decorators"] = (Story, { parameters }) => (
</ChatContext.Provider>
);

const DemoDecorator: Story["decorators"] = (Story) => (
<DemoProvider>
const DemoDecorator: Story["decorators"] = (Story, { parameters }) => (
<DemoContext.Provider
value={{
isDemoUser: false,
isSharingEnabled: true,
...parameters.demoContext,
}}
>
<Story />
</DemoProvider>
</DemoContext.Provider>
);

const meta: Meta<typeof ExportButtons> = {
Expand All @@ -51,5 +57,11 @@ export const IsStreaming: Story = {
},
};

// TODO
export const SharingDisabled: Story = {};
export const SharingDisabled: Story = {
parameters: {
demoContext: {
isDemoUser: true,
isSharingEnabled: false,
},
},
};
2 changes: 1 addition & 1 deletion apps/nextjs/src/components/ContextProviders/Demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type DemoContextProps =
isSharingEnabled: boolean;
};

const DemoContext = createContext<DemoContextProps | null>(null);
export const DemoContext = createContext<DemoContextProps | null>(null);

export type DemoProviderProps = Readonly<{ children: React.ReactNode }>;

Expand Down
Loading

0 comments on commit badf85e

Please sign in to comment.