Skip to content

Commit 0e8eaf8

Browse files
authored
specify block view based on styleVersion (#55)
1 parent fec942a commit 0e8eaf8

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

lib/web/cortina.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ module.exports = function cortinaCommon(options) {
2424
proxyPrefixPath,
2525
hostUrl,
2626
redisConfig,
27-
redisKey,
2827
siteNameKey = 'site_name',
2928
localeTextKey = 'locale_text',
29+
useStyle10 = false,
3030
} = options
31+
32+
const blockView = useStyle10 ? 'style10' : 'style9'
33+
const redisKey = (options.redisKey || 'CortinaBlock_') + (useStyle10 ? 'style10_' : 'style9_')
34+
3135
let { supportedLanguages = ['sv', 'en'] } = options
3236
if (!supportedLanguages.length) supportedLanguages = ['sv', 'en']
3337
const globalLink = supportedLanguages.length === 1 ? true : options.globalLink || false
@@ -70,6 +74,7 @@ module.exports = function cortinaCommon(options) {
7074
redis: client,
7175
blocks: addBlocks,
7276
redisKey,
77+
version: blockView,
7378
})
7479
.then(blocks => {
7580
res.locals.blocks = _prepareBlocks(req, res, blocks)

lib/web/cortina.test.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const cortina = require('./cortina')
2+
3+
jest.mock('@kth/log', () => ({ error: jest.fn() }))
4+
5+
jest.mock('@kth/cortina-block', () => jest.fn())
6+
const mockCortinaPackage = require('@kth/cortina-block')
7+
8+
describe('cortina wrapper styleVersion', () => {
9+
const mockReq = { query: {}, url: '', hostname: '' }
10+
const mockRes = { locals: {} }
11+
const mockNext = jest.fn()
12+
13+
mockCortinaPackage.mockResolvedValue([])
14+
15+
test('use "view style10" when useStyle10 is true', async () => {
16+
const options = {
17+
useStyle10: true,
18+
}
19+
20+
const middleware = cortina(options)
21+
22+
await middleware(mockReq, mockRes, mockNext)
23+
24+
expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ version: 'style10' }))
25+
})
26+
test('use "view style9" when useStyle10 is false', async () => {
27+
const options = {
28+
useStyle10: false,
29+
}
30+
31+
const middleware = cortina(options)
32+
33+
await middleware(mockReq, mockRes, mockNext)
34+
35+
expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ version: 'style9' }))
36+
})
37+
test('use "view style9" when useStyle10 is missing', async () => {
38+
const options = {
39+
useStyle10: undefined,
40+
}
41+
42+
const middleware = cortina(options)
43+
44+
await middleware(mockReq, mockRes, mockNext)
45+
46+
expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ version: 'style9' }))
47+
})
48+
49+
test('use redis key with "_style10" when useStyle10 is true', async () => {
50+
const options = {
51+
useStyle10: true,
52+
}
53+
54+
const middleware = cortina(options)
55+
56+
await middleware(mockReq, mockRes, mockNext)
57+
58+
expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ redisKey: 'CortinaBlock_style10_' }))
59+
})
60+
test('use redis key with "_style9" when useStyle10 is false', async () => {
61+
const options = {
62+
useStyle10: false,
63+
}
64+
65+
const middleware = cortina(options)
66+
67+
await middleware(mockReq, mockRes, mockNext)
68+
69+
expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ redisKey: 'CortinaBlock_style9_' }))
70+
})
71+
test('use redis key with "_style9" when useStyle10 is missing', async () => {
72+
const options = {
73+
useStyle10: undefined,
74+
}
75+
76+
const middleware = cortina(options)
77+
78+
await middleware(mockReq, mockRes, mockNext)
79+
80+
expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ redisKey: 'CortinaBlock_style9_' }))
81+
})
82+
test('combine style suffix with custom key', async () => {
83+
const options = {
84+
redisKey: 'custom_key_',
85+
useStyle10: true,
86+
}
87+
88+
const middleware = cortina(options)
89+
90+
await middleware(mockReq, mockRes, mockNext)
91+
92+
expect(mockCortinaPackage).toBeCalledWith(expect.objectContaining({ redisKey: 'custom_key_style10_' }))
93+
})
94+
})

0 commit comments

Comments
 (0)