Skip to content

Commit

Permalink
added unit tests for mapper function
Browse files Browse the repository at this point in the history
  • Loading branch information
nroh555 committed Jun 7, 2024
1 parent 3825257 commit 2c7929d
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 12 deletions.
192 changes: 192 additions & 0 deletions web/__test__/MapToExec.test.tsx
Original file line number Diff line number Diff line change
@@ -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);
});
});
13 changes: 1 addition & 12 deletions web/src/screens/ExecScreen.tsx
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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,
};
});
};
16 changes: 16 additions & 0 deletions web/src/utils/mapToExec.ts
Original file line number Diff line number Diff line change
@@ -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,
};
});
};

0 comments on commit 2c7929d

Please sign in to comment.