|
| 1 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 2 | +import { expect, fn, userEvent, waitFor, within } from "@storybook/test"; |
| 3 | +import { |
| 4 | + MockGroupSyncSettings, |
| 5 | + MockOrganization, |
| 6 | + MockRoleSyncSettings, |
| 7 | +} from "testHelpers/entities"; |
| 8 | +import { ExportPolicyButton } from "./ExportPolicyButton"; |
| 9 | + |
| 10 | +const meta: Meta<typeof ExportPolicyButton> = { |
| 11 | + title: "modules/resources/ExportPolicyButton", |
| 12 | + component: ExportPolicyButton, |
| 13 | + args: { |
| 14 | + syncSettings: MockGroupSyncSettings, |
| 15 | + type: "groups", |
| 16 | + organization: MockOrganization, |
| 17 | + }, |
| 18 | +}; |
| 19 | + |
| 20 | +export default meta; |
| 21 | +type Story = StoryObj<typeof ExportPolicyButton>; |
| 22 | + |
| 23 | +export const Default: Story = {}; |
| 24 | + |
| 25 | +export const ClickExportGroupPolicy: Story = { |
| 26 | + args: { |
| 27 | + syncSettings: MockGroupSyncSettings, |
| 28 | + type: "groups", |
| 29 | + organization: MockOrganization, |
| 30 | + download: fn(), |
| 31 | + }, |
| 32 | + play: async ({ canvasElement, args }) => { |
| 33 | + const canvas = within(canvasElement); |
| 34 | + await userEvent.click( |
| 35 | + canvas.getByRole("button", { name: "Export Policy" }), |
| 36 | + ); |
| 37 | + await waitFor(() => |
| 38 | + expect(args.download).toHaveBeenCalledWith( |
| 39 | + expect.anything(), |
| 40 | + `${MockOrganization.name}_groups-policy.json`, |
| 41 | + ), |
| 42 | + ); |
| 43 | + const blob: Blob = (args.download as jest.Mock).mock.lastCall[0]; |
| 44 | + await expect(blob.type).toEqual("application/json"); |
| 45 | + await expect(await blob.text()).toEqual( |
| 46 | + JSON.stringify(MockGroupSyncSettings, null, 2), |
| 47 | + ); |
| 48 | + }, |
| 49 | +}; |
| 50 | + |
| 51 | +export const ClickExportRolePolicy: Story = { |
| 52 | + args: { |
| 53 | + syncSettings: MockRoleSyncSettings, |
| 54 | + type: "roles", |
| 55 | + organization: MockOrganization, |
| 56 | + download: fn(), |
| 57 | + }, |
| 58 | + play: async ({ canvasElement, args }) => { |
| 59 | + const canvas = within(canvasElement); |
| 60 | + await userEvent.click( |
| 61 | + canvas.getByRole("button", { name: "Export Policy" }), |
| 62 | + ); |
| 63 | + await waitFor(() => |
| 64 | + expect(args.download).toHaveBeenCalledWith( |
| 65 | + expect.anything(), |
| 66 | + `${MockOrganization.name}_roles-policy.json`, |
| 67 | + ), |
| 68 | + ); |
| 69 | + const blob: Blob = (args.download as jest.Mock).mock.lastCall[0]; |
| 70 | + await expect(blob.type).toEqual("application/json"); |
| 71 | + await expect(await blob.text()).toEqual( |
| 72 | + JSON.stringify(MockRoleSyncSettings, null, 2), |
| 73 | + ); |
| 74 | + }, |
| 75 | +}; |
0 commit comments