classnames.spec.js
1 month ago
dateUtils.spec.js
1 month ago
formatters.spec.js
1 month ago
pluginUtils.spec.js
1 month ago
transformers.spec.js
1 month ago
transformers.spec.js
104 lines
| 1 | import { |
| 2 | transformMediaItem, |
| 3 | transformUserItem, |
| 4 | } from "../transformers"; |
| 5 | |
| 6 | describe("transformers", () => { |
| 7 | describe("transformMediaItem", () => { |
| 8 | it("flattens the API shape into the component shape", () => { |
| 9 | const item = { |
| 10 | video: { id: 1, post_id: 100, title: "Hello" }, |
| 11 | stats: [{ data: "42 views" }, { data: "1m 30s" }], |
| 12 | poster_image: "https://cdn/p.jpg", |
| 13 | }; |
| 14 | expect(transformMediaItem(item)).toEqual({ |
| 15 | id: 1, |
| 16 | postId: 100, |
| 17 | title: "Hello", |
| 18 | totalViews: "42", |
| 19 | avgViewTime: "1m 30s", |
| 20 | poster_image: "https://cdn/p.jpg", |
| 21 | }); |
| 22 | }); |
| 23 | |
| 24 | it("strips commas from view counts in stats[0].data", () => { |
| 25 | const item = { |
| 26 | video: { id: 2, post_id: 2, title: "A" }, |
| 27 | stats: [{ data: "1,234,567 views" }, { data: "0s" }], |
| 28 | }; |
| 29 | expect(transformMediaItem(item).totalViews).toBe("1234567"); |
| 30 | }); |
| 31 | |
| 32 | it("falls back to '0' views and '0s' avg when stats are missing", () => { |
| 33 | const item = { video: { id: 3, post_id: 3, title: "A" } }; |
| 34 | const out = transformMediaItem(item); |
| 35 | expect(out.totalViews).toBe("0"); |
| 36 | expect(out.avgViewTime).toBe("0s"); |
| 37 | }); |
| 38 | |
| 39 | it("falls back to localized 'Untitled' when title is empty", () => { |
| 40 | const out = transformMediaItem({ |
| 41 | video: { id: 4, post_id: 4, title: "" }, |
| 42 | stats: [], |
| 43 | }); |
| 44 | expect(out.title).toBe("Untitled"); |
| 45 | }); |
| 46 | |
| 47 | it("decodes HTML entities in the title", () => { |
| 48 | const out = transformMediaItem({ |
| 49 | video: { id: 5, post_id: 5, title: "John's & Co" }, |
| 50 | stats: [], |
| 51 | }); |
| 52 | expect(out.title).toBe("John's & Co"); |
| 53 | }); |
| 54 | |
| 55 | it("defaults postId to null when video.post_id is absent", () => { |
| 56 | const out = transformMediaItem({ |
| 57 | video: { id: 6, title: "x" }, |
| 58 | stats: [], |
| 59 | }); |
| 60 | expect(out.postId).toBeNull(); |
| 61 | }); |
| 62 | |
| 63 | it("defaults poster_image to null when not provided", () => { |
| 64 | const out = transformMediaItem({ |
| 65 | video: { id: 7, post_id: 7, title: "x" }, |
| 66 | stats: [], |
| 67 | }); |
| 68 | expect(out.poster_image).toBeNull(); |
| 69 | }); |
| 70 | |
| 71 | it("returns '0' totalViews when stats[0].data has no digits", () => { |
| 72 | const item = { |
| 73 | video: { id: 8, post_id: 8, title: "x" }, |
| 74 | stats: [{ data: "no number here" }, { data: "1s" }], |
| 75 | }; |
| 76 | expect(transformMediaItem(item).totalViews).toBe("0"); |
| 77 | }); |
| 78 | }); |
| 79 | |
| 80 | describe("transformUserItem", () => { |
| 81 | it("flattens the API shape into the component shape", () => { |
| 82 | const item = { |
| 83 | user: { id: 11, name: "Ada Lovelace" }, |
| 84 | stats: [{ data: "9 views" }, { data: "30s" }], |
| 85 | }; |
| 86 | expect(transformUserItem(item)).toEqual({ |
| 87 | id: 11, |
| 88 | name: "Ada Lovelace", |
| 89 | totalViews: "9", |
| 90 | avgViewTime: "30s", |
| 91 | }); |
| 92 | }); |
| 93 | |
| 94 | it("falls back to localized 'Unknown User' when name is empty", () => { |
| 95 | const out = transformUserItem({ |
| 96 | user: { id: 13, name: "" }, |
| 97 | stats: [], |
| 98 | }); |
| 99 | expect(out.name).toBe("Unknown User"); |
| 100 | }); |
| 101 | |
| 102 | }); |
| 103 | }); |
| 104 |