Skip to content

Commit 374efe4

Browse files
committed
Fix post image to use .shift() instead of .pop()
1 parent c9b6736 commit 374efe4

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed

bun.lockb

347 Bytes
Binary file not shown.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"devDependencies": {
2828
"@eslint/js": "^9.13.0",
29+
"@faker-js/faker": "^9.1.0",
2930
"@types/bun": "latest",
3031
"eslint": "^9.13.0",
3132
"eslint-config-prettier": "^9.1.0",

src/adapters/posts.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export function adaptPost(post: Discussion): Post {
1818
: undefined,
1919
category: post.category,
2020
tags: post.json_metadata.tags,
21-
image: post.json_metadata.image?.pop(),
21+
image: post.json_metadata.image?.length
22+
? post.json_metadata.image[0]
23+
: undefined,
2224
canonical: post.url,
2325
content: post.body,
2426
meta: {

src/tests/adapters/posts.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, test } from "bun:test";
2+
3+
import { adaptPost } from "~/adapters/posts.ts";
4+
import { fakeDiscussion } from "~/tests/mocks.ts";
5+
6+
describe("adaptPost", () => {
7+
test("should return the first image from the post", () => {
8+
const discussion = fakeDiscussion();
9+
const post = adaptPost(discussion);
10+
11+
expect(post.image).toBe(discussion.json_metadata?.image[0]);
12+
expect(post.community?.id).toBe(discussion.community);
13+
expect(post.community?.name).toBe(discussion.community_title);
14+
});
15+
});
File renamed without changes.

src/tests/mocks.ts

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { faker } from "@faker-js/faker";
2+
import type { Discussion } from "@hiveio/dhive";
3+
4+
export function fakeDiscussion(): Discussion {
5+
return {
6+
abs_rshares: "",
7+
active: "",
8+
active_votes: [],
9+
allow_curation_rewards: false,
10+
allow_replies: false,
11+
allow_votes: false,
12+
author: faker.internet.username(),
13+
author_reputation: 0,
14+
author_rewards: "",
15+
beneficiaries: [],
16+
body: faker.lorem.paragraphs(2),
17+
body_length: "",
18+
cashout_time: "",
19+
category: faker.lorem.word(),
20+
children: 0,
21+
children_abs_rshares: "",
22+
created: "",
23+
curator_payout_value: "",
24+
depth: 0,
25+
post_id: faker.number.int(),
26+
// @ts-expect-error It actually comes in as an object, not string
27+
json_metadata: {
28+
app: faker.internet.jwt(),
29+
description: faker.lorem.sentences(2),
30+
format: "markdown+html",
31+
tags: faker.lorem.words(5).split(" "),
32+
image: faker.helpers.multiple(() => faker.image.url(), {
33+
count: { min: 0, max: 5 }
34+
})
35+
},
36+
last_payout: "",
37+
last_update: "",
38+
max_accepted_payout: "",
39+
max_cashout_time: "",
40+
net_rshares: "",
41+
net_votes: 0,
42+
parent_author: "",
43+
parent_permlink: "",
44+
pending_payout_value: "",
45+
percent_hbd: 0,
46+
permlink: faker.internet.url(),
47+
promoted: "",
48+
reblogged_by: [],
49+
replies: [],
50+
reward_weight: 0,
51+
root_comment: 0,
52+
root_title: "",
53+
title: faker.lorem.words(5),
54+
total_payout_value: "",
55+
total_pending_payout_value: "",
56+
total_vote_weight: 0,
57+
url: faker.internet.url(),
58+
vote_rshares: "",
59+
community: `hive-${faker.number.int()}`,
60+
community_title: faker.lorem.words(2)
61+
};
62+
}
63+
64+
// export function fakePost(): Post {
65+
// const created = faker.date.past();
66+
//
67+
// return {
68+
// id: faker.number.int().toString(),
69+
// canonical: faker.internet.url(),
70+
// category: faker.lorem.word(),
71+
// content: faker.lorem.paragraphs(2),
72+
// created: created,
73+
// updated: faker.date.past({ refDate: created }),
74+
// description: faker.lorem.sentences(2),
75+
// meta: { app: faker.internet.jwt(), format: "markdown+html" },
76+
// tags: faker.lorem.words(5).split(" "),
77+
// title: faker.lorem.words(5),
78+
// author: faker.internet.username(),
79+
// image: faker.image.url()
80+
// };
81+
// }

0 commit comments

Comments
 (0)