From ae336f14294a7323f888ccdd477495a12b8bae68 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Tue, 13 Aug 2024 22:30:59 +1200 Subject: [PATCH] Add extra getCSS tests --- server/homebrew.api.spec.js | 54 ++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/server/homebrew.api.spec.js b/server/homebrew.api.spec.js index 287e68ce4..22c53a409 100644 --- a/server/homebrew.api.spec.js +++ b/server/homebrew.api.spec.js @@ -50,6 +50,7 @@ describe('Tests for api', ()=>{ res = { status : jest.fn(()=>res), send : jest.fn(()=>{}), + set : jest.fn(()=>{}), setHeader : jest.fn(()=>{}) }; @@ -917,7 +918,7 @@ brew`); }); }); describe('Get CSS tests', ()=>{ - it('should return CSS for a brew', async ()=>{ + it('get CSS - successful', async ()=>{ const testBrew = { title: 'test brew', text: '```css\n\nI Have a style!\n````\n\n' }; const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew })); @@ -926,21 +927,54 @@ brew`); const fn = api.getBrew('share', true); const req = { brew: {} }; - const res = { - status : jest.fn(()=>res), - send : jest.fn(()=>{}), - set : jest.fn(()=>{}), - setHeader : jest.fn(()=>{}) - }; const next = jest.fn(); await fn(req, null, next); await api.getCSS(req, res); expect(req.brew).toEqual(testBrew); expect(req.brew).toHaveProperty('style', '\nI Have a style!\n'); - expect(next).toHaveBeenCalled(); - expect(api.getId).toHaveBeenCalledWith(req); - expect(model.get).toHaveBeenCalledWith({ shareId: '1' }); + expect(res.set).toHaveBeenCalledWith({ + 'Cache-Control' : 'no-cache', + 'Content-Type' : 'text/css' + }); + }); + + it('get CSS - no style in brew', async ()=>{ + const testBrew = { title: 'test brew', text: 'I don\'t have a style!' }; + + const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew })); + api.getId = jest.fn(()=>({ id: '1', googleId: undefined })); + model.get = jest.fn(()=>toBrewPromise(testBrew)); + + const fn = api.getBrew('share', true); + const req = { brew: {} }; + const next = jest.fn(); + await fn(req, null, next); + await api.getCSS(req, res); + + expect(req.brew).toEqual(testBrew); + expect(req.brew).toHaveProperty('style'); + expect(res.status).toHaveBeenCalledWith(404); + expect(res.send).toHaveBeenCalledWith(''); + }); + + it('get CSS - no brew', async ()=>{ + const testBrew = { }; + + const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew })); + api.getId = jest.fn(()=>({ id: '1', googleId: undefined })); + model.get = jest.fn(()=>toBrewPromise(testBrew)); + + const fn = api.getBrew('share', true); + const req = { brew: {} }; + const next = jest.fn(); + await fn(req, null, next); + await api.getCSS(req, res); + + expect(req.brew).toEqual(testBrew); + expect(req.brew).toHaveProperty('style'); + expect(res.status).toHaveBeenCalledWith(404); + expect(res.send).toHaveBeenCalledWith(''); }); }); });