Skip to content

Commit

Permalink
Sort ChooseProject list alphabetically (#2617)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Sep 21, 2023
1 parent 40bfc19 commit 3860c75
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/components/ProjectScreen/ChooseProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export default function ChooseProject(): ReactElement {
useEffect(() => {
const userId = getUserId();
if (userId) {
getAllActiveProjectsByUser(userId).then(setProjectList);
getAllActiveProjectsByUser(userId).then((projects) => {
projects.sort((a: Project, b: Project) => a.name.localeCompare(b.name));
setProjectList(projects);
});
}
}, []);

Expand Down
27 changes: 23 additions & 4 deletions src/components/ProjectScreen/tests/ChooseProject.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "tests/reactI18nextMock";
import { Project } from "api/models";
import ChooseProject from "components/ProjectScreen/ChooseProject";
import { newProject } from "types/project";
import { randomIntString } from "utilities/utilities";

jest.mock("backend", () => ({
getAllActiveProjectsByUser: () => mockGetProjects(),
Expand All @@ -19,14 +20,32 @@ jest.mock("react-router-dom", () => ({
}));

const mockGetProjects = jest.fn();
const mockProj = (id: string): Project => ({ ...newProject(id), id });
const mockProj = (name: string): Project => ({
...newProject(name),
id: randomIntString(),
});

let testRenderer: renderer.ReactTestRenderer;

it("renders with 2 projects", async () => {
mockGetProjects.mockResolvedValue([mockProj("0"), mockProj("1")]);
const hasText = (item: renderer.ReactTestInstance, text: string): boolean => {
const found = item.findAll(
(node) => node.children.length === 1 && node.children[0] === text
);
return found.length !== 0;
};

it("renders with projects in alphabetical order", async () => {
const unordered = ["In the middle", "should be last", "alphabetically first"];
mockGetProjects.mockResolvedValue(unordered.map((name) => mockProj(name)));
await renderer.act(async () => {
testRenderer = renderer.create(<ChooseProject />);
});
expect(testRenderer.root.findAllByType(ListItemButton)).toHaveLength(2);
const items = testRenderer.root.findAllByType(ListItemButton);
expect(items).toHaveLength(unordered.length);
expect(hasText(items[0], unordered[0])).toBeFalsy;
expect(hasText(items[1], unordered[1])).toBeFalsy;
expect(hasText(items[2], unordered[2])).toBeFalsy;
expect(hasText(items[0], unordered[2])).toBeTruthy;
expect(hasText(items[1], unordered[0])).toBeTruthy;
expect(hasText(items[2], unordered[1])).toBeTruthy;
});

0 comments on commit 3860c75

Please sign in to comment.