diff --git a/web/__test__/MapToExec.test.tsx b/web/__test__/MapToExec.test.tsx new file mode 100644 index 00000000..7e288024 --- /dev/null +++ b/web/__test__/MapToExec.test.tsx @@ -0,0 +1,192 @@ +import { describe, expect, it } from "vitest"; +import { mapToExec } from "../src/utils/mapToExec"; +import { Exec } from "../src/types/types"; + +describe("mapToExec", () => { + it("should map valid data correctly", () => { + const data = { + data: [ + { + id: 1, + attributes: { + name: "John Doe", + bio: "A great leader", + position: "President", + image: { + data: { + attributes: { + url: "/uploads/john_doe.jpg", + }, + }, + }, + }, + }, + ], + }; + + const expected: Exec[] = [ + { + id: 1, + name: "John Doe", + bio: "A great leader", + position: "President", + image: "/uploads/john_doe.jpg", + }, + ]; + + expect(mapToExec(data)).toEqual(expected); + }); + + it("should handle missing image field gracefully", () => { + const data = { + data: [ + { + id: 1, + attributes: { + name: "Jane Doe", + bio: "A great vice president", + position: "Vice President", + image: null, + }, + }, + ], + }; + + const expected: Exec[] = [ + { + id: 1, + name: "Jane Doe", + bio: "A great vice president", + position: "Vice President", + image: "", + }, + ]; + + expect(mapToExec(data)).toEqual(expected); + }); + + it("should handle missing name field gracefully", () => { + const data = { + data: [ + { + id: 1, + attributes: { + name: null, + bio: "A great treasurer", + position: "Treasurer", + image: { + data: { + attributes: { + url: "/uploads/jane_doe.jpg", + }, + }, + }, + }, + }, + ], + }; + + const expected: Exec[] = [ + { + id: 1, + name: "", + bio: "A great treasurer", + position: "Treasurer", + image: "/uploads/jane_doe.jpg", + }, + ]; + + expect(mapToExec(data)).toEqual(expected); + }); + + it("should handle missing bio field gracefully", () => { + const data = { + data: [ + { + id: 1, + attributes: { + name: "John Smith", + bio: null, + position: "Secretary", + image: { + data: { + attributes: { + url: "/uploads/john_smith.jpg", + }, + }, + }, + }, + }, + ], + }; + + const expected: Exec[] = [ + { + id: 1, + name: "John Smith", + bio: "", + position: "Secretary", + image: "/uploads/john_smith.jpg", + }, + ]; + + expect(mapToExec(data)).toEqual(expected); + }); + + it("should handle missing position field gracefully", () => { + const data = { + data: [ + { + id: 1, + attributes: { + name: "John Smith", + bio: "A great secretary", + position: null, + image: { + data: { + attributes: { + url: "/uploads/john_smith.jpg", + }, + }, + }, + }, + }, + ], + }; + + const expected: Exec[] = [ + { + id: 1, + name: "John Smith", + bio: "A great secretary", + position: "", + image: "/uploads/john_smith.jpg", + }, + ]; + + expect(mapToExec(data)).toEqual(expected); + }); + + it("should handle completely missing attributes gracefully", () => { + const data = { + data: [ + { + id: 1, + attributes: null, + }, + ], + }; + + const expected: Exec[] = [ + { + id: 1, + name: "", + bio: "", + position: "", + image: "", + }, + ]; + + expect(mapToExec(data)).toEqual(expected); + }); +}); diff --git a/web/src/screens/ExecScreen.tsx b/web/src/screens/ExecScreen.tsx index 132526ac..bc5fa96c 100644 --- a/web/src/screens/ExecScreen.tsx +++ b/web/src/screens/ExecScreen.tsx @@ -1,6 +1,7 @@ import LoadingSpinner from "@components/LoadingSpinner"; import useFetch from "../hooks/useFetch"; import { Exec } from "../types/types"; +import { mapToExec } from "@utils/mapToExec"; function ExecScreen() { const { loading, data, error } = useFetch( @@ -36,15 +37,3 @@ function ExecScreen() { } export default ExecScreen; - -const mapToExec = (data: any): Exec[] => { - return data.data.map((item: any) => { - return { - id: item.id, - name: item.attributes.name, - bio: item.attributes.bio, - position: item.attributes.position, - image: item.attributes.image.data.attributes.url, - }; - }); -}; diff --git a/web/src/utils/mapToExec.ts b/web/src/utils/mapToExec.ts new file mode 100644 index 00000000..6a879d2a --- /dev/null +++ b/web/src/utils/mapToExec.ts @@ -0,0 +1,16 @@ +import { Exec } from "../types/types"; + +export const mapToExec = (data: any): Exec[] => { + return data.data.map((item: any) => { + const attributes = item.attributes || {}; + const imageUrl = attributes.image?.data?.attributes?.url || ""; + + return { + id: item.id, + name: attributes.name || "", + bio: attributes.bio || "", + position: attributes.position || "", + image: imageUrl, + }; + }); +};