|
| 1 | +import type { Meta, StoryObj } from "@storybook/react-vite"; |
| 2 | +import { expect, fn, userEvent, waitFor, within } from "storybook/test"; |
| 3 | +import { |
| 4 | + MockGroupSyncSettings, |
| 5 | + MockOrganization, |
| 6 | + MockOrganizationSyncSettings, |
| 7 | + MockRoleSyncSettings, |
| 8 | +} from "#/testHelpers/entities"; |
| 9 | +import { ExportPolicyButton } from "./ExportPolicyButton"; |
| 10 | + |
| 11 | +const meta: Meta<typeof ExportPolicyButton> = { |
| 12 | + title: "modules/idpSync/ExportPolicyButton", |
| 13 | + component: ExportPolicyButton, |
| 14 | + args: { |
| 15 | + syncSettings: MockGroupSyncSettings, |
| 16 | + filename: `${MockOrganization.name}_groups-policy.json`, |
| 17 | + size: "sm", |
| 18 | + }, |
| 19 | +}; |
| 20 | + |
| 21 | +export default meta; |
| 22 | +type Story = StoryObj<typeof ExportPolicyButton>; |
| 23 | + |
| 24 | +export const Default: Story = {}; |
| 25 | + |
| 26 | +const downloadGroupPolicy = fn<(file: Blob, filename: string) => void>(); |
| 27 | +const downloadRolePolicy = fn<(file: Blob, filename: string) => void>(); |
| 28 | +const downloadOrganizationPolicy = fn<(file: Blob, filename: string) => void>(); |
| 29 | + |
| 30 | +const expectExportedPolicy = async ( |
| 31 | + canvasElement: HTMLElement, |
| 32 | + download: typeof downloadGroupPolicy, |
| 33 | + filename: string, |
| 34 | + settings: unknown, |
| 35 | +) => { |
| 36 | + const canvas = within(canvasElement); |
| 37 | + |
| 38 | + await userEvent.click(canvas.getByRole("button", { name: "Export policy" })); |
| 39 | + await waitFor(() => |
| 40 | + expect(download).toHaveBeenCalledWith(expect.anything(), filename), |
| 41 | + ); |
| 42 | + const [blob] = download.mock.lastCall ?? []; |
| 43 | + await expect(blob).toBeInstanceOf(Blob); |
| 44 | + if (!(blob instanceof Blob)) { |
| 45 | + return; |
| 46 | + } |
| 47 | + await expect(blob.type).toEqual("application/json"); |
| 48 | + await expect(await blob.text()).toEqual(JSON.stringify(settings, null, 2)); |
| 49 | +}; |
| 50 | + |
| 51 | +export const ClickExportGroupPolicy: Story = { |
| 52 | + args: { |
| 53 | + syncSettings: MockGroupSyncSettings, |
| 54 | + filename: `${MockOrganization.name}_groups-policy.json`, |
| 55 | + size: "sm", |
| 56 | + download: downloadGroupPolicy, |
| 57 | + }, |
| 58 | + play: async ({ canvasElement }) => { |
| 59 | + await expectExportedPolicy( |
| 60 | + canvasElement, |
| 61 | + downloadGroupPolicy, |
| 62 | + `${MockOrganization.name}_groups-policy.json`, |
| 63 | + MockGroupSyncSettings, |
| 64 | + ); |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +export const ClickExportRolePolicy: Story = { |
| 69 | + args: { |
| 70 | + syncSettings: MockRoleSyncSettings, |
| 71 | + filename: `${MockOrganization.name}_roles-policy.json`, |
| 72 | + size: "sm", |
| 73 | + download: downloadRolePolicy, |
| 74 | + }, |
| 75 | + play: async ({ canvasElement }) => { |
| 76 | + await expectExportedPolicy( |
| 77 | + canvasElement, |
| 78 | + downloadRolePolicy, |
| 79 | + `${MockOrganization.name}_roles-policy.json`, |
| 80 | + MockRoleSyncSettings, |
| 81 | + ); |
| 82 | + }, |
| 83 | +}; |
| 84 | + |
| 85 | +export const ClickExportOrganizationPolicy: Story = { |
| 86 | + args: { |
| 87 | + syncSettings: MockOrganizationSyncSettings, |
| 88 | + filename: "organizations_policy.json", |
| 89 | + download: downloadOrganizationPolicy, |
| 90 | + }, |
| 91 | + play: async ({ canvasElement }) => { |
| 92 | + await expectExportedPolicy( |
| 93 | + canvasElement, |
| 94 | + downloadOrganizationPolicy, |
| 95 | + "organizations_policy.json", |
| 96 | + MockOrganizationSyncSettings, |
| 97 | + ); |
| 98 | + }, |
| 99 | +}; |
0 commit comments