Created
September 12, 2020 09:58
-
-
Save edharmowongso/a5f53f2e59f12b3721568c534593b053 to your computer and use it in GitHub Desktop.
Third Part (TDD)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Location: controller/__tests__/campaign.mocha.js | |
// LINE CODE 1 - 11 -> https://gist.github.com/edharmowongso/2a052f5b352bbe0dbfeb5a9dce1bdda8 | |
const { | |
CampaignMockedModules: { | |
'@pomona/pomona2-dbmodels': { | |
models: { | |
mongo: { campaign: Campaign } | |
} | |
} | |
}, | |
CampaignModule: CampaignController | |
} = require('./mocks/campaign.mockedModules') | |
describe('Campaign Controller test', function() { | |
describe('GetCampaignsByBrandId test', function() { | |
let res, | |
next, | |
wrongId = '5d0e0ef4ea9baa7fb13ddsadfa', | |
fakeId = '5d9afccd7758c65dba4a531c' | |
describe('Joi validation failed', function() { | |
beforeEach(function() { | |
res = mockRes() | |
next = sinon.stub() | |
}) | |
it('throws error if not sending brand_id attr in request params', async function() { | |
const req = mockReq({ | |
query: {}, | |
headers: {} | |
}) | |
await CampaignController.GetCampaignsByBrandId(req, res, next) | |
expect(next.lastCall.args[0]).match(/"brand_id" is required/gi) | |
}) | |
it('throws error if sending invalid brand_id attr in request params', async function() { | |
const req = mockReq({ | |
query: { brand_id: wrongId }, | |
headers: {} | |
}) | |
const matchedString = new RegExp( | |
`"brand_id" with value "${wrongId}" fails to match the valid mongo id pattern`, | |
'gi' | |
) | |
await CampaignController.GetCampaignsByBrandId(req, res, next) | |
expect(next.lastCall.args[0]).match(matchedString) | |
}) | |
}) | |
describe('Joi validation passed', function() { | |
it('returns expected results', async function() { | |
const expectedOutput = { | |
data: { | |
name: 'Noddle Festival', | |
start_at: '01-08-2020', | |
end_at: '01-10-2020', | |
products: [ | |
{ | |
id: 'abcdefg123456', | |
value: 'Chicken Noddle' | |
} | |
] | |
} | |
} | |
const req = mockReq({ | |
query: { brand_id: fakeId }, | |
headers: {} | |
}) | |
// stub Campaign.find | |
Campaign.find.returns(expectedOutput) | |
await CampaignController.GetCampaignsByBrandId(req, res, next) | |
expect(res.json.lastCall.args[0]).to.deep.equalInAnyOrder( | |
expectedOutput | |
) | |
}) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment