Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add test to nodejs sdk #31

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sdks/nodejs-client/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"@babel/preset-env"
]
}
4 changes: 2 additions & 2 deletions sdks/nodejs-client/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios from 'axios'

const BASE_URL = 'https://api.dify.ai/v1'
export const BASE_URL = 'https://api.dify.ai/v1'

const routes = {
export const routes = {
application: {
method: 'GET',
url: () => `/parameters`
Expand Down
66 changes: 66 additions & 0 deletions sdks/nodejs-client/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { DifyClient, BASE_URL, routes } from ".";

import axios from 'axios'

jest.mock('axios')

describe('Client', () => {
let difyClient
beforeEach(() => {
difyClient = new DifyClient('test')
})

test('should create a client', () => {
expect(difyClient).toBeDefined();
})
// test updateApiKey
test('should update the api key', () => {
difyClient.updateApiKey('test2');
expect(difyClient.apiKey).toBe('test2');
})
});

describe('Send Requests', () => {
let difyClient

beforeEach(() => {
difyClient = new DifyClient('test')
})

afterEach(() => {
jest.resetAllMocks()
})

it('should make a successful request to the application parameter', async () => {
const method = 'GET'
const endpoint = routes.application.url
const expectedResponse = { data: 'response' }
axios.mockResolvedValue(expectedResponse)

await difyClient.sendRequest(method, endpoint)

expect(axios).toHaveBeenCalledWith({
method,
url: `${BASE_URL}${endpoint}`,
data: null,
params: null,
headers: {
Authorization: `Bearer ${difyClient.apiKey}`,
'Content-Type': 'application/json',
},
responseType: 'json',
})

})

it('should handle errors from the API', async () => {
const method = 'GET'
const endpoint = '/test-endpoint'
const errorMessage = 'Request failed with status code 404'
axios.mockRejectedValue(new Error(errorMessage))

await expect(difyClient.sendRequest(method, endpoint)).rejects.toThrow(
errorMessage
)
})
})
14 changes: 14 additions & 0 deletions sdks/nodejs-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,21 @@
"<crazywoola> <<427733928@qq.com>> (https://github.com/crazywoola)"
],
"license": "MIT",
"scripts": {
"test": "jest"
},
"jest": {
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
}
},
"dependencies": {
"axios": "^1.3.5"
},
"devDependencies": {
"@babel/core": "^7.21.8",
"@babel/preset-env": "^7.21.5",
"babel-jest": "^29.5.0",
"jest": "^29.5.0"
}
}