Skip to content

Commit b1b8730

Browse files
refactor: Use MSW for the tests
1 parent 6434b1e commit b1b8730

24 files changed

+1581
-692
lines changed

package-lock.json

Lines changed: 709 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@types/uuid": "9.0.8",
3939
"husky": "8.0.3",
4040
"lint-staged": "13.3.0",
41+
"msw": "^2.11.3",
4142
"npm-run-all2": "5.0.2",
4243
"rimraf": "3.0.2",
4344
"ts-node": "10.9.2",

src/authentication.test.ts

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { vi } from 'vitest'
1+
import { HttpResponse, http } from 'msw'
22
import {
33
getAuthorizationUrl,
44
getAuthStateParameter,
55
getAuthToken,
66
revokeAuthToken,
77
TwistScope,
88
} from './authentication'
9-
import { setupRestClientMock } from './testUtils/mocks'
9+
import { server } from './testUtils/msw-setup'
1010

1111
describe('authentication', () => {
1212
describe('getAuthStateParameter', () => {
@@ -70,47 +70,62 @@ describe('authentication', () => {
7070
})
7171

7272
describe('getAuthToken', () => {
73-
let mockRequest: ReturnType<typeof setupRestClientMock>
74-
75-
beforeEach(() => {
73+
it('should exchange auth code for access token', async () => {
7674
const mockResponse = {
77-
accessToken: 'access-token-123',
78-
tokenType: 'Bearer',
79-
refreshToken: 'refresh-token-456',
80-
expiresIn: 3600,
75+
access_token: 'access-token-123',
76+
token_type: 'Bearer',
77+
refresh_token: 'refresh-token-456',
78+
expires_in: 3600,
8179
}
82-
mockRequest = setupRestClientMock(mockResponse, 200)
83-
})
8480

85-
afterEach(() => {
86-
vi.clearAllMocks()
87-
})
81+
server.use(
82+
http.post('https://twist.com/oauth/token', async ({ request }) => {
83+
const body = await request.json()
84+
expect(body).toEqual({
85+
client_id: 'client-id',
86+
client_secret: 'client-secret',
87+
code: 'auth-code',
88+
grant_type: 'authorization_code',
89+
})
90+
return HttpResponse.json(mockResponse)
91+
}),
92+
)
8893

89-
it('should exchange auth code for access token', async () => {
9094
const args = {
9195
clientId: 'client-id',
9296
clientSecret: 'client-secret',
9397
code: 'auth-code',
9498
}
9599

96100
const result = await getAuthToken(args)
97-
98-
expect(mockRequest).toHaveBeenCalledWith(
99-
'POST',
100-
'https://twist.com/oauth/token',
101-
'',
102-
undefined,
103-
{
104-
clientId: 'client-id',
105-
clientSecret: 'client-secret',
106-
code: 'auth-code',
107-
grantType: 'authorization_code',
108-
},
109-
)
110101
expect(result.accessToken).toBe('access-token-123')
102+
expect(result.tokenType).toBe('Bearer')
103+
expect(result.refreshToken).toBe('refresh-token-456')
104+
expect(result.expiresIn).toBe(3600)
111105
})
112106

113107
it('should include redirect URI in token request if provided', async () => {
108+
const mockResponse = {
109+
access_token: 'access-token-123',
110+
token_type: 'Bearer',
111+
refresh_token: 'refresh-token-456',
112+
expires_in: 3600,
113+
}
114+
115+
server.use(
116+
http.post('https://twist.com/oauth/token', async ({ request }) => {
117+
const body = await request.json()
118+
expect(body).toEqual({
119+
client_id: 'client-id',
120+
client_secret: 'client-secret',
121+
code: 'auth-code',
122+
grant_type: 'authorization_code',
123+
redirect_uri: 'https://myapp.com/callback',
124+
})
125+
return HttpResponse.json(mockResponse)
126+
}),
127+
)
128+
114129
const args = {
115130
clientId: 'client-id',
116131
clientSecret: 'client-secret',
@@ -119,50 +134,30 @@ describe('authentication', () => {
119134
}
120135

121136
await getAuthToken(args)
122-
123-
expect(mockRequest).toHaveBeenCalledWith(
124-
'POST',
125-
'https://twist.com/oauth/token',
126-
'',
127-
undefined,
128-
expect.objectContaining({
129-
redirectUri: 'https://myapp.com/callback',
130-
}),
131-
)
132137
})
133138
})
134139

135140
describe('revokeAuthToken', () => {
136-
let mockRequest: ReturnType<typeof setupRestClientMock>
137-
138-
beforeEach(() => {
139-
mockRequest = setupRestClientMock({}, 200)
140-
})
141-
142-
afterEach(() => {
143-
vi.clearAllMocks()
144-
})
145-
146141
it('should revoke access token', async () => {
142+
server.use(
143+
http.post('https://twist.com/oauth/revoke', async ({ request }) => {
144+
const body = await request.json()
145+
expect(body).toEqual({
146+
client_id: 'client-id',
147+
client_secret: 'client-secret',
148+
token: 'access-token',
149+
})
150+
return HttpResponse.json({})
151+
}),
152+
)
153+
147154
const args = {
148155
clientId: 'client-id',
149156
clientSecret: 'client-secret',
150157
accessToken: 'access-token',
151158
}
152159

153160
const result = await revokeAuthToken(args)
154-
155-
expect(mockRequest).toHaveBeenCalledWith(
156-
'POST',
157-
'https://twist.com/oauth/revoke',
158-
'',
159-
undefined,
160-
{
161-
clientId: 'client-id',
162-
clientSecret: 'client-secret',
163-
token: 'access-token',
164-
},
165-
)
166161
expect(result).toBe(true)
167162
})
168163
})
Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,103 @@
1-
import { vi } from 'vitest'
2-
import { setupRestClientMock } from '../testUtils/mocks'
1+
import { HttpResponse, http } from 'msw'
2+
import { apiUrl } from '../testUtils/msw-handlers'
3+
import { server } from '../testUtils/msw-setup'
34
import { TEST_API_TOKEN } from '../testUtils/test-defaults'
45
import { ChannelsClient } from './channels-client'
56

67
describe('ChannelsClient', () => {
78
let client: ChannelsClient
8-
let mockRequest: ReturnType<typeof setupRestClientMock>
99

1010
beforeEach(() => {
1111
client = new ChannelsClient(TEST_API_TOKEN)
12-
mockRequest = setupRestClientMock(null)
13-
})
14-
15-
afterEach(() => {
16-
vi.clearAllMocks()
1712
})
1813

1914
describe('favoriteChannel', () => {
2015
it('should favorite a channel', async () => {
21-
await client.favoriteChannel(123)
22-
23-
expect(mockRequest).toHaveBeenCalledWith(
24-
'POST',
25-
'https://api.twist.com/api/v3/',
26-
'channels/favorite',
27-
TEST_API_TOKEN,
28-
{ id: 123 },
16+
server.use(
17+
http.post(apiUrl('api/v3/channels/favorite'), async ({ request }) => {
18+
const body = await request.json()
19+
expect(body).toEqual({ id: 123 })
20+
expect(request.headers.get('Authorization')).toBe(`Bearer ${TEST_API_TOKEN}`)
21+
return HttpResponse.json(null)
22+
}),
2923
)
24+
25+
await client.favoriteChannel(123)
3026
})
3127
})
3228

3329
describe('unfavoriteChannel', () => {
3430
it('should unfavorite a channel', async () => {
35-
await client.unfavoriteChannel(123)
36-
37-
expect(mockRequest).toHaveBeenCalledWith(
38-
'POST',
39-
'https://api.twist.com/api/v3/',
40-
'channels/unfavorite',
41-
TEST_API_TOKEN,
42-
{ id: 123 },
31+
server.use(
32+
http.post(apiUrl('api/v3/channels/unfavorite'), async ({ request }) => {
33+
const body = await request.json()
34+
expect(body).toEqual({ id: 123 })
35+
expect(request.headers.get('Authorization')).toBe(`Bearer ${TEST_API_TOKEN}`)
36+
return HttpResponse.json(null)
37+
}),
4338
)
39+
40+
await client.unfavoriteChannel(123)
4441
})
4542
})
4643

4744
describe('addUser', () => {
4845
it('should add a user to channel', async () => {
49-
await client.addUser(123, 456)
50-
51-
expect(mockRequest).toHaveBeenCalledWith(
52-
'POST',
53-
'https://api.twist.com/api/v3/',
54-
'channels/add_user',
55-
TEST_API_TOKEN,
56-
{ id: 123, userId: 456 },
46+
server.use(
47+
http.post(apiUrl('api/v3/channels/add_user'), async ({ request }) => {
48+
const body = await request.json()
49+
expect(body).toEqual({ id: 123, user_id: 456 })
50+
expect(request.headers.get('Authorization')).toBe(`Bearer ${TEST_API_TOKEN}`)
51+
return HttpResponse.json(null)
52+
}),
5753
)
54+
55+
await client.addUser(123, 456)
5856
})
5957
})
6058

6159
describe('addUsers', () => {
6260
it('should add multiple users to channel', async () => {
63-
await client.addUsers(123, [456, 789, 101])
64-
65-
expect(mockRequest).toHaveBeenCalledWith(
66-
'POST',
67-
'https://api.twist.com/api/v3/',
68-
'channels/add_users',
69-
TEST_API_TOKEN,
70-
{ id: 123, userIds: [456, 789, 101] },
61+
server.use(
62+
http.post(apiUrl('api/v3/channels/add_users'), async ({ request }) => {
63+
const body = await request.json()
64+
expect(body).toEqual({ id: 123, user_ids: [456, 789, 101] })
65+
expect(request.headers.get('Authorization')).toBe(`Bearer ${TEST_API_TOKEN}`)
66+
return HttpResponse.json(null)
67+
}),
7168
)
69+
70+
await client.addUsers(123, [456, 789, 101])
7271
})
7372
})
7473

7574
describe('removeUser', () => {
7675
it('should remove a user from channel', async () => {
77-
await client.removeUser(123, 456)
78-
79-
expect(mockRequest).toHaveBeenCalledWith(
80-
'POST',
81-
'https://api.twist.com/api/v3/',
82-
'channels/remove_user',
83-
TEST_API_TOKEN,
84-
{ id: 123, userId: 456 },
76+
server.use(
77+
http.post(apiUrl('api/v3/channels/remove_user'), async ({ request }) => {
78+
const body = await request.json()
79+
expect(body).toEqual({ id: 123, user_id: 456 })
80+
expect(request.headers.get('Authorization')).toBe(`Bearer ${TEST_API_TOKEN}`)
81+
return HttpResponse.json(null)
82+
}),
8583
)
84+
85+
await client.removeUser(123, 456)
8686
})
8787
})
8888

8989
describe('removeUsers', () => {
9090
it('should remove multiple users from channel', async () => {
91-
await client.removeUsers(123, [456, 789, 101])
92-
93-
expect(mockRequest).toHaveBeenCalledWith(
94-
'POST',
95-
'https://api.twist.com/api/v3/',
96-
'channels/remove_users',
97-
TEST_API_TOKEN,
98-
{ id: 123, userIds: [456, 789, 101] },
91+
server.use(
92+
http.post(apiUrl('api/v3/channels/remove_users'), async ({ request }) => {
93+
const body = await request.json()
94+
expect(body).toEqual({ id: 123, user_ids: [456, 789, 101] })
95+
expect(request.headers.get('Authorization')).toBe(`Bearer ${TEST_API_TOKEN}`)
96+
return HttpResponse.json(null)
97+
}),
9998
)
99+
100+
await client.removeUsers(123, [456, 789, 101])
100101
})
101102
})
102103
})

src/clients/channels-client.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,16 @@ export class ChannelsClient {
214214
* @param userId - The user ID to remove.
215215
*/
216216
async removeUser(id: number, userId: number): Promise<void> {
217-
await request('POST', this.getBaseUri(), `${ENDPOINT_CHANNELS}/remove_user`, this.apiToken, {
218-
id,
219-
userId,
220-
})
217+
await request(
218+
'POST',
219+
this.getBaseUri(),
220+
`${ENDPOINT_CHANNELS}/remove_user`,
221+
this.apiToken,
222+
{
223+
id,
224+
userId,
225+
},
226+
)
221227
}
222228

223229
/**
@@ -227,9 +233,15 @@ export class ChannelsClient {
227233
* @param userIds - Array of user IDs to remove.
228234
*/
229235
async removeUsers(id: number, userIds: number[]): Promise<void> {
230-
await request('POST', this.getBaseUri(), `${ENDPOINT_CHANNELS}/remove_users`, this.apiToken, {
231-
id,
232-
userIds,
233-
})
236+
await request(
237+
'POST',
238+
this.getBaseUri(),
239+
`${ENDPOINT_CHANNELS}/remove_users`,
240+
this.apiToken,
241+
{
242+
id,
243+
userIds,
244+
},
245+
)
234246
}
235247
}

0 commit comments

Comments
 (0)