-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
instead of calling getMessages() arbitrarily
- Loading branch information
1 parent
7e65e00
commit f6e1fb5
Showing
6 changed files
with
51 additions
and
111 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,29 @@ | ||
const proxyquire = require('proxyquire') | ||
const { faker } = require('@faker-js/faker') | ||
|
||
const words = faker.lorem.words | ||
const modulePath = '../user-locals' | ||
|
||
describe('user-locals middleware', () => { | ||
let req, res, next | ||
beforeEach(() => { | ||
res = { | ||
locals: {}, | ||
const { parseFlashMessages } = require('../user-locals') | ||
|
||
describe('parseFlashMessages', () => { | ||
it('valid JSON in success:with-body', () => { | ||
const BODY = { heading: 'foo bar', body: 'baz bing' } | ||
const RAW_FLASH_MESSAGES = { | ||
success: ['foo', '{"test":1}'], | ||
error: ['bar', 'baz'], | ||
'success:with-body': [JSON.stringify(BODY)], | ||
} | ||
next = sinon.spy() | ||
}) | ||
|
||
describe('#getMessages', () => { | ||
afterEach(() => { | ||
expect(next).to.have.been.called | ||
expect(parseFlashMessages(RAW_FLASH_MESSAGES)).to.deep.equal({ | ||
...RAW_FLASH_MESSAGES, | ||
'success:with-body': [BODY], | ||
}) | ||
}) | ||
|
||
context('With no messages in the flash', () => { | ||
it('returns the values', () => { | ||
const userLocals = require(modulePath) | ||
const flash = sinon.stub().returns({}) | ||
req = { | ||
path: '', | ||
flash, | ||
} | ||
userLocals(req, res, next) | ||
|
||
const messages = res.locals.getMessages() | ||
|
||
expect(flash).to.have.been.calledWith() | ||
expect(messages).to.deep.equal({}) | ||
}) | ||
}) | ||
|
||
context('With valid messages in the flash', () => { | ||
it('returns the values', () => { | ||
const userLocals = require(modulePath) | ||
const flashData = { | ||
success: [words(5), '{"test":1}'], | ||
error: [words(5), words(5)], | ||
'success:with-body': [ | ||
JSON.stringify({ heading: words(2), body: words(10) }), | ||
], | ||
} | ||
const flash = sinon.stub().returns(flashData) | ||
req = { | ||
path: '', | ||
flash, | ||
} | ||
|
||
userLocals(req, res, next) | ||
|
||
const messages = res.locals.getMessages() | ||
|
||
expect(messages).to.equal(flashData) | ||
expect(typeof messages['success:with-body'][0]).to.equal('object') | ||
expect(typeof messages.success[1]).to.equal('string') | ||
expect(flash).to.have.been.calledWith() | ||
}) | ||
}) | ||
|
||
context('With invalid JSON messages in the flash', () => { | ||
it('returns the string and reports the error', () => { | ||
const captureException = sinon.spy() | ||
const userLocals = proxyquire(modulePath, { | ||
'../lib/reporter': { | ||
captureException, | ||
}, | ||
}) | ||
const flashData = { | ||
'success:with-body': [`heading: ${words(2)}`], | ||
} | ||
const flash = sinon.stub().returns(flashData) | ||
req = { | ||
path: '', | ||
flash, | ||
} | ||
|
||
userLocals(req, res, next) | ||
|
||
const messages = res.locals.getMessages() | ||
it('invalid JSON in success:with-body', () => { | ||
const RAW_FLASH_MESSAGES = { | ||
success: ['foo', '{"test":1}'], | ||
error: ['bar', 'baz'], | ||
'success:with-body': ["I'm no valid JSON"], | ||
} | ||
|
||
expect(messages).to.equal(flashData) | ||
expect(typeof messages['success:with-body'][0]).to.equal('string') | ||
expect(flash).to.have.been.calledWith() | ||
expect(captureException).to.have.been.called | ||
}) | ||
}) | ||
expect(parseFlashMessages(RAW_FLASH_MESSAGES)).to.deep.equal( | ||
RAW_FLASH_MESSAGES | ||
) | ||
}) | ||
}) |
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
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
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
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