Skip to content

Commit 3117619

Browse files
authored
Feature/add test to nodejs sdk (#31)
1 parent f5b2271 commit 3117619

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

sdks/nodejs-client/babel.config.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env"
4+
]
5+
}

sdks/nodejs-client/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import axios from 'axios'
22

3-
const BASE_URL = 'https://api.dify.ai/v1'
3+
export const BASE_URL = 'https://api.dify.ai/v1'
44

5-
const routes = {
5+
export const routes = {
66
application: {
77
method: 'GET',
88
url: () => `/parameters`

sdks/nodejs-client/index.test.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { DifyClient, BASE_URL, routes } from ".";
2+
3+
import axios from 'axios'
4+
5+
jest.mock('axios')
6+
7+
describe('Client', () => {
8+
let difyClient
9+
beforeEach(() => {
10+
difyClient = new DifyClient('test')
11+
})
12+
13+
test('should create a client', () => {
14+
expect(difyClient).toBeDefined();
15+
})
16+
// test updateApiKey
17+
test('should update the api key', () => {
18+
difyClient.updateApiKey('test2');
19+
expect(difyClient.apiKey).toBe('test2');
20+
})
21+
});
22+
23+
describe('Send Requests', () => {
24+
let difyClient
25+
26+
beforeEach(() => {
27+
difyClient = new DifyClient('test')
28+
})
29+
30+
afterEach(() => {
31+
jest.resetAllMocks()
32+
})
33+
34+
it('should make a successful request to the application parameter', async () => {
35+
const method = 'GET'
36+
const endpoint = routes.application.url
37+
const expectedResponse = { data: 'response' }
38+
axios.mockResolvedValue(expectedResponse)
39+
40+
await difyClient.sendRequest(method, endpoint)
41+
42+
expect(axios).toHaveBeenCalledWith({
43+
method,
44+
url: `${BASE_URL}${endpoint}`,
45+
data: null,
46+
params: null,
47+
headers: {
48+
Authorization: `Bearer ${difyClient.apiKey}`,
49+
'Content-Type': 'application/json',
50+
},
51+
responseType: 'json',
52+
})
53+
54+
})
55+
56+
it('should handle errors from the API', async () => {
57+
const method = 'GET'
58+
const endpoint = '/test-endpoint'
59+
const errorMessage = 'Request failed with status code 404'
60+
axios.mockRejectedValue(new Error(errorMessage))
61+
62+
await expect(difyClient.sendRequest(method, endpoint)).rejects.toThrow(
63+
errorMessage
64+
)
65+
})
66+
})

sdks/nodejs-client/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,21 @@
1414
"<crazywoola> <<427733928@qq.com>> (https://github.com/crazywoola)"
1515
],
1616
"license": "MIT",
17+
"scripts": {
18+
"test": "jest"
19+
},
20+
"jest": {
21+
"transform": {
22+
"^.+\\.[t|j]sx?$": "babel-jest"
23+
}
24+
},
1725
"dependencies": {
1826
"axios": "^1.3.5"
27+
},
28+
"devDependencies": {
29+
"@babel/core": "^7.21.8",
30+
"@babel/preset-env": "^7.21.5",
31+
"babel-jest": "^29.5.0",
32+
"jest": "^29.5.0"
1933
}
2034
}

0 commit comments

Comments
 (0)