Skip to content

Commit

Permalink
test(services): cover base tweet-getter-service
Browse files Browse the repository at this point in the history
  • Loading branch information
louisgrasset committed Nov 23, 2023
1 parent 18a1584 commit 9fa24a0
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/services/__tests__/tweets-getter.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Scraper, Tweet } from "@the-convocation/twitter-scraper";

import { tweetsGetterService } from "../tweets-getter.service.js";

jest.mock("../../constants.js", () => ({}));

jest.mock("ora", () => ({
default: jest.fn(() => ({
start: jest
.fn()
.mockImplementation(() => ({ text: "", succeed: jest.fn() })),
})),
__esModule: true,
}));

const makeTweetMock = (update: Partial<Tweet>): Tweet => {
const text = update.text || "Hello World";
return {
id: Math.floor(
1000000000000000000 + Math.random() * 9000000000000000000,
).toString(),
conversationId: undefined,
hashtags: [],
html: text,
inReplyToStatus: undefined,
inReplyToStatusId: undefined,
isQuoted: undefined,
isReply: undefined,
isRetweet: undefined,
permanentUrl: undefined,
photos: [],
quotedStatus: undefined,
quotedStatusId: undefined,
text: text,
timestamp: Date.now(),
urls: [],
userId: "userId",
username: "username",
sensitiveContent: undefined,
...update,
// Rest, not used in the service
likes: undefined,
isPin: undefined,
isSelfThread: undefined,
mentions: [],
name: undefined,
place: undefined,
thread: [],
timeParsed: undefined,
replies: 0,
retweets: 0,
retweetedStatus: undefined,
retweetedStatusId: undefined,
videos: [],
views: undefined,
};
};

class MockTwitterClient {
public async *getTweets(
user: string,
maxTweets?: number,
): AsyncGenerator<Tweet> {
// Mocking the asynchronous generator function
for (let i = 0; i < (maxTweets || 200); i++) {
yield {
...makeTweetMock({ username: user }),
id: i.toString(),
} as Tweet;
}
}
}

describe("tweetsGetterService", () => {
it("should return a list of tweets", async () => {
const client = new MockTwitterClient();
const tweets = await tweetsGetterService(client as Scraper);
expect(tweets).toHaveLength(200);
});
});

0 comments on commit 9fa24a0

Please sign in to comment.