From 843a0a739e01624a79162feefab6bb26b18384c8 Mon Sep 17 00:00:00 2001
From: crazywoola
Date: Fri, 12 May 2023 14:51:35 +0800
Subject: [PATCH] feat: add tests
---
sdks/nodejs-client/index.js | 2 +-
sdks/nodejs-client/tests/client.test.js | 68 +++++++++++++++++++++++--
2 files changed, 64 insertions(+), 6 deletions(-)
diff --git a/sdks/nodejs-client/index.js b/sdks/nodejs-client/index.js
index ae19d48b53..22292bd553 100644
--- a/sdks/nodejs-client/index.js
+++ b/sdks/nodejs-client/index.js
@@ -1,6 +1,6 @@
import axios from 'axios'
-const BASE_URL = 'https://api.dify.ai/v1'
+export const BASE_URL = 'https://api.dify.ai/v1'
const routes = {
application: {
diff --git a/sdks/nodejs-client/tests/client.test.js b/sdks/nodejs-client/tests/client.test.js
index 549316b4bc..f53637419c 100644
--- a/sdks/nodejs-client/tests/client.test.js
+++ b/sdks/nodejs-client/tests/client.test.js
@@ -1,8 +1,66 @@
-import { LangGeniusClient } from "..";
+import { LangGeniusClient, BASE_URL } from "..";
+
+import axios from 'axios'
+
+jest.mock('axios')
describe('Client', () => {
- test('should create a client', () => {
- const client = new LangGeniusClient('test');
- expect(client).toBeDefined();
+ let langGeniusClient
+ beforeEach(() => {
+ langGeniusClient = new LangGeniusClient('test')
+ })
+
+ test('should create a client', () => {
+ expect(langGeniusClient).toBeDefined();
+ })
+ // test updateApiKey
+ test('should update the api key', () => {
+ langGeniusClient.updateApiKey('test2');
+ expect(langGeniusClient.apiKey).toBe('test2');
+ })
+});
+
+describe('sendRequest', () => {
+ let langGeniusClient
+
+ beforeEach(() => {
+ langGeniusClient = new LangGeniusClient('test')
+ })
+
+ afterEach(() => {
+ jest.resetAllMocks()
+ })
+
+ it('should make a successful request to the API', async () => {
+ const method = 'GET'
+ const endpoint = '/test-endpoint'
+ const expectedResponse = { data: 'response' }
+ axios.mockResolvedValue(expectedResponse)
+
+ await langGeniusClient.sendRequest(method, endpoint)
+
+ expect(axios).toHaveBeenCalledWith({
+ method,
+ url: `${BASE_URL}${endpoint}`,
+ data: null,
+ params: null,
+ headers: {
+ Authorization: `Bearer ${langGeniusClient.apiKey}`,
+ 'Content-Type': 'application/json',
+ },
+ responseType: 'json',
})
-});
\ No newline at end of file
+
+ })
+
+ 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(langGeniusClient.sendRequest(method, endpoint)).rejects.toThrow(
+ errorMessage
+ )
+ })
+})
\ No newline at end of file