|
| 1 | +import { fromJS } from "immutable"; |
| 2 | + |
| 3 | +import { mountedComponent, screen, userEvent } from "../../../../test-utils"; |
| 4 | +import { listHeaders, lookups } from "../../../../test"; |
| 5 | +import { ACTIONS } from "../../../permissions"; |
| 6 | + |
| 7 | +import NAMESPACE from "./namespace"; |
| 8 | +import UserGroupsList from "./container"; |
| 9 | + |
| 10 | +describe("<UserGroupsList />", () => { |
| 11 | + const dataLength = 30; |
| 12 | + const data = Array.from({ length: dataLength }, (_, i) => ({ |
| 13 | + id: i + 1, |
| 14 | + unique_id: `usergroup-${i + 1}`, |
| 15 | + name: `User Group ${i + 1}`, |
| 16 | + description: `Test description ${i + 1}` |
| 17 | + })); |
| 18 | + |
| 19 | + const initialState = fromJS({ |
| 20 | + records: { |
| 21 | + user_groups: { |
| 22 | + data, |
| 23 | + metadata: { total: dataLength, per: 20, page: 1 }, |
| 24 | + loading: false, |
| 25 | + errors: false |
| 26 | + } |
| 27 | + }, |
| 28 | + user: { |
| 29 | + permissions: { |
| 30 | + users: [ACTIONS.MANAGE] |
| 31 | + }, |
| 32 | + listHeaders: { |
| 33 | + user_groups: listHeaders(NAMESPACE) |
| 34 | + } |
| 35 | + }, |
| 36 | + forms: { |
| 37 | + options: { |
| 38 | + lookups: lookups() |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + it("should render record list table", () => { |
| 44 | + mountedComponent(<UserGroupsList />, initialState, {}, [`/admin/${NAMESPACE}`]); |
| 45 | + expect(screen.getByRole("grid")).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("triggers a sort action when a header is clicked", async () => { |
| 49 | + const { store } = mountedComponent(<UserGroupsList />, initialState, {}, [`/admin/${NAMESPACE}`]); |
| 50 | + const user = userEvent.setup(); |
| 51 | + const expectedAction = { |
| 52 | + payload: { |
| 53 | + data: fromJS({ |
| 54 | + disabled: ["false"], |
| 55 | + total: 30, |
| 56 | + per: 20, |
| 57 | + page: 1, |
| 58 | + order: "asc", |
| 59 | + order_by: "name" |
| 60 | + }) |
| 61 | + }, |
| 62 | + type: "user_groups/SET_USER_GROUPS_FILTER" |
| 63 | + }; |
| 64 | + |
| 65 | + const columnHeader = screen.getByTestId("headcol-0"); |
| 66 | + |
| 67 | + await user.click(columnHeader); |
| 68 | + |
| 69 | + expect(expectedAction).toEqual(store.getActions()[2]); |
| 70 | + }); |
| 71 | + |
| 72 | + it.skip("goes to a new page when clicking next page", async () => { |
| 73 | + // TODO: This test does not work because the rest middleware is required |
| 74 | + const user = userEvent.setup(); |
| 75 | + |
| 76 | + mountedComponent(<UserGroupsList />, initialState, {}, [`/admin/${NAMESPACE}`]); |
| 77 | + |
| 78 | + await user.click(screen.getByTestId("pagination-next")); |
| 79 | + |
| 80 | + expect(screen.getByText("21-30 messages.record_list.of 30")).toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should set the filters when apply is clicked", async () => { |
| 84 | + const user = userEvent.setup(); |
| 85 | + const { store } = mountedComponent(<UserGroupsList />, initialState, {}, [`/admin/${NAMESPACE}`]); |
| 86 | + |
| 87 | + const expectedAction = { |
| 88 | + payload: { data: fromJS({ disabled: ["false"], total: 30, per: 20, page: 1 }) }, |
| 89 | + type: "user_groups/SET_USER_GROUPS_FILTER" |
| 90 | + }; |
| 91 | + |
| 92 | + await user.click(screen.getByText("filters.apply_filters")); |
| 93 | + |
| 94 | + const action = store.getActions()[1]; |
| 95 | + |
| 96 | + expect(action).toEqual(expectedAction); |
| 97 | + }); |
| 98 | +}); |
0 commit comments