Skip to content

Commit

Permalink
test: add test for request.spec.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidvdn committed Aug 10, 2024
1 parent e52111e commit fc59643
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ResponseData } from "./interface";
import { url } from './const';


const sendRequest = async () => {
export const sendRequest = async () => {
const builder = new RequestBuilder();
const request = builder
.setParams({ name: 'John Doe' })
Expand All @@ -16,7 +16,7 @@ const sendRequest = async () => {

const response = await axios.post<ResponseData>(url, request.data, request);
console.log(response.data)

return response.data
}

sendRequest();
22 changes: 22 additions & 0 deletions app/builder/request.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import axios from 'axios';
import { makePostRequest } from './request';
import { PostData } from './interface';

describe('Builder', () => {
const data: PostData = {
title: 'test title',
body: 'test body',
}

it('should send request with axios', async () => {
jest.spyOn(axios, 'post').mockReturnValueOnce(Promise.resolve({ data : { id: 4 } }));
const result = await makePostRequest(data);
expect(result).toEqual({ id: 4 });
})

it('should fail request', async () => {
jest.spyOn(axios, 'post').mockReturnValueOnce(Promise.reject({ error: 'error' }));
const result = await makePostRequest(data);
expect(result).toEqual({ error: 'error' });
})
})
2 changes: 1 addition & 1 deletion app/builder/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export async function makePostRequest(data: PostData) {
console.log('Response Data:', response.data);
return response.data
} catch (error) {
console.error('Error:', error);
return error;
}
}

0 comments on commit fc59643

Please sign in to comment.