Browse Source

Refactor helper methods for fetch response mocking

Thibaud Colas 1 year ago
parent
commit
4ec22a4b62

+ 1 - 1
client/src/api/client.test.js

@@ -3,7 +3,7 @@ import client from './client';
 describe('client API', () => {
   it('should succeed fetching', (done) => {
     const response = '{"meta":{"total_count":1},"items":[]}';
-    fetch.mockResponseSuccess(response);
+    fetch.mockResponseSuccessJSON(response);
 
     client.get('/example/url').then((result) => {
       expect(result).toMatchSnapshot();

+ 2 - 2
client/src/controllers/OrderableController.test.js

@@ -142,7 +142,7 @@ describe('OrderableController', () => {
         'orderable',
       ]);
 
-      fetch.mockResponseSuccess('');
+      fetch.mockResponseSuccessJSON('');
 
       // emulate a drag end (with change)
 
@@ -250,7 +250,7 @@ describe('OrderableController', () => {
 
       expect(global.fetch).not.toHaveBeenCalled();
 
-      fetch.mockResponseSuccess('');
+      fetch.mockResponseSuccessJSON('');
 
       await Promise.resolve(handle.dispatchEvent(new KeyboardEvent(...ENTER)));
 

+ 9 - 62
client/src/controllers/SwapController.test.js

@@ -183,13 +183,7 @@ describe('SwapController', () => {
         document.addEventListener('w-swap:success', resolve);
       });
 
-      fetch.mockImplementationOnce(() =>
-        Promise.resolve({
-          ok: true,
-          status: 200,
-          text: () => Promise.resolve(results),
-        }),
-      );
+      fetch.mockResponseSuccessText(results);
 
       expect(window.location.search).toEqual('');
       expect(handleError).not.toHaveBeenCalled();
@@ -390,12 +384,7 @@ describe('SwapController', () => {
       const onErrorEvent = jest.fn();
       document.addEventListener('w-swap:error', onErrorEvent);
 
-      fetch.mockImplementationOnce(() =>
-        Promise.resolve({
-          ok: false,
-          status: 500,
-        }),
-      );
+      fetch.mockResponseFailure();
 
       expect(window.location.search).toEqual('');
       expect(handleError).not.toHaveBeenCalled();
@@ -478,13 +467,7 @@ describe('SwapController', () => {
         document.addEventListener('w-swap:success', resolve);
       });
 
-      fetch.mockImplementationOnce(() =>
-        Promise.resolve({
-          ok: true,
-          status: 200,
-          text: () => Promise.resolve(results),
-        }),
-      );
+      fetch.mockResponseSuccessText(results);
 
       expect(window.location.search).toEqual('');
       expect(handleError).not.toHaveBeenCalled();
@@ -543,13 +526,7 @@ describe('SwapController', () => {
 
       document.addEventListener('w-swap:begin', beginEventHandler);
 
-      fetch.mockImplementationOnce(() =>
-        Promise.resolve({
-          ok: true,
-          status: 200,
-          text: () => Promise.resolve(results),
-        }),
-      );
+      fetch.mockResponseSuccessText(results);
 
       expect(window.location.search).toEqual('');
       expect(handleError).not.toHaveBeenCalled();
@@ -609,13 +586,7 @@ describe('SwapController', () => {
       beginEventHandler = jest.fn();
       document.addEventListener('w-swap:begin', beginEventHandler);
 
-      fetch.mockImplementationOnce(() =>
-        Promise.resolve({
-          ok: true,
-          status: 200,
-          text: () => Promise.resolve(results),
-        }),
-      );
+      fetch.mockResponseSuccessText(results);
     });
 
     it('should allow for actions to call the replace method directly, defaulting to the form action url', async () => {
@@ -994,13 +965,7 @@ describe('SwapController', () => {
       const beginEventHandler = jest.fn();
       document.addEventListener('w-swap:begin', beginEventHandler);
 
-      fetch.mockImplementationOnce(() =>
-        Promise.resolve({
-          ok: true,
-          status: 200,
-          text: () => Promise.resolve(results),
-        }),
-      );
+      fetch.mockResponseSuccessText(results);
 
       expect(window.location.search).toEqual('');
       expect(handleError).not.toHaveBeenCalled();
@@ -1066,13 +1031,7 @@ describe('SwapController', () => {
       const beginEventHandler = jest.fn();
       document.addEventListener('w-swap:begin', beginEventHandler);
 
-      fetch.mockImplementationOnce(() =>
-        Promise.resolve({
-          ok: true,
-          status: 200,
-          text: () => Promise.resolve(results),
-        }),
-      );
+      fetch.mockResponseSuccessText(results);
 
       expect(window.location.search).toEqual('');
       expect(handleError).not.toHaveBeenCalled();
@@ -1148,13 +1107,7 @@ describe('SwapController', () => {
       const beginEventHandler = jest.fn();
       document.addEventListener('w-swap:begin', beginEventHandler);
 
-      fetch.mockImplementationOnce(() =>
-        Promise.resolve({
-          ok: true,
-          status: 200,
-          text: () => Promise.resolve(results),
-        }),
-      );
+      fetch.mockResponseSuccessText(results);
 
       expect(window.location.search).toEqual('');
       expect(handleError).not.toHaveBeenCalled();
@@ -1226,13 +1179,7 @@ describe('SwapController', () => {
 
       document.addEventListener('w-swap:begin', beginEventHandler);
 
-      fetch.mockImplementationOnce(() =>
-        Promise.resolve({
-          ok: true,
-          status: 200,
-          text: () => Promise.resolve(results),
-        }),
-      );
+      fetch.mockResponseSuccessText(results);
 
       expect(window.location.search).toEqual('');
       expect(handleError).not.toHaveBeenCalled();

+ 2 - 2
client/src/controllers/UpgradeController.test.js

@@ -44,7 +44,7 @@ describe('UpgradeController', () => {
 
     expect(global.fetch).not.toHaveBeenCalled();
 
-    fetch.mockResponseSuccess(JSON.stringify(data));
+    fetch.mockResponseSuccessJSON(JSON.stringify(data));
 
     // start application
     application = Application.start();
@@ -92,7 +92,7 @@ describe('UpgradeController', () => {
       },
     };
 
-    fetch.mockResponseSuccess(JSON.stringify(data));
+    fetch.mockResponseSuccessJSON(JSON.stringify(data));
 
     expect(global.fetch).not.toHaveBeenCalled();
 

+ 14 - 2
client/tests/mock-fetch.js

@@ -2,8 +2,8 @@
 global.fetch = jest.fn();
 global.Headers = jest.fn();
 
-// Helper to mock a success response.
-fetch.mockResponseSuccess = (body) => {
+// Helper to mock a success JSON response.
+fetch.mockResponseSuccessJSON = (body) => {
   fetch.mockImplementationOnce(() =>
     Promise.resolve({
       json: () => Promise.resolve(JSON.parse(body)),
@@ -14,6 +14,18 @@ fetch.mockResponseSuccess = (body) => {
   );
 };
 
+// Helper to mock a success text response.
+fetch.mockResponseSuccessText = (body) => {
+  fetch.mockImplementationOnce(() =>
+    Promise.resolve({
+      text: () => Promise.resolve(body),
+      ok: true,
+      status: 200,
+      statusText: 'OK',
+    }),
+  );
+};
+
 // Helper to mock a failure response.
 fetch.mockResponseFailure = () => {
   fetch.mockImplementationOnce(() =>