forked from xdevplatform/twitter-api-typescript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2.client.test.ts
72 lines (63 loc) · 1.98 KB
/
v2.client.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2021 Twitter, Inc.
// SPDX-License-Identifier: Apache-2.0
import { Client } from "../src";
import nock from "nock";
import Stream from "stream";
describe("test v2 client", () => {
let client: Client;
beforeEach(() => {
client = new Client("bearer-token");
});
test("rest api call", () => {
nock("https://api.twitter.com", {
reqheaders: {
'User-Agent': /^twitter-api-typescript-sdk/
}
})
.get("/2/tweets/20")
.reply(200, { data: { id: "20", text: "just setting up my twttr" } });
return client.tweets.findTweetById("20").then((tweet) =>
expect(tweet?.data).toEqual({
id: "20",
text: "just setting up my twttr",
})
);
});
test("stream api call", async () => {
const readableStream = new Stream.Readable({
objectMode: true,
read: () => undefined,
});
readableStream.push(
JSON.stringify({ data: { id: "20", text: "just setting up my twttr" } }) +
"\r\n"
);
nock("https://api.twitter.com")
.get("/2/tweets/sample/stream")
.reply(200, () => readableStream);
const stream = client.tweets.sampleStream();
for await (const item of stream) {
return expect(item).toEqual({
data: { id: "20", text: "just setting up my twttr" },
});
}
});
test("paginated api call", async () => {
nock("https://api.twitter.com")
.get("/2/users/TwitterDev/followers")
.reply(200, { data: {}, meta: { next_token: "test-next-token" } });
nock("https://api.twitter.com")
.get("/2/users/TwitterDev/followers")
.query({ pagination_token: "test-next-token" })
.reply(200, { data: {} });
const pages = client.users.usersIdFollowers("TwitterDev");
const results: Awaited<ReturnType<typeof pages["then"]>>[] = [];
for await (const test of pages) {
results.push(test);
}
expect(results).toEqual([
{ data: {}, meta: { next_token: "test-next-token" } },
{ data: {} },
]);
});
});