generated from UoaWDCC/ssr-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests for mapper function
- Loading branch information
Showing
3 changed files
with
209 additions
and
12 deletions.
There are no files selected for viewing
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,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); | ||
}); | ||
}); |
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
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, | ||
}; | ||
}); | ||
}; |