|
1 | 1 | import type { Meta, StoryObj } from "@storybook/react";
|
2 | 2 | import { expect, userEvent, waitFor, within } from "@storybook/test";
|
| 3 | +import type { AuthorizationResponse } from "api/typesGenerated"; |
3 | 4 | import {
|
4 | 5 | MockNoPermissions,
|
5 | 6 | MockOrganization,
|
6 | 7 | MockOrganization2,
|
7 | 8 | MockPermissions,
|
8 | 9 | } from "testHelpers/entities";
|
9 | 10 | import { withDashboardProvider } from "testHelpers/storybook";
|
10 |
| -import { OrganizationSidebarView } from "./OrganizationSidebarView"; |
| 11 | +import { |
| 12 | + OrganizationSidebarView, |
| 13 | + type OrganizationWithPermissions, |
| 14 | +} from "./OrganizationSidebarView"; |
11 | 15 |
|
12 | 16 | const meta: Meta<typeof OrganizationSidebarView> = {
|
13 | 17 | title: "modules/management/OrganizationSidebarView",
|
@@ -286,3 +290,114 @@ export const OrgsDisabled: Story = {
|
286 | 290 | showOrganizations: false,
|
287 | 291 | },
|
288 | 292 | };
|
| 293 | + |
| 294 | +const commonPerms: AuthorizationResponse = { |
| 295 | + editOrganization: true, |
| 296 | + editMembers: true, |
| 297 | + editGroups: true, |
| 298 | + auditOrganization: true, |
| 299 | +}; |
| 300 | + |
| 301 | +const activeOrganization: OrganizationWithPermissions = { |
| 302 | + ...MockOrganization, |
| 303 | + display_name: "Omega org", |
| 304 | + name: "omega", |
| 305 | + id: "1", |
| 306 | + permissions: { |
| 307 | + ...commonPerms, |
| 308 | + }, |
| 309 | +}; |
| 310 | + |
| 311 | +export const OrgsSortedAlphabetically: Story = { |
| 312 | + args: { |
| 313 | + activeOrganization, |
| 314 | + permissions: { |
| 315 | + ...MockPermissions, |
| 316 | + createOrganization: true, |
| 317 | + }, |
| 318 | + organizations: [ |
| 319 | + { |
| 320 | + ...MockOrganization, |
| 321 | + display_name: "Zeta Org", |
| 322 | + id: "2", |
| 323 | + name: "zeta", |
| 324 | + permissions: commonPerms, |
| 325 | + }, |
| 326 | + { |
| 327 | + ...MockOrganization, |
| 328 | + display_name: "alpha Org", |
| 329 | + id: "3", |
| 330 | + name: "alpha", |
| 331 | + permissions: commonPerms, |
| 332 | + }, |
| 333 | + activeOrganization, |
| 334 | + ], |
| 335 | + }, |
| 336 | + play: async ({ canvasElement }) => { |
| 337 | + const canvas = within(canvasElement); |
| 338 | + await userEvent.click(canvas.getByRole("button", { name: /Omega org/i })); |
| 339 | + |
| 340 | + // dropdown is not in #storybook-root so must query full document |
| 341 | + const globalScreen = within(document.body); |
| 342 | + |
| 343 | + await waitFor(() => { |
| 344 | + expect(globalScreen.queryByText("alpha Org")).toBeInTheDocument(); |
| 345 | + expect(globalScreen.queryByText("Zeta Org")).toBeInTheDocument(); |
| 346 | + }); |
| 347 | + |
| 348 | + const orgElements = globalScreen.getAllByRole("option"); |
| 349 | + // filter out Create btn |
| 350 | + const filteredElems = orgElements.slice(0, 3); |
| 351 | + |
| 352 | + const orgNames = filteredElems.map( |
| 353 | + // handling fuzzy matching |
| 354 | + (el) => el.textContent?.replace(/^[A-Z]/, "").trim() || "", |
| 355 | + ); |
| 356 | + |
| 357 | + // active name first |
| 358 | + expect(orgNames).toEqual(["Omega org", "alpha Org", "Zeta Org"]); |
| 359 | + }, |
| 360 | +}; |
| 361 | + |
| 362 | +export const SearchForOrg: Story = { |
| 363 | + args: { |
| 364 | + activeOrganization, |
| 365 | + permissions: MockPermissions, |
| 366 | + organizations: [ |
| 367 | + { |
| 368 | + ...MockOrganization, |
| 369 | + display_name: "Zeta Org", |
| 370 | + id: "2", |
| 371 | + name: "zeta", |
| 372 | + permissions: commonPerms, |
| 373 | + }, |
| 374 | + { |
| 375 | + ...MockOrganization, |
| 376 | + display_name: "alpha Org", |
| 377 | + id: "3", |
| 378 | + name: "fish", |
| 379 | + permissions: commonPerms, |
| 380 | + }, |
| 381 | + activeOrganization, |
| 382 | + ], |
| 383 | + }, |
| 384 | + play: async ({ canvasElement }) => { |
| 385 | + const canvas = within(canvasElement); |
| 386 | + await userEvent.click(canvas.getByRole("button", { name: /Omega org/i })); |
| 387 | + |
| 388 | + // dropdown is not in #storybook-root so must query full document |
| 389 | + const globalScreen = within(document.body); |
| 390 | + const searchInput = |
| 391 | + await globalScreen.getByPlaceholderText("Find organization"); |
| 392 | + |
| 393 | + await userEvent.type(searchInput, "ALPHA"); |
| 394 | + |
| 395 | + const filteredResult = await globalScreen.findByText("alpha Org"); |
| 396 | + expect(filteredResult).toBeInTheDocument(); |
| 397 | + |
| 398 | + // Omega org remains visible as the default org |
| 399 | + await waitFor(() => { |
| 400 | + expect(globalScreen.queryByText("Zeta Org")).not.toBeInTheDocument(); |
| 401 | + }); |
| 402 | + }, |
| 403 | +}; |
0 commit comments