Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Storybook Testing with Additional and Refactored Tests #1240

Merged
merged 23 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3519400
fix: Theme provider issue in storybook
NabinKawan Jun 10, 2024
fc350fe
fix: ActionsBar interactions test
NabinKawan Jun 10, 2024
7cc82db
fix: test interaction loading button
NabinKawan Jun 10, 2024
a5da0ac
Add interaction tests for card
NabinKawan Jun 10, 2024
6033b44
Add interaction tests for checkbox
NabinKawan Jun 10, 2024
91fde96
chore: Update DashboardTopNav interaction test
NabinKawan Jun 10, 2024
67d95c9
chore: Update DelegationActionRadio interaction test
NabinKawan Jun 10, 2024
fa48c61
chore: Update footer interaction test
NabinKawan Jun 10, 2024
d8feb98
chore: Add interaction tests for (data-missing, format incorrect, not…
NabinKawan Jun 10, 2024
889e817
test: Invalid GovernanceAction details
NabinKawan Jun 11, 2024
8b22deb
chore: refactor tests for Input story and add interaction for 'helpfu…
NabinKawan Jun 11, 2024
31c1d48
test: story LinkWithIcon
NabinKawan Jun 11, 2024
365d7b5
chore: test Slider component overflow
NabinKawan Jun 11, 2024
9dd228b
test: story StatusPill
NabinKawan Jun 11, 2024
3508c5c
test: story Step
NabinKawan Jun 11, 2024
a828db2
test: story TextArea
NabinKawan Jun 11, 2024
b07a830
chore: Remove home-link in TopNav story
NabinKawan Jun 11, 2024
be10f7e
test: story loading modal
NabinKawan Jun 11, 2024
917e503
test: story Voting Power Model
NabinKawan Jun 11, 2024
789ac3b
test: story dashboard card
NabinKawan Jun 11, 2024
5ff75a3
chore: Increase interaction test timeout to 30 sec
NabinKawan Jun 11, 2024
39af694
fix: story Footer interaction (feedback-footer-button)
NabinKawan Jun 11, 2024
b1badcc
chore: Remove helps-link test from Drawer story
NabinKawan Jun 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions govtool/frontend/.storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from "react";
import type { Preview } from "@storybook/react";
import { ThemeProvider } from "@emotion/react";
import { theme } from "../src/theme";
import { MemoryRouter, Routes, Route } from "react-router-dom";
import { QueryClient, QueryClientProvider } from "react-query";
import type { Preview } from "@storybook/react";
import React from "react";
import { I18nextProvider } from "react-i18next";
import i18n from "../src/i18n";
import { QueryClient, QueryClientProvider } from "react-query";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { ModalProvider } from "../src/context/modal";
import i18n from "../src/i18n";
import { theme } from "../src/theme";

const queryClient = new QueryClient();

Expand All @@ -23,9 +23,9 @@ const preview: Preview = {
decorators: [
(Story) => (
<QueryClientProvider client={queryClient}>
<ModalProvider>
<I18nextProvider i18n={i18n}>
<ThemeProvider theme={theme}>
<ThemeProvider theme={theme}>
<ModalProvider>
<I18nextProvider i18n={i18n}>
<MemoryRouter>
<Routes>
<Route
Expand All @@ -44,9 +44,9 @@ const preview: Preview = {
/>
</Routes>
</MemoryRouter>
</ThemeProvider>
</I18nextProvider>
</ModalProvider>
</I18nextProvider>
</ModalProvider>
</ThemeProvider>
</QueryClientProvider>
),
],
Expand Down
13 changes: 13 additions & 0 deletions govtool/frontend/.storybook/test-runner-jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { getJestConfig } = require("@storybook/test-runner");

/**
* @type {import('@jest/types').Config.InitialOptions}
*/
module.exports = {
// The default configuration comes from @storybook/test-runner
...getJestConfig(),
/** Add your own overrides below
* @see https://jestjs.io/docs/configuration
*/
testTimeout: 30000,
};
11 changes: 8 additions & 3 deletions govtool/frontend/src/stories/ActionsBar.stories.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { userEvent, within } from "@storybook/testing-library";
import { expect, jest } from "@storybook/jest";
import type { Meta, StoryObj } from "@storybook/react";
import {
GOVERNANCE_ACTIONS_FILTERS,
GOVERNANCE_ACTIONS_SORTING,
} from "@consts";
import { expect, jest } from "@storybook/jest";
import type { Meta, StoryObj } from "@storybook/react";
import { userEvent, within } from "@storybook/testing-library";
import { DataActionsBar } from "@/components/molecules";

const meta = {
title: "Example/DataActionsBar",
component: DataActionsBar,
args: {
filterOptions: GOVERNANCE_ACTIONS_FILTERS,
sortOptions: GOVERNANCE_ACTIONS_SORTING,
},
parameters: {
layout: "centered",
},
Expand All @@ -25,6 +29,7 @@ export const ActionsBarComponent: Story = {
searchText: "",
sortingActive: false,
sortOpen: false,
filterOptions: GOVERNANCE_ACTIONS_FILTERS,
isFiltering: true,
setFiltersOpen: jest.fn(),
setSortOpen: jest.fn(),
Expand Down
31 changes: 24 additions & 7 deletions govtool/frontend/src/stories/Button.stories.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, jest } from "@storybook/jest";
import type { Meta, StoryObj } from "@storybook/react";
import { userEvent, within } from "@storybook/testing-library";
import { expect, jest } from "@storybook/jest";

import { Button } from "@atoms";

Expand All @@ -10,12 +10,6 @@ const meta = {
args: {
onClick: jest.fn(),
},
play: async ({ canvasElement, args }) => {
const canvas = within(canvasElement);
expect(canvas.getByRole("button")).toHaveTextContent("Button");
await userEvent.click(canvas.getByRole("button"));
await expect(args.onClick).toHaveBeenCalled();
},
parameters: {
layout: "centered",
},
Expand All @@ -31,10 +25,22 @@ export const Contained: Story = {
size: "medium",
variant: "contained",
},
play: async ({ canvasElement, args }) => {
const canvas = within(canvasElement);
expect(canvas.getByRole("button")).toHaveTextContent("Button");
await userEvent.click(canvas.getByRole("button"));
await expect(args.onClick).toHaveBeenCalled();
},
};

export const Outlined: Story = {
args: { size: "small", variant: "outlined", children: "Button" },
play: async ({ canvasElement, args }) => {
const canvas = within(canvasElement);
expect(canvas.getByRole("button")).toHaveTextContent("Button");
await userEvent.click(canvas.getByRole("button"));
await expect(args.onClick).toHaveBeenCalled();
},
};

export const Text: Story = {
Expand All @@ -43,6 +49,12 @@ export const Text: Story = {
variant: "text",
children: "Button",
},
play: async ({ canvasElement, args }) => {
const canvas = within(canvasElement);
expect(canvas.getByRole("button")).toHaveTextContent("Button");
await userEvent.click(canvas.getByRole("button"));
await expect(args.onClick).toHaveBeenCalled();
},
};

export const LoadingButton: Story = {
Expand All @@ -52,4 +64,9 @@ export const LoadingButton: Story = {
variant: "contained",
isLoading: true,
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
expect(canvas.getByRole("button")).toHaveTextContent("Button");
await expect(canvas.getByRole("button")).toBeDisabled();
},
};
15 changes: 15 additions & 0 deletions govtool/frontend/src/stories/Card.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect } from "@storybook/jest";
import type { Meta, StoryObj } from "@storybook/react";

import { Card } from "@molecules";
import { within } from "@storybook/testing-library";

const meta = {
title: "Example/Card",
Expand Down Expand Up @@ -33,10 +35,23 @@ type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await expect(
canvas.getByText("Put here whatever you want to display in the card."),
).toBeVisible();
},
};

export const WithLabel: Story = {
args: {
label: "Label goes here",
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await expect(
canvas.getByText("Put here whatever you want to display in the card."),
).toBeVisible();
await expect(canvas.getByText("Label goes here")).toBeVisible();
},
};
26 changes: 26 additions & 0 deletions govtool/frontend/src/stories/Checkbox.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect } from "@storybook/jest";
import type { Meta, StoryFn } from "@storybook/react";

import { Field } from "@molecules";
import { within } from "@storybook/testing-library";
import { ComponentProps } from "react";

const meta = {
Expand All @@ -19,19 +21,43 @@ const Template: StoryFn<ComponentProps<typeof Field.Checkbox>> = (args) => (
);

export const Default = Template.bind({});
Default.play = async ({ canvasElement }) => {
const canvas = within(canvasElement);
expect(canvas.getByRole("checkbox")).not.toBeChecked();
};

export const WithLabel = Template.bind({});
WithLabel.args = {
label: "Label",
};
WithLabel.play = async ({ canvasElement }) => {
const canvas = within(canvasElement);
expect(canvas.getByRole("checkbox")).not.toBeChecked();
expect(canvas.getByText("Label")).toBeVisible();
};

export const Error = Template.bind({});
Error.args = {
errorMessage: "Error message",
};
Error.play = async ({ canvasElement, args }) => {
const canvas = within(canvasElement);
const errorId = args.errorMessage!.toLowerCase().split(" ").join("-");

expect(canvas.getByRole("checkbox")).not.toBeChecked();
expect(canvas.getByTestId(`${errorId}-error`)).toBeVisible();
};

export const ErrorAndLabel = Template.bind({});
ErrorAndLabel.args = {
errorMessage: "Error message",
label: "Label",
};
ErrorAndLabel.play = async ({ canvasElement, args }) => {
const canvas = within(canvasElement);
const errorId = args.errorMessage!.toLowerCase().split(" ").join("-");

expect(canvas.getByRole("checkbox")).not.toBeChecked();
expect(canvas.getByText("Label")).toBeVisible();
expect(canvas.getByTestId(`${errorId}-error`)).toBeVisible();
};
44 changes: 24 additions & 20 deletions govtool/frontend/src/stories/DashboardCard.stories.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Meta, StoryObj } from "@storybook/react";

import { DashboardActionCard } from "@molecules";
import { within } from "@storybook/testing-library";
import { expect } from "@storybook/jest";
import { within } from "@storybook/testing-library";
import { IMAGES } from "@/consts";

const meta = {
Expand All @@ -17,52 +17,57 @@ const meta = {
export default meta;
type Story = StoryObj<typeof meta>;

async function assertCardInfo(canvas: ReturnType<typeof within>) {
expect(canvas.getByText("Action card")).toBeVisible();
expect(canvas.queryByText(/lorem/i)).toBeInTheDocument();
expect(canvas.queryByRole("img")).toBeInTheDocument();
}

export const DashboardCardComponent: Story = {
args: {
buttons: [
{ children: "first button" },
{ children: "second button" },
],
buttons: [{ children: "first button" }, { children: "second button" }],
description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
imageURL: IMAGES.govActionDelegateImage,
title: "Action card",
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
expect(canvas.getByText("Action card")).toBeInTheDocument();
expect(canvas.getByText(/lorem/i)).toBeInTheDocument();

await assertCardInfo(canvas);

const buttons = canvas.getAllByRole("button");
await expect(buttons[0].textContent).toBe("first button");
await expect(buttons[1].textContent).toBe("second button");
expect(canvas.getByRole("img")).toBeInTheDocument();
},
};

export const WithDRepIdDashboardCardComponent: Story = {
args: {
buttons: [
{ children: "first button" },
{ children: "second button" },
],
buttons: [{ children: "first button" }, { children: "second button" }],
description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
imageURL: IMAGES.govActionDelegateImage,
title: "Action card",
},
play: async ({ canvasElement }) => {
const dRepId = "";
const canvas = within(canvasElement);

await assertCardInfo(canvas);
await expect(canvas.getByTestId("drep-id-info")).toHaveTextContent(dRepId);
},
};

export const LoadingDashboardCard: Story = {
args: {
buttons: [
{ children: "first button" },
{ children: "second button" },
],
buttons: [{ children: "first button" }, { children: "second button" }],
description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
imageURL: IMAGES.govActionDelegateImage,
title: "Action card",
isLoading: true,
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

expect(canvas.queryByText("Action card")).not.toBeInTheDocument();
expect(canvas.queryByText(/lorem/i)).not.toBeInTheDocument();
await expect(canvas.queryAllByRole("button")).toHaveLength(0);
Expand All @@ -72,17 +77,16 @@ export const LoadingDashboardCard: Story = {

export const InProgressDashboardCard: Story = {
args: {
buttons: [
{ children: "first button" },
{ children: "second button" },
],
buttons: [{ children: "first button" }, { children: "second button" }],
description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
imageURL: IMAGES.govActionDelegateImage,
title: "Action card",
state: "inProgress",
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await assertCardInfo(canvas);
await expect(canvas.getAllByText(/in progress/i)).toHaveLength(2);
},
};
12 changes: 1 addition & 11 deletions govtool/frontend/src/stories/DashboardTopNav.stories.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Meta, StoryObj } from "@storybook/react";

import { DashboardTopNav } from "@organisms";
import { within, userEvent, waitFor, screen } from "@storybook/testing-library";
import { expect } from "@storybook/jest";
import { within } from "@storybook/testing-library";

const meta = {
title: "Example/DashboardTopNav",
Expand All @@ -18,15 +18,5 @@ export const DashboardTopNavComponent: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await expect(canvas.getByText("Example title")).toBeInTheDocument();
await expect(canvas.getByText("Voting power:")).toBeInTheDocument();

await expect(canvas.getByTestId("InfoOutlinedIcon")).toBeInTheDocument();
await userEvent.hover(canvas.getByTestId("InfoOutlinedIcon"));
await waitFor(async () => {
await expect(screen.getByRole("tooltip")).toBeInTheDocument();
await expect(screen.getByRole("tooltip")).toHaveTextContent(
/DRep Voting Power/i,
);
});
},
};
Loading
Loading