-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc.client.test.ts
172 lines (148 loc) · 4.46 KB
/
rpc.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { describe, it, expect, vi } from 'vitest'
import { createRPCClient } from './rpc.client'
import { JsonRpcResponse, RpcError } from './rpc.common'
const mockFetch = vi.fn()
global.fetch = mockFetch
const methods = {
getData: async (context: { userId: string }, id: string) => {
if (id === 'error') {
// throw new Error('Test error')
throw new RpcError('Test error', 123)
}
return { id, userId: context.userId }
},
getData2: async (context: { userId: string }, id: string, name: string) => {
if (id === 'error') {
// throw new Error('Test error')
throw new RpcError('Test error', 123)
}
return { id, name, userId: context.userId }
}
}
const client = createRPCClient<typeof methods>({
url: '/api/rpc'
})
describe('RPC Client', () => {
beforeEach(() => {
mockFetch.mockReset()
})
it('should call a method and return the result', async () => {
const response1: JsonRpcResponse = {
jsonrpc: '2.0',
result: { id: 'test-id', userId: 'user-123' },
id: '1'
}
const response2: JsonRpcResponse = {
jsonrpc: '2.0',
result: { id: 'test-id2', userId: 'user-123' },
id: '2'
}
mockFetch
.mockResolvedValueOnce({
ok: true,
json: async () => response1
})
.mockResolvedValueOnce({
ok: true,
json: async () => response2
})
const result = await client.getData('test-id')
const result2 = await client.getData('test-id2')
expect(result).toEqual({ id: 'test-id', userId: 'user-123' })
expect(result2).toEqual({ id: 'test-id2', userId: 'user-123' })
expect(mockFetch).toHaveBeenCalledTimes(2)
})
it('should handle a method error', async () => {
const response: JsonRpcResponse = {
jsonrpc: '2.0',
error: { code: 123, message: 'Test error' },
id: '1'
}
mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => response
})
await expect(client.getData('error')).rejects.toThrow(RpcError)
})
it('should handle batch requests', async () => {
const batchResponses: JsonRpcResponse[] = [
{
jsonrpc: '2.0',
result: { id: 'test-id-1', userId: 'user-123' },
id: '1'
},
{
jsonrpc: '2.0',
result: { id: 'test-id-2', userId: 'user-123' },
id: '2'
}
]
mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => batchResponses
})
const [result1, result2] = await Promise.all([
client.getData('test-id-1'),
client.getData('test-id-2')
])
expect(result1).toEqual({ id: 'test-id-1', userId: 'user-123' })
expect(result2).toEqual({ id: 'test-id-2', userId: 'user-123' })
// Verify that only one fetch call was made
expect(mockFetch).toHaveBeenCalledTimes(1)
})
it('should handle batch requests and a single next', async () => {
const batchResponses: JsonRpcResponse[] = [
{
jsonrpc: '2.0',
result: { id: 'test-id-1', userId: 'user-123' },
id: '1'
},
{
jsonrpc: '2.0',
result: { id: 'test-id-2', userId: 'user-123' },
id: '2'
}
]
const singleResponse: JsonRpcResponse = {
jsonrpc: '2.0',
result: { id: 'test-id-3', userId: 'user-123' },
id: '3'
}
mockFetch
.mockResolvedValueOnce({
ok: true,
json: async () => batchResponses
})
.mockResolvedValueOnce({
ok: true,
json: async () => singleResponse
})
const [result1, result2] = await Promise.all([
client.getData('test-id-1'),
client.getData('test-id-2')
])
const result3 = await client.getData('test-id-3')
expect(result1).toEqual({ id: 'test-id-1', userId: 'user-123' })
expect(result2).toEqual({ id: 'test-id-2', userId: 'user-123' })
expect(result3).toEqual({ id: 'test-id-3', userId: 'user-123' })
expect(mockFetch).toHaveBeenCalledTimes(2)
})
it('should create message objects correctly', () => {
const message1 = client.message.getData('test-id-1')
const message2 = client.message.getData('test-id-2')
expect(message1).toEqual({ method: 'getData', params: ['test-id-1'] })
expect(message2).toEqual({ method: 'getData', params: ['test-id-2'] })
})
it('should create message objects correctly with sync option', () => {
const message1 = client.message.getData('test-id-1', { sync: true })
const message2 = client.message.getData('test-id-2')
const message3 = client.message.getData2('test-id-3', 'extra-param', { sync: true })
expect(message1).toEqual({ method: 'getData', params: ['test-id-1'], sync: true })
expect(message2).toEqual({ method: 'getData', params: ['test-id-2'] })
expect(message3).toEqual({
method: 'getData2',
params: ['test-id-3', 'extra-param'],
sync: true
})
})
})