test: split following/likes tests (#33) (thanks @VACInc)

This commit is contained in:
Peter Steinberger
2026-01-12 00:46:32 +00:00
parent baede65f77
commit dec78547fe
5 changed files with 619 additions and 735 deletions
+101 -74
View File
@@ -43,53 +43,31 @@ describe('TwitterClient following pagination', () => {
vi.restoreAllMocks();
});
it('paginates following results with cursor', async () => {
mockFetch
.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({
data: {
user: {
result: {
it('returns nextCursor from the response', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({
data: {
user: {
result: {
timeline: {
timeline: {
timeline: {
instructions: [
{
entries: [
makeUserEntry('1', 'alice', 'Alice'),
{ content: { cursorType: 'Bottom', value: 'cursor-2' } },
],
},
],
},
instructions: [
{
entries: [
makeUserEntry('1', 'alice', 'Alice'),
{ content: { cursorType: 'Bottom', value: 'cursor-2' } },
],
},
],
},
},
},
},
}),
})
.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({
data: {
user: {
result: {
timeline: {
timeline: {
instructions: [
{
entries: [makeUserEntry('2', 'bob', 'Bob')],
},
],
},
},
},
},
},
}),
});
},
}),
});
const client = new TwitterClient({ cookies: validCookies });
const clientPrivate = client as unknown as TwitterClientPrivate;
@@ -99,44 +77,93 @@ describe('TwitterClient following pagination', () => {
const result = await client.getFollowing('user-id', 3);
expect(result.success).toBe(true);
expect(result.users).toHaveLength(2);
expect(result.users).toHaveLength(1);
expect(result.users?.[0].username).toBe('alice');
expect(result.users?.[1].username).toBe('bob');
expect(result.nextCursor).toBeUndefined();
expect(mockFetch).toHaveBeenCalledTimes(2);
expect(result.nextCursor).toBe('cursor-2');
expect(mockFetch).toHaveBeenCalledTimes(1);
});
it('returns error when a later page fails', async () => {
mockFetch
.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({
data: {
user: {
result: {
it('passes cursor parameter to following API and returns nextCursor', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({
data: {
user: {
result: {
timeline: {
timeline: {
timeline: {
instructions: [
{
entries: [
makeUserEntry('1', 'alice', 'Alice'),
{ content: { cursorType: 'Bottom', value: 'cursor-2' } },
],
},
],
},
instructions: [
{
entries: [
makeUserEntry('1', 'alice', 'Alice'),
{ content: { cursorType: 'Bottom', value: 'cursor-2' } },
],
},
],
},
},
},
},
}),
})
.mockResolvedValueOnce({
ok: false,
status: 500,
text: async () => 'boom',
});
},
}),
});
const client = new TwitterClient({ cookies: validCookies });
const clientPrivate = client as unknown as TwitterClientPrivate;
clientPrivate.getFollowingQueryIds = async () => ['q1'];
clientPrivate.getFollowingViaRest = vi.fn();
const result = await client.getFollowing('user-id', 20, 'prev-cursor-xyz');
expect(result.success).toBe(true);
expect(result.nextCursor).toBe('cursor-2');
const [url] = mockFetch.mock.calls[0];
const parsedVars = JSON.parse(new URL(url as string).searchParams.get('variables') as string);
expect(parsedVars.cursor).toBe('prev-cursor-xyz');
});
it('returns undefined nextCursor when no cursor in response', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({
data: {
user: {
result: {
timeline: {
timeline: {
instructions: [
{
entries: [makeUserEntry('1', 'alice', 'Alice')],
},
],
},
},
},
},
},
}),
});
const client = new TwitterClient({ cookies: validCookies });
const clientPrivate = client as unknown as TwitterClientPrivate;
clientPrivate.getFollowingQueryIds = async () => ['q1'];
clientPrivate.getFollowingViaRest = vi.fn();
const result = await client.getFollowing('user-id', 20);
expect(result.success).toBe(true);
expect(result.nextCursor).toBeUndefined();
});
it('returns an error for non-ok responses', async () => {
mockFetch.mockResolvedValueOnce({
ok: false,
status: 500,
text: async () => 'boom',
});
const client = new TwitterClient({ cookies: validCookies });
const clientPrivate = client as unknown as TwitterClientPrivate;
@@ -146,8 +173,8 @@ describe('TwitterClient following pagination', () => {
const result = await client.getFollowing('user-id', 3);
expect(result.success).toBe(false);
expect(result.users).toHaveLength(1);
expect(result.error).toContain('500');
expect(result.nextCursor).toBe('cursor-2');
expect(result.users).toBeUndefined();
expect(result.nextCursor).toBeUndefined();
});
});