From 3117619ef3b99e94200a381082596ddfd4c49a12 Mon Sep 17 00:00:00 2001 From: crazywoola <100913391+crazywoola@users.noreply.github.com> Date: Tue, 16 May 2023 09:22:47 +0800 Subject: [PATCH] Feature/add test to nodejs sdk (#31) --- sdks/nodejs-client/babel.config.json | 5 +++ sdks/nodejs-client/index.js | 4 +- sdks/nodejs-client/index.test.js | 66 ++++++++++++++++++++++++++++ sdks/nodejs-client/package.json | 14 ++++++ 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 sdks/nodejs-client/babel.config.json create mode 100644 sdks/nodejs-client/index.test.js diff --git a/sdks/nodejs-client/babel.config.json b/sdks/nodejs-client/babel.config.json new file mode 100644 index 0000000000..0639bf7643 --- /dev/null +++ b/sdks/nodejs-client/babel.config.json @@ -0,0 +1,5 @@ +{ + "presets": [ + "@babel/preset-env" + ] +} \ No newline at end of file diff --git a/sdks/nodejs-client/index.js b/sdks/nodejs-client/index.js index 6faf5f5b06..48b6ea753f 100644 --- a/sdks/nodejs-client/index.js +++ b/sdks/nodejs-client/index.js @@ -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` diff --git a/sdks/nodejs-client/index.test.js b/sdks/nodejs-client/index.test.js new file mode 100644 index 0000000000..e08b8e82af --- /dev/null +++ b/sdks/nodejs-client/index.test.js @@ -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 + ) + }) +}) \ No newline at end of file diff --git a/sdks/nodejs-client/package.json b/sdks/nodejs-client/package.json index 9c1a5ed5cf..c658b73d98 100644 --- a/sdks/nodejs-client/package.json +++ b/sdks/nodejs-client/package.json @@ -14,7 +14,21 @@ " <<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" } } \ No newline at end of file