|
| 1 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 2 | +import { expect, userEvent, within } from "@storybook/test"; |
| 3 | +import type { ProvisionerDaemon } from "api/typesGenerated"; |
| 4 | +import { type FC, useState } from "react"; |
| 5 | +import { ProvisionerTagsField } from "./ProvisionerTagsField"; |
| 6 | + |
| 7 | +const meta: Meta<typeof ProvisionerTagsField> = { |
| 8 | + title: "modules/provisioners/ProvisionerTagsField", |
| 9 | + component: ProvisionerTagsField, |
| 10 | + args: { |
| 11 | + value: {}, |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +export default meta; |
| 16 | +type Story = StoryObj<typeof ProvisionerTagsField>; |
| 17 | + |
| 18 | +export const Empty: Story = { |
| 19 | + args: { |
| 20 | + value: {}, |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +export const WithInitialValue: Story = { |
| 25 | + args: { |
| 26 | + value: { |
| 27 | + cluster: "dogfood-2", |
| 28 | + env: "gke", |
| 29 | + scope: "organization", |
| 30 | + }, |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +type StatefulProvisionerTagsFieldProps = { |
| 35 | + initialValue?: ProvisionerDaemon["tags"]; |
| 36 | +}; |
| 37 | + |
| 38 | +const StatefulProvisionerTagsField: FC<StatefulProvisionerTagsFieldProps> = ({ |
| 39 | + initialValue = {}, |
| 40 | +}) => { |
| 41 | + const [value, setValue] = useState<ProvisionerDaemon["tags"]>(initialValue); |
| 42 | + return <ProvisionerTagsField value={value} onChange={setValue} />; |
| 43 | +}; |
| 44 | + |
| 45 | +export const OnOverwriteOwner: Story = { |
| 46 | + play: async ({ canvasElement }) => { |
| 47 | + const user = userEvent.setup(); |
| 48 | + const canvas = within(canvasElement); |
| 49 | + const keyInput = canvas.getByLabelText("Tag key"); |
| 50 | + const valueInput = canvas.getByLabelText("Tag value"); |
| 51 | + const addButton = canvas.getByRole("button", { name: "Add tag" }); |
| 52 | + |
| 53 | + await user.type(keyInput, "owner"); |
| 54 | + await user.type(valueInput, "dogfood-2"); |
| 55 | + await user.click(addButton); |
| 56 | + |
| 57 | + await canvas.findByText("Cannot override owner tag"); |
| 58 | + }, |
| 59 | +}; |
| 60 | + |
| 61 | +export const OnInvalidScope: Story = { |
| 62 | + play: async ({ canvasElement }) => { |
| 63 | + const user = userEvent.setup(); |
| 64 | + const canvas = within(canvasElement); |
| 65 | + const keyInput = canvas.getByLabelText("Tag key"); |
| 66 | + const valueInput = canvas.getByLabelText("Tag value"); |
| 67 | + const addButton = canvas.getByRole("button", { name: "Add tag" }); |
| 68 | + |
| 69 | + await user.type(keyInput, "scope"); |
| 70 | + await user.type(valueInput, "invalid"); |
| 71 | + await user.click(addButton); |
| 72 | + |
| 73 | + await canvas.findByText("Scope value must be 'organization' or 'user'"); |
| 74 | + }, |
| 75 | +}; |
| 76 | + |
| 77 | +export const OnAddTag: Story = { |
| 78 | + render: () => <StatefulProvisionerTagsField />, |
| 79 | + play: async ({ canvasElement }) => { |
| 80 | + const user = userEvent.setup(); |
| 81 | + const canvas = within(canvasElement); |
| 82 | + const keyInput = canvas.getByLabelText("Tag key"); |
| 83 | + const valueInput = canvas.getByLabelText("Tag value"); |
| 84 | + const addButton = canvas.getByRole("button", { name: "Add tag" }); |
| 85 | + |
| 86 | + await user.type(keyInput, "cluster"); |
| 87 | + await user.type(valueInput, "dogfood-2"); |
| 88 | + await user.click(addButton); |
| 89 | + |
| 90 | + const addedTag = await canvas.findByTestId("tag-cluster"); |
| 91 | + await expect(addedTag).toHaveTextContent("cluster dogfood-2"); |
| 92 | + }, |
| 93 | +}; |
| 94 | + |
| 95 | +export const OnRemoveTag: Story = { |
| 96 | + render: () => ( |
| 97 | + <StatefulProvisionerTagsField initialValue={{ cluster: "dogfood-2" }} /> |
| 98 | + ), |
| 99 | + play: async ({ canvasElement }) => { |
| 100 | + const user = userEvent.setup(); |
| 101 | + const canvas = within(canvasElement); |
| 102 | + const removeButton = canvas.getByRole("button", { name: "Delete cluster" }); |
| 103 | + |
| 104 | + await user.click(removeButton); |
| 105 | + |
| 106 | + await expect(canvas.queryByTestId("tag-cluster")).toBeNull(); |
| 107 | + }, |
| 108 | +}; |
0 commit comments