From 16d7b11b8d03037fbc2db3c4f6ffcc7d62848242 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sat, 26 Jul 2025 18:44:59 +1200 Subject: [PATCH] Add request-middleware test file --- .../homebrew/utils/request-middleware.spec.js | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 client/homebrew/utils/request-middleware.spec.js diff --git a/client/homebrew/utils/request-middleware.spec.js b/client/homebrew/utils/request-middleware.spec.js new file mode 100644 index 000000000..d7c198394 --- /dev/null +++ b/client/homebrew/utils/request-middleware.spec.js @@ -0,0 +1,74 @@ +import requestMiddleware from './request-middleware'; + +jest.mock('superagent'); +import request from 'superagent'; + +describe('request-middleware', ()=>{ + let version; + + let setFn; + let testFn; + + beforeEach(()=>{ + jest.resetAllMocks(); + version = global.version; + + global.version = '999'; + + setFn = jest.fn(); + testFn = jest.fn(()=>{ return { set: setFn }; }); + }); + + afterEach(()=>{ + global.version = version; + }); + + it('should add header to get', ()=>{ + // Ensure tests functions have been reset + expect(testFn).not.toHaveBeenCalled(); + expect(setFn).not.toHaveBeenCalled(); + + request.get = testFn; + + requestMiddleware.get('path'); + + expect(testFn).toHaveBeenCalledWith('path'); + expect(setFn).toHaveBeenCalledWith('Homebrewery-Version', '999'); + }); + + it('should add header to put', ()=>{ + expect(testFn).not.toHaveBeenCalled(); + expect(setFn).not.toHaveBeenCalled(); + + request.put = testFn; + + requestMiddleware.put('path'); + + expect(testFn).toHaveBeenCalledWith('path'); + expect(setFn).toHaveBeenCalledWith('Homebrewery-Version', '999'); + }); + + it('should add header to post', ()=>{ + expect(testFn).not.toHaveBeenCalled(); + expect(setFn).not.toHaveBeenCalled(); + + request.post = testFn; + + requestMiddleware.post('path'); + + expect(testFn).toHaveBeenCalledWith('path'); + expect(setFn).toHaveBeenCalledWith('Homebrewery-Version', '999'); + }); + + it('should add header to delete', ()=>{ + expect(testFn).not.toHaveBeenCalled(); + expect(setFn).not.toHaveBeenCalled(); + + request.delete = testFn; + + requestMiddleware.delete('path'); + + expect(testFn).toHaveBeenCalledWith('path'); + expect(setFn).toHaveBeenCalledWith('Homebrewery-Version', '999'); + }); +});