forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/add test to nodejs sdk (langgenius#31)
- Loading branch information
1 parent
f5b2271
commit 3117619
Showing
4 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters