Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Jul 10, 2024
1 parent fdb8035 commit e94ee1f
Show file tree
Hide file tree
Showing 10 changed files with 439 additions and 4 deletions.
35 changes: 35 additions & 0 deletions playwright/components/Dropdown/Dropdown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
*
* Copyright 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* /
*/
import { expect, test } from "@playwright/test";

test.describe("Dropdown", () => {
test("should be able to select a value", async ({ page }) => {
await page.goto(`iframe.html?viewMode=story&id=dropdown--default`, {
waitUntil: "networkidle",
});

await page.getByRole("combobox").click();
await expect(page).toHaveScreenshot({ fullPage: true });

await page.getByRole("option", { name: "Option 2" }).click();
await expect(page.getByRole("combobox")).toHaveText("Option 2");

await page.getByRole("combobox").click();
await expect(page).toHaveScreenshot({ fullPage: true });
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions src/components/Dropdown/Dropdown.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
*/
import { Dropdown } from "./Dropdown";
import { fn } from "@storybook/test";
import { Meta } from "@storybook/react";
import { ComponentProps } from "react";

export default {
title: "Controls/Dropdown",
title: "Dropdown",
component: Dropdown,
tags: ["autodocs"],
parameters: {
controls: {
include: ["defaultValue", "placeholder", "error", "values"],
include: ["defaultValue", "placeholder", "error"],
},
},
argTypes: {
Expand All @@ -38,7 +40,7 @@ export default {
type: "string",
},
values: {
type: "array",
type: "string",
},
},
args: {
Expand All @@ -50,7 +52,7 @@ export default {
["Option2", "Option 2"],
],
},
};
} satisfies Meta<ComponentProps<typeof Dropdown>>;

export const Default = {
parameters: {
Expand Down
83 changes: 83 additions & 0 deletions src/components/Dropdown/Dropdown.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
*
* Copyright 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* /
*/

import { describe, expect, it } from "vitest";
import { composeStories } from "@storybook/react";
import * as stories from "./Dropdown.stories";
import { act, render, waitFor } from "@testing-library/react";
import React from "react";
import { userEvent } from "@storybook/test";

const { Default, WithHelpLabel, WithError, WithDefaultValue } =
composeStories(stories);

describe("Dropdown", () => {
it("renders a Default dropdown", () => {
const { container } = render(<Default />);
expect(container).toMatchSnapshot();
});
it("renders a dropdown with a help label", () => {
const { container } = render(<WithHelpLabel />);
expect(container).toMatchSnapshot();
});
it("renders a dropdown with an error ", () => {
const { container } = render(<WithError />);
expect(container).toMatchSnapshot();
});
it("renders a dropdown with a default value ", () => {
const { container } = render(<WithDefaultValue />);
expect(container).toMatchSnapshot();
});
it("can be opened", async () => {
const { getByRole, container } = render(<Default />);
await act(async () => {
await userEvent.click(getByRole("combobox"));
});
expect(container).toMatchSnapshot();
});
it("can select a value", async () => {
const { getByRole, container } = render(<Default />);
await act(async () => {
await userEvent.click(getByRole("combobox"));
});

await waitFor(() =>
expect(getByRole("option", { name: "Option 2" })).toBeInTheDocument(),
);

await act(async () => {
await userEvent.click(getByRole("option", { name: "Option 2" }));
});

expect(getByRole("combobox")).toHaveTextContent("Option 2");

await act(async () => {
await userEvent.click(getByRole("combobox"));
});

await waitFor(() =>
expect(getByRole("option", { name: "Option 2" })).toHaveAttribute(
"aria-selected",
"true",
),
);

// Option 2 should be selected
expect(container).toMatchSnapshot();
});
});
Loading

0 comments on commit e94ee1f

Please sign in to comment.