-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.test.ts
More file actions
68 lines (61 loc) · 2.36 KB
/
api.test.ts
File metadata and controls
68 lines (61 loc) · 2.36 KB
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
import { describe, expect, test, beforeEach, afterEach } from 'bun:test';
import { getNodeTypes, getTaskStatuses, searchNodes } from './api.js';
describe('nodeTypes API', () => {
let savedFetch: typeof globalThis.fetch;
beforeEach(() => {
savedFetch = globalThis.fetch;
});
afterEach(() => {
globalThis.fetch = savedFetch;
});
test('getNodeTypes returns node_types array on 200', async () => {
globalThis.fetch = (async () =>
({
ok: true,
json: () => Promise.resolve({ node_types: [{ id: 't1', name: 'Task' }] })
}) as Response) as any as typeof globalThis.fetch;
const rows = await getNodeTypes();
expect(rows).toEqual([{ id: 't1', name: 'Task' }]);
});
test('getNodeTypes throws on non-OK response', async () => {
globalThis.fetch = (async () =>
({
ok: false,
status: 500,
json: () => Promise.resolve({ error: 'nope' })
}) as Response) as any as typeof globalThis.fetch;
await expect(getNodeTypes()).rejects.toThrow('nope');
});
test('getTaskStatuses returns task_statuses array on 200', async () => {
globalThis.fetch = (async () =>
({
ok: true,
json: () => Promise.resolve({ task_statuses: [{ id: 's1' }] })
}) as Response) as any as typeof globalThis.fetch;
const rows = await getTaskStatuses();
expect(rows).toEqual([{ id: 's1' }]);
});
test('searchNodes returns hits array on 200', async () => {
const g = globalThis as typeof globalThis & { window?: { location: { origin: string } } };
const prev = g.window;
g.window = { location: { origin: 'http://localhost' } } as any;
try {
const hit = {
id: 'n1',
title: 'T',
project_id: 'p1',
project_title: 'P',
status_id: 's1'
};
globalThis.fetch = (async () =>
({
ok: true,
json: () => Promise.resolve([hit])
}) as Response) as any as typeof globalThis.fetch;
const rows = await searchNodes('q', 'p0');
expect(rows).toEqual([hit]);
} finally {
g.window = prev;
}
});
});