Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/web/cortina.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ module.exports = function cortinaCommon(options) {
proxyPrefixPath,
hostUrl,
redisConfig,
redisKey,
siteNameKey = 'site_name',
localeTextKey = 'locale_text',
useStyle10 = false,
} = options

const blockView = useStyle10 ? 'style10' : 'style9'
const redisKey = (options.redisKey || 'CortinaBlock_') + (useStyle10 ? 'style10_' : 'style9_')

let { supportedLanguages = ['sv', 'en'] } = options
if (!supportedLanguages.length) supportedLanguages = ['sv', 'en']
const globalLink = supportedLanguages.length === 1 ? true : options.globalLink || false
Expand Down Expand Up @@ -70,6 +74,7 @@ module.exports = function cortinaCommon(options) {
redis: client,
blocks: addBlocks,
redisKey,
version: blockView,
})
.then(blocks => {
res.locals.blocks = _prepareBlocks(req, res, blocks)
Expand Down
94 changes: 94 additions & 0 deletions lib/web/cortina.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const cortina = require('./cortina')

jest.mock('@kth/log', () => ({ error: jest.fn() }))

jest.mock('@kth/cortina-block', () => jest.fn())
const mockCortinaPackage = require('@kth/cortina-block')

describe('cortina wrapper styleVersion', () => {
const mockReq = { query: {}, url: '', hostname: '' }
const mockRes = { locals: {} }
const mockNext = jest.fn()

mockCortinaPackage.mockResolvedValue([])

test('use "view style10" when useStyle10 is true', async () => {
const options = {
useStyle10: true,
}

const middleware = cortina(options)

await middleware(mockReq, mockRes, mockNext)

expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ version: 'style10' }))
})
test('use "view style9" when useStyle10 is false', async () => {
const options = {
useStyle10: false,
}

const middleware = cortina(options)

await middleware(mockReq, mockRes, mockNext)

expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ version: 'style9' }))
})
test('use "view style9" when useStyle10 is missing', async () => {
const options = {
useStyle10: undefined,
}

const middleware = cortina(options)

await middleware(mockReq, mockRes, mockNext)

expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ version: 'style9' }))
})

test('use redis key with "_style10" when useStyle10 is true', async () => {
const options = {
useStyle10: true,
}

const middleware = cortina(options)

await middleware(mockReq, mockRes, mockNext)

expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ redisKey: 'CortinaBlock_style10_' }))
})
test('use redis key with "_style9" when useStyle10 is false', async () => {
const options = {
useStyle10: false,
}

const middleware = cortina(options)

await middleware(mockReq, mockRes, mockNext)

expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ redisKey: 'CortinaBlock_style9_' }))
})
test('use redis key with "_style9" when useStyle10 is missing', async () => {
const options = {
useStyle10: undefined,
}

const middleware = cortina(options)

await middleware(mockReq, mockRes, mockNext)

expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ redisKey: 'CortinaBlock_style9_' }))
})
test('combine style suffix with custom key', async () => {
const options = {
redisKey: 'custom_key_',
useStyle10: true,
}

const middleware = cortina(options)

await middleware(mockReq, mockRes, mockNext)

expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ redisKey: 'custom_key_style10_' }))
})
})