-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
182 additions
and
73 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/app/machines/views/MachineList/MachineListTable/GroupColumn/GroupColumn.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import GroupColumn from "./GroupColumn"; | ||
|
||
import { FetchGroupKey } from "app/store/machine/types"; | ||
import { useFetchMachineCount } from "app/store/machine/utils/hooks"; | ||
import { machineStateListGroup as machineStateListGroupFactory } from "testing/factories"; | ||
import { renderWithMockStore, screen, waitFor } from "testing/utils"; | ||
|
||
jest.mock("app/store/machine/utils/hooks"); | ||
|
||
const mockedUseFetchMachineCount = useFetchMachineCount as jest.MockedFunction< | ||
typeof useFetchMachineCount | ||
>; | ||
mockedUseFetchMachineCount.mockReturnValue({ | ||
machineCountLoading: false, | ||
machineCountLoaded: true, | ||
machineCount: 2, | ||
}); | ||
|
||
it("displays the correct column name and machines count", () => { | ||
const group = machineStateListGroupFactory({ | ||
collapsed: false, | ||
count: 5, | ||
name: "Test Group", | ||
value: "test-group", | ||
}); | ||
renderWithMockStore( | ||
<GroupColumn | ||
callId="test-call-id" | ||
filter={null} | ||
group={group} | ||
grouping={FetchGroupKey.Status} | ||
hiddenGroups={[null]} | ||
setHiddenGroups={jest.fn()} | ||
showActions={false} | ||
/> | ||
); | ||
|
||
expect(screen.getByText(/Test Group/)).toBeInTheDocument(); | ||
expect(screen.getByText(/5 machines/)).toBeInTheDocument(); | ||
}); | ||
|
||
it("displays correct fetched machines count when initial count is null", async () => { | ||
mockedUseFetchMachineCount.mockReturnValue({ | ||
machineCountLoading: false, | ||
machineCountLoaded: true, | ||
machineCount: 2, | ||
}); | ||
const group = machineStateListGroupFactory({ | ||
collapsed: false, | ||
count: null, | ||
name: "Test Group", | ||
value: "test-group", | ||
}); | ||
|
||
renderWithMockStore( | ||
<GroupColumn | ||
callId="test-call-id" | ||
filter={null} | ||
group={group} | ||
grouping={FetchGroupKey.Status} | ||
hiddenGroups={[null]} | ||
setHiddenGroups={jest.fn()} | ||
showActions={false} | ||
/> | ||
); | ||
|
||
expect(screen.getByText(/Test Group/)).toBeInTheDocument(); | ||
await waitFor(() => | ||
expect(screen.getByText(/2 machines/)).toBeInTheDocument() | ||
); | ||
}); |
86 changes: 86 additions & 0 deletions
86
src/app/machines/views/MachineList/MachineListTable/GroupColumn/GroupColumn.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Button } from "@canonical/react-components"; | ||
|
||
import DoubleRow from "app/base/components/DoubleRow"; | ||
import GroupCheckbox from "app/machines/views/MachineList/MachineListTable/GroupCheckbox"; | ||
import MachineListGroupCount from "app/machines/views/MachineList/MachineListTable/MachineListGroupCount"; | ||
import type { GroupRowsProps } from "app/machines/views/MachineList/MachineListTable/types"; | ||
import type { MachineStateListGroup } from "app/store/machine/types"; | ||
|
||
export enum Label { | ||
HideGroup = "Hide", | ||
ShowGroup = "Show", | ||
} | ||
|
||
const GroupColumn = ({ | ||
group, | ||
hiddenGroups, | ||
setHiddenGroups, | ||
showActions, | ||
callId, | ||
grouping, | ||
filter, | ||
}: Pick< | ||
GroupRowsProps, | ||
"hiddenGroups" | "setHiddenGroups" | "showActions" | "callId" | "filter" | ||
> & { | ||
grouping: NonNullable<GroupRowsProps["grouping"]>; | ||
group: MachineStateListGroup; | ||
}) => { | ||
const { collapsed, count, name, value } = group; | ||
return ( | ||
<> | ||
<DoubleRow | ||
data-testid="group-cell" | ||
primary={ | ||
showActions ? ( | ||
<GroupCheckbox | ||
callId={callId} | ||
group={group} | ||
groupName={name} | ||
grouping={grouping} | ||
/> | ||
) : ( | ||
<strong>{name}</strong> | ||
) | ||
} | ||
secondary={ | ||
<MachineListGroupCount | ||
count={count} | ||
filter={filter} | ||
group={value} | ||
grouping={grouping} | ||
/> | ||
} | ||
secondaryClassName={ | ||
showActions ? "u-nudge--secondary-row u-align--left" : null | ||
} | ||
/> | ||
<div className="machine-list__group-toggle"> | ||
<Button | ||
appearance="base" | ||
dense | ||
hasIcon | ||
onClick={() => { | ||
if (collapsed) { | ||
setHiddenGroups && | ||
setHiddenGroups( | ||
hiddenGroups.filter((hiddenGroup) => hiddenGroup !== value) | ||
); | ||
} else { | ||
setHiddenGroups && | ||
setHiddenGroups(hiddenGroups.concat([value as string])); | ||
} | ||
}} | ||
> | ||
{collapsed ? ( | ||
<i className="p-icon--plus">{Label.ShowGroup}</i> | ||
) : ( | ||
<i className="p-icon--minus">{Label.HideGroup}</i> | ||
)} | ||
</Button> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default GroupColumn; |
1 change: 1 addition & 0 deletions
1
src/app/machines/views/MachineList/MachineListTable/GroupColumn/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, Label } from "./GroupColumn"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters