Skip to content

Commit 11b7c1d

Browse files
committed
test: improve
1 parent 8c300a2 commit 11b7c1d

9 files changed

+52
-64
lines changed

tests/api/notifications.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { describe, expect, it } from 'vitest'
2-
import { api, setApiConfig } from '../../src'
2+
import { api, isSuccess, setApiConfig } from '../../src'
33
import { config } from '../config'
44

55
describe('notifications should work', () => {
66
setApiConfig(config)
77

88
it('list should work', async () => {
99
const result = await api.notifications.list()
10-
expect(result.status).toBe(200)
10+
expect(isSuccess(result)).toBe(true)
1111
expect(result.data.data.length).greaterThanOrEqual(1)
1212
})
1313
})

tests/api/personal-update.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from 'vitest'
2-
import { api, setApiConfig } from '../../src'
2+
import { api, isSuccess, setApiConfig } from '../../src'
33
import { config } from '../config'
4+
45
describe('personal update should work', () => {
56
setApiConfig(config)
67

@@ -12,10 +13,15 @@ describe('personal update should work', () => {
1213
limit,
1314
}
1415
)
15-
expect(result.status).toBe(200)
16-
expect(result.data.success).toBe(true)
16+
expect(isSuccess(result)).toBe(true)
1717
expect(
1818
result.data.data.filter((post) => !post.pinned?.personalUpdate).length
1919
).toBe(limit)
2020
})
21+
22+
it('followingUpdates should work', async () => {
23+
const result = await api.personalUpdate.followingUpdates()
24+
expect(isSuccess(result)).toBe(true)
25+
expect(result.data.data.length).greaterThan(0)
26+
})
2127
})

tests/api/posts.test.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { api, setApiConfig } from '../../src'
2+
import { api, isSuccess, setApiConfig } from '../../src'
33
import { PostType } from '../../src/types/options'
44
import { config } from '../config'
55

@@ -10,32 +10,30 @@ describe('posts should work', () => {
1010

1111
it('share should work', async () => {
1212
const post = await api.posts.get(PostType.ORIGINAL, id)
13-
expect(post.status).toBe(200)
13+
expect(isSuccess(post)).toBe(true)
14+
1415
const shareCount = post.data.data.shareCount
1516

1617
const result = await api.posts.share(
1718
PostType.ORIGINAL,
1819
'61b8b9d298f39200100ba010',
1920
'weibo'
2021
)
21-
expect(result.status).toBe(200)
22-
expect(result.data.success).toBe(true)
22+
expect(isSuccess(result)).toBe(true)
2323

2424
const updatedPost = await api.posts.get(PostType.ORIGINAL, id)
25-
expect(updatedPost.status).toBe(200)
25+
expect(isSuccess(updatedPost)).toBe(true)
2626
expect(updatedPost.data.data.shareCount).greaterThan(shareCount)
2727
})
2828

2929
it('like should work', async () => {
3030
const result = await api.posts.like(PostType.ORIGINAL, id)
31-
expect(result.status).toBe(200)
32-
expect(result.data.success).toBe(true)
31+
expect(isSuccess(result)).toBe(true)
3332
})
3433

3534
it('unlike should work', async () => {
3635
const result = await api.posts.unlike(PostType.ORIGINAL, id)
37-
expect(result.status).toBe(200)
38-
expect(result.data.success).toBe(true)
36+
expect(isSuccess(result)).toBe(true)
3937
})
4038
})
4139

@@ -45,8 +43,7 @@ describe('comment should work', () => {
4543
'ORIGINAL_POST',
4644
'61c0d39877abf80010426ba3'
4745
)
48-
expect(result.status).toBe(200)
49-
expect(result.data.success).toBe(true)
46+
expect(isSuccess(result)).toBe(true)
5047
expect(result.data.data.length).greaterThan(0)
5148
})
5249

@@ -55,17 +52,15 @@ describe('comment should work', () => {
5552
'ORIGINAL_POST',
5653
'61c04361d8c958001025e772'
5754
)
58-
expect(result.status).toBe(200)
59-
expect(result.data.success).toBe(true)
55+
expect(isSuccess(result)).toBe(true)
6056
})
6157

6258
it('unlike should work', async () => {
6359
const result = await api.comments.unlike(
6460
'ORIGINAL_POST',
6561
'61c04361d8c958001025e772'
6662
)
67-
expect(result.status).toBe(200)
68-
expect(result.data.success).toBe(true)
63+
expect(isSuccess(result)).toBe(true)
6964
})
7065
})
7166

@@ -75,12 +70,11 @@ describe('new post should work', () => {
7570
it('create should work', async () => {
7671
const result = await api.posts.create(
7772
PostType.ORIGINAL,
78-
`Test For Jike-SDK. 不要评论~请忽略~\n\n${Math.random()}`,
73+
`Test Post. \n\n${Math.random()}`,
7974
/** 悄悄话 */
8075
{ topicId: '5be41ae2a666dd00172d6072' }
8176
)
82-
expect(result.status).toBe(200)
83-
expect(result.data.success).toBe(true)
77+
expect(isSuccess(result)).toBe(true)
8478
expect(result.data.toast).toBe('发送成功')
8579
expect(result.data.data.id).a('string')
8680
id = result.data.data.id
@@ -92,16 +86,14 @@ describe('new post should work', () => {
9286
id,
9387
`测试评论${Math.random()}`
9488
)
95-
expect(result.status).toBe(200)
96-
expect(result.data.success).toBe(true)
89+
expect(isSuccess(result)).toBe(true)
9790
expect(result.data.toast).toBe('发送成功')
9891
expect(result.data.data.id).a('string')
9992
})
10093

10194
it('remove should work', async () => {
10295
const result = await api.posts.remove(PostType.ORIGINAL, id)
103-
expect(result.status).toBe(200)
104-
expect(result.data.success).toBe(true)
96+
expect(isSuccess(result)).toBe(true)
10597
expect(result.data.toast).toBe('删除成功')
10698
})
10799
})

tests/api/recommend-feed.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { describe, expect, it } from 'vitest'
2-
import { api, setApiConfig } from '../../src'
2+
import { api, isSuccess, setApiConfig } from '../../src'
33
import { config } from '../config'
44

55
setApiConfig(config)
66

77
describe('comment should work', () => {
88
it('list should work', async () => {
99
const result = await api.recommendFeed.list()
10-
expect(result.status).toBe(200)
11-
expect(result.data.success).toBe(true)
10+
expect(isSuccess(result)).toBe(true)
1211
expect(result.data.data.length).greaterThan(0)
1312
})
1413
})

tests/api/upload.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sharp from 'sharp'
22
import Md5 from 'md5'
33
import { describe, expect, it } from 'vitest'
44
import { faker } from '@faker-js/faker'
5-
import { api, setApiConfig } from '../../src'
5+
import { api, isSuccess, setApiConfig } from '../../src'
66
import { config } from '../config'
77

88
describe('upload should work', async () => {
@@ -11,8 +11,8 @@ describe('upload should work', async () => {
1111
let token: string
1212
const image = await sharp({
1313
create: {
14-
width: Math.floor(Math.random() * 100),
15-
height: Math.floor(Math.random() * 100),
14+
width: Math.floor(Math.random() * 100) + 1,
15+
height: Math.floor(Math.random() * 100) + 1,
1616
channels: 3,
1717
background: faker.internet.color(),
1818
},
@@ -23,8 +23,7 @@ describe('upload should work', async () => {
2323

2424
it('token should work', async () => {
2525
const result = await api.upload.token(md5)
26-
expect(result.status).toBe(200)
27-
expect(result.data.success).toBe(true)
26+
expect(isSuccess(result)).toBe(true)
2827
expect(result.data.uptoken).a('string')
2928
token = result.data.uptoken
3029
})

tests/api/user-relation.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { api, setApiConfig } from '../../src'
2+
import { api, isSuccess, setApiConfig } from '../../src'
33
import { config } from '../config'
44

55
describe('user relation should work', () => {
@@ -10,15 +10,13 @@ describe('user relation should work', () => {
1010

1111
it('getFollowerList should work', async () => {
1212
const result = await api.userRelation.getFollowerList(username, { limit })
13-
expect(result.status).toBe(200)
14-
expect(result.data.success).toBe(true)
13+
expect(isSuccess(result)).toBe(true)
1514
expect(result.data.data.length).toBe(limit)
1615
})
1716

1817
it('getFollowingList should work', async () => {
1918
const result = await api.userRelation.getFollowingList(username, { limit })
20-
expect(result.status).toBe(200)
21-
expect(result.data.success).toBe(true)
19+
expect(isSuccess(result)).toBe(true)
2220
expect(result.data.data.length).toBe(limit)
2321
})
2422
})

tests/api/users.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { api, setApiConfig } from '../../src'
2+
import { api, isSuccess, setApiConfig } from '../../src'
33
import { config } from '../config'
44

55
describe('users should work', () => {
@@ -24,8 +24,7 @@ describe('users should work', () => {
2424
const token = process.env['REFRESH_TOKEN']
2525
if (!token) return
2626
const result = await api.users.refreshToken(token)
27-
expect(result.status).toBe(200)
28-
expect(result.data.success).toBe(true)
27+
expect(isSuccess(result)).toBe(true)
2928
expect(result.data[`x-${config.endpointId}-access-token`]).toBeTruthy()
3029
expect(result.data[`x-${config.endpointId}-refresh-token`]).toBeTruthy()
3130
})
@@ -37,16 +36,14 @@ describe('users should work', () => {
3736
it('sendSms should work', async () => {
3837
if (!mobile || smsCode || password) return
3938
const result = await api.users.getSmsCode('+86', mobile)
40-
expect(result.status).toBe(200)
41-
expect(result.data.success).toBe(true)
39+
expect(isSuccess(result)).toBe(true)
4240
expect(result.data.data.action).toBe('LOGIN')
4341
})
4442

4543
it('loginWithSmsCode should work', async () => {
4644
if (!mobile || !smsCode) return
4745
const result = await api.users.loginWithSmsCode('+86', mobile, smsCode)
48-
expect(result.status).toBe(200)
49-
expect(result.data.success).toBe(true)
46+
expect(isSuccess(result)).toBe(true)
5047
if (result.data.success) expect(result.data.user).toBeTruthy()
5148
})
5249

@@ -57,8 +54,7 @@ describe('users should work', () => {
5754
mobile,
5855
password
5956
)
60-
expect(result.status).toBe(200)
61-
expect(result.data.success).toBe(true)
57+
expect(isSuccess(result)).toBe(true)
6258
if (result.data.success) expect(result.data.user).toBeTruthy()
6359
})
6460

tests/request.test.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@ import type { ApiConfig } from '../src'
1313
if (!globalThis.AbortController) globalThis.AbortController = AbortController
1414

1515
describe('request', () => {
16-
const config = {
16+
const config: ApiConfig = {
1717
endpointId: 'keji',
1818
endpointUrl: 'https://keji.org/api/',
1919
bundleId: 'org.keji.keji',
20+
appVersion: '1.2.3',
2021
buildNo: '10086',
2122
userAgent: 'keji-useragent',
2223
}
2324

2425
it('access token should not be empty', () => {
2526
const token = 'TEST_TOKEN'
26-
setApiConfig({
27-
accessToken: token,
28-
...config,
29-
})
27+
setApiConfig({ ...config, accessToken: token })
3028
expect(getAccessToken()).toBe(token)
3129
})
3230

@@ -64,15 +62,14 @@ describe('request', () => {
6462

6563
it('headers should be correct', () => {
6664
expect(req.headers.get('User-Agent')).toBe(apiConfig.userAgent)
67-
expect(req.headers.get(`x-${config.endpointId}-access-token`)).toBe(
68-
apiConfig.accessToken
69-
)
70-
expect(
71-
req.headers.get(`x-${config.endpointId}-device-properties`)
72-
).includes(apiConfig.idfv)
73-
expect(req.headers.get(`x-${config.endpointId}-device-id`)).includes(
74-
apiConfig.deviceId
75-
)
65+
expect(req.headers.get('App-Version')).toBe(apiConfig.appVersion)
66+
67+
const accessToken = `x-${config.endpointId}-access-token`
68+
const deviceProperties = `x-${config.endpointId}-device-properties`
69+
const deviceId = `x-${config.endpointId}-device-id`
70+
expect(req.headers.get(accessToken)).toBe(apiConfig.accessToken)
71+
expect(req.headers.get(deviceProperties)).includes(apiConfig.idfv)
72+
expect(req.headers.get(deviceId)).includes(apiConfig.deviceId)
7673
})
7774
})
7875

tests/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"extends": "../tsconfig.json"
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {}
34
}

0 commit comments

Comments
 (0)