-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapi.spec.ts
More file actions
100 lines (85 loc) · 2.73 KB
/
api.spec.ts
File metadata and controls
100 lines (85 loc) · 2.73 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { server } from './mock'
import { API } from '../src'
import { http, HttpResponse } from 'msw'
import { TooManyRequestsError } from '../src/error'
let client: API
beforeAll(() => {
client = new API(process.env.HACKMD_ACCESS_TOKEN!)
return server.listen()
})
afterEach(() => {
server.resetHandlers()
})
afterAll(() => {
server.close()
// Add explicit cleanup to ensure Jest exits properly
return new Promise(resolve => setTimeout(resolve, 100))
})
test('getMe', async () => {
const response = await client.getMe({ unwrapData: false })
expect(response).toHaveProperty('status', 200)
expect(response).toHaveProperty('headers')
})
test('getMe unwrapped', async () => {
const response = await client.getMe()
expect(typeof response).toBe('object')
expect(response).toHaveProperty('id')
expect(response).toHaveProperty('name')
expect(response).toHaveProperty('email')
expect(response).toHaveProperty('userPath')
expect(response).toHaveProperty('photo')
})
test('should throw axios error object if set wrapResponseErrors to false', async () => {
const customCilent = new API(process.env.HACKMD_ACCESS_TOKEN!, undefined, {
wrapResponseErrors: false,
})
server.use(
http.get('https://api.hackmd.io/v1/me', () => {
return new HttpResponse(null, {
status: 429
})
})
)
try {
await customCilent.getMe()
} catch (error: any) {
expect(error).toHaveProperty('response')
expect(error.response).toHaveProperty('status', 429)
}
})
test('should throw HackMD error object', async () => {
// Create a client with retry disabled to avoid conflicts with error handling test
const clientWithoutRetry = new API(process.env.HACKMD_ACCESS_TOKEN!, undefined, {
wrapResponseErrors: true,
retryConfig: undefined // Disable retry logic for this test
})
server.use(
http.get('https://api.hackmd.io/v1/me', () => {
return HttpResponse.json(
{},
{
status: 429,
headers: {
'X-RateLimit-UserLimit': '100',
'x-RateLimit-UserRemaining': '0',
'x-RateLimit-UserReset': String(
new Date().getTime() + 1000 * 60 * 60 * 24,
),
},
}
)
})
)
try {
await clientWithoutRetry.getMe()
// If we get here, the test should fail because an error wasn't thrown
expect('no error thrown').toBe('error should have been thrown')
} catch (error: any) {
expect(error).toBeInstanceOf(TooManyRequestsError)
expect(error).toHaveProperty('code', 429)
expect(error).toHaveProperty('statusText', 'Too Many Requests')
expect(error).toHaveProperty('userLimit', 100)
expect(error).toHaveProperty('userRemaining', 0)
expect(error).toHaveProperty('resetAfter')
}
})