11import axios from "axios" ;
22import MockAdapter from "axios-mock-adapter" ;
3-
4- // Import the function to be tested
53import apicaller from "../src/hal-api-caller" ;
64import optionsResponse from "./mocks/options-response.json" ;
75import getResponse from "./mocks/get-response.json" ;
@@ -17,39 +15,134 @@ describe("Apicaller functions test", () => {
1715 mockAxios . restore ( ) ;
1816 } ) ;
1917
20- it ( "should make a successful OPTIONS request and return HalResponse" , async ( ) => {
18+ it ( "Should make a successful OPTIONS request and return HalResponse" , async ( ) => {
2119 const url = "https://example.com" ;
2220 const headers = { CustomHeader : "CustomValue" } ;
2321 const responseData = optionsResponse ;
2422 const responseHeaders = { "Content-Type" : "application/json" } ;
2523
26- // Mocking the Axios request and response
2724 mockAxios . onOptions ( url ) . reply ( 200 , responseData , responseHeaders ) ;
2825
29- // Calling the function
3026 const result = await apicaller . options ( { url, headers } ) ;
3127
32- // Assertions
3328 expect ( result ) . toBeDefined ( ) ;
3429 expect ( result . halResource ) . toBeDefined ( ) ;
3530 expect ( result . halResource . getTitle ( ) ) . toEqual ( "Options available on Quote ID" ) ;
3631 } ) ;
3732
38- it ( "should make a successful GET request and return HalResponse" , async ( ) => {
33+ it ( "Should make a successful GET request and return HalResponse" , async ( ) => {
3934 const url = "https://example.com" ;
4035 const headers = { CustomHeader : "CustomValue" } ;
4136 const responseData = getResponse ;
4237 const responseHeaders = { "Content-Type" : "application/json" } ;
4338
44- // Mocking the Axios request and response
4539 mockAxios . onGet ( url ) . reply ( 200 , responseData , responseHeaders ) ;
4640
47- // Calling the function
4841 const result = await apicaller . get ( { url, headers } ) ;
4942
50- // Assertions
5143 expect ( result ) . toBeDefined ( ) ;
5244 expect ( result . halResource ) . toBeDefined ( ) ;
5345 expect ( result . halResource . getItems ( ) . length ) . toBe ( 10 ) ;
5446 } ) ;
47+
48+ it ( "Should make a successful PATCH request" , async ( ) => {
49+ const url = "https://example.com" ;
50+ const body = { data : "updatedData" } ;
51+ const headers = { CustomHeader : "CustomValue" } ;
52+ const responseData = { success : true } ;
53+ const responseHeaders = { "Content-Type" : "application/json" } ;
54+
55+ mockAxios . onPatch ( url ) . reply ( 200 , responseData , responseHeaders ) ;
56+
57+ const result = await apicaller . patch ( { url, body, headers } ) ;
58+
59+ expect ( result ) . toBeDefined ( ) ;
60+ expect ( result . status ) . toBe ( 200 ) ;
61+ expect ( result . body ) . toEqual ( responseData ) ;
62+ } ) ;
63+
64+ it ( "Should handle PATCH request failure" , async ( ) => {
65+ const url = "https://example.com" ;
66+ const body = { data : "updatedData" } ;
67+ const headers = { CustomHeader : "CustomValue" } ;
68+
69+ mockAxios . onPatch ( url ) . reply ( 500 ) ;
70+
71+ await expect ( apicaller . patch ( { url, body, headers } ) ) . rejects . toThrow ( "Request failed with status code 500" ) ;
72+ } ) ;
73+
74+ it ( "Should make a successful POST request" , async ( ) => {
75+ const url = "https://example.com" ;
76+ const body = { name : "New Resource" } ;
77+ const headers = { CustomHeader : "CustomValue" } ;
78+ const responseData = { id : 1 } ;
79+ const responseHeaders = { "Content-Type" : "application/json" } ;
80+
81+ mockAxios . onPost ( url ) . reply ( 201 , responseData , responseHeaders ) ;
82+
83+ const result = await apicaller . post ( { url, body, headers } ) ;
84+
85+ expect ( result ) . toBeDefined ( ) ;
86+ expect ( result . status ) . toBe ( 201 ) ;
87+ expect ( result . body ) . toEqual ( responseData ) ;
88+ } ) ;
89+
90+ it ( "Should handle POST request failure" , async ( ) => {
91+ const url = "https://example.com" ;
92+ const body = { name : "New Resource" } ;
93+ const headers = { CustomHeader : "CustomValue" } ;
94+
95+ mockAxios . onPost ( url ) . reply ( 404 ) ;
96+
97+ await expect ( apicaller . post ( { url, body, headers } ) ) . rejects . toThrow ( "Request failed with status code 404" ) ;
98+ } ) ;
99+
100+ it ( "Should make a successful PUT request" , async ( ) => {
101+ const url = "https://example.com" ;
102+ const body = { name : "Updated Resource" } ;
103+ const headers = { CustomHeader : "CustomValue" } ;
104+ const responseData = { success : true } ;
105+ const responseHeaders = { "Content-Type" : "application/json" } ;
106+
107+ mockAxios . onPut ( url ) . reply ( 200 , responseData , responseHeaders ) ;
108+
109+ const result = await apicaller . put ( { url, body, headers } ) ;
110+
111+ expect ( result ) . toBeDefined ( ) ;
112+ expect ( result . status ) . toBe ( 200 ) ;
113+ expect ( result . body ) . toEqual ( responseData ) ;
114+ } ) ;
115+
116+ it ( "Should make a successful DELETE request" , async ( ) => {
117+ const url = "https://example.com" ;
118+ const headers = { CustomHeader : "CustomValue" } ;
119+ const responseData = { success : true } ;
120+ const responseHeaders = { "Content-Type" : "application/json" } ;
121+
122+ mockAxios . onDelete ( url ) . reply ( 204 , responseData , responseHeaders ) ;
123+
124+ const result = await apicaller . del ( { url, headers } ) ;
125+
126+ expect ( result ) . toBeDefined ( ) ;
127+ expect ( result . status ) . toBe ( 204 ) ;
128+ expect ( result . body ) . toEqual ( responseData ) ;
129+ } ) ;
130+
131+ it ( "Should handle DELETE request failure" , async ( ) => {
132+ const url = "https://example.com" ;
133+ const headers = { CustomHeader : "CustomValue" } ;
134+
135+ mockAxios . onDelete ( url ) . reply ( 403 ) ;
136+
137+ await expect ( apicaller . del ( { url, headers } ) ) . rejects . toThrow ( "Request failed with status code 403" ) ;
138+ } ) ;
139+
140+ it ( "Should handle OPTIONS request failure" , async ( ) => {
141+ const url = "https://example.com" ;
142+ const headers = { CustomHeader : "CustomValue" } ;
143+
144+ mockAxios . onOptions ( url ) . reply ( 500 ) ;
145+
146+ await expect ( apicaller . options ( { url, headers } ) ) . rejects . toThrow ( "Request failed with status code 500" ) ;
147+ } ) ;
55148} ) ;
0 commit comments