@financial-times/dotcom-middleware-app-context 7.3.0 → 7.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/node/index.d.ts +6 -0
- package/dist/tsconfig.tsbuildinfo +3260 -0
- package/package.json +8 -5
- package/src/__test__/index.spec.ts +101 -0
package/package.json
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
{
|
2
2
|
"name": "@financial-times/dotcom-middleware-app-context",
|
3
|
-
"version": "7.3.
|
3
|
+
"version": "7.3.2",
|
4
4
|
"description": "",
|
5
5
|
"main": "dist/node/index.js",
|
6
6
|
"types": "src/index.ts",
|
7
7
|
"scripts": {
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1",
|
9
|
-
"tsc": "../../node_modules/.bin/tsc --incremental",
|
10
9
|
"clean": "npm run clean:dist && npm run clean:node_modules",
|
11
10
|
"clean:dist": "rm -rf dist",
|
12
11
|
"clean:node_modules": "rm -rf node_modules",
|
12
|
+
"build:node": "tsc",
|
13
13
|
"build": "npm run build:node",
|
14
14
|
"dev": "npm run build:node -- --watch",
|
15
|
-
"build:node": "npm run tsc -- --module commonjs --outDir ./dist/node",
|
16
15
|
"preinstall": "[ \"$INIT_CWD\" != \"$PWD\" ] || npm_config_yes=true npx check-engine"
|
17
16
|
},
|
18
17
|
"keywords": [],
|
@@ -25,12 +24,16 @@
|
|
25
24
|
"node-mocks-http": "^1.7.5"
|
26
25
|
},
|
27
26
|
"dependencies": {
|
28
|
-
"@financial-times/dotcom-server-app-context": "
|
27
|
+
"@financial-times/dotcom-server-app-context": "file:../dotcom-server-app-context"
|
29
28
|
},
|
30
29
|
"engines": {
|
31
30
|
"node": ">= 14.0.0",
|
32
31
|
"npm": "7.x || 8.x"
|
33
32
|
},
|
33
|
+
"files": [
|
34
|
+
"dist/",
|
35
|
+
"src/"
|
36
|
+
],
|
34
37
|
"repository": {
|
35
38
|
"type": "git",
|
36
39
|
"repository": "https://github.com/Financial-Times/dotcom-page-kit.git",
|
@@ -40,4 +43,4 @@
|
|
40
43
|
"volta": {
|
41
44
|
"extends": "../../package.json"
|
42
45
|
}
|
43
|
-
}
|
46
|
+
}
|
@@ -0,0 +1,101 @@
|
|
1
|
+
jest.mock('@financial-times/dotcom-server-app-context')
|
2
|
+
|
3
|
+
import httpMocks from 'node-mocks-http'
|
4
|
+
import { AppContext } from '@financial-times/dotcom-server-app-context'
|
5
|
+
import { init } from '../index'
|
6
|
+
|
7
|
+
const appContext = {
|
8
|
+
appName: 'my-app-name',
|
9
|
+
appVersion: '123'
|
10
|
+
}
|
11
|
+
|
12
|
+
const headers = {
|
13
|
+
'ft-edition': 'international',
|
14
|
+
'ft-ab': '-',
|
15
|
+
'ft-anonymous-user': '-'
|
16
|
+
}
|
17
|
+
|
18
|
+
describe('dotcom-middleware-app-context', () => {
|
19
|
+
let instance
|
20
|
+
let request
|
21
|
+
let response
|
22
|
+
let next
|
23
|
+
|
24
|
+
beforeEach(() => {
|
25
|
+
request = httpMocks.createRequest({ headers })
|
26
|
+
response = httpMocks.createResponse({ locals: {} })
|
27
|
+
next = jest.fn()
|
28
|
+
instance = init({ appContext })
|
29
|
+
})
|
30
|
+
|
31
|
+
afterEach(() => {
|
32
|
+
jest.resetAllMocks()
|
33
|
+
})
|
34
|
+
|
35
|
+
it('returns a request handler function', () => {
|
36
|
+
expect(instance).toBeInstanceOf(Function)
|
37
|
+
})
|
38
|
+
|
39
|
+
describe('when handling a request', () => {
|
40
|
+
it('initialises app context with inferred data', () => {
|
41
|
+
const expected = {
|
42
|
+
appContext: expect.objectContaining({ edition: 'international' })
|
43
|
+
}
|
44
|
+
|
45
|
+
instance(request, response, next)
|
46
|
+
|
47
|
+
expect(AppContext).toHaveBeenCalledWith(expected)
|
48
|
+
})
|
49
|
+
|
50
|
+
it('ignores default "-" header values', () => {
|
51
|
+
const expectedA = {
|
52
|
+
appContext: expect.not.objectContaining({ abTestState: '-' })
|
53
|
+
}
|
54
|
+
|
55
|
+
const expectedB = {
|
56
|
+
appContext: expect.objectContaining({ isUserLoggedIn: false })
|
57
|
+
}
|
58
|
+
|
59
|
+
instance(request, response, next)
|
60
|
+
|
61
|
+
expect(AppContext).toHaveBeenCalledWith(expectedA)
|
62
|
+
expect(AppContext).toHaveBeenCalledWith(expectedB)
|
63
|
+
})
|
64
|
+
|
65
|
+
it('initialises app context with provided app context overrides', () => {
|
66
|
+
const expected = { appContext: expect.objectContaining(appContext) }
|
67
|
+
|
68
|
+
instance(request, response, next)
|
69
|
+
|
70
|
+
expect(AppContext).toHaveBeenCalledWith(expected)
|
71
|
+
})
|
72
|
+
|
73
|
+
it('appends the app context instance to response.locals', () => {
|
74
|
+
instance(request, response, next)
|
75
|
+
expect(response.locals.appContext).toBeInstanceOf(AppContext)
|
76
|
+
})
|
77
|
+
|
78
|
+
it('calls the fallthrough function', () => {
|
79
|
+
instance(request, response, next)
|
80
|
+
expect(next).toHaveBeenCalled()
|
81
|
+
})
|
82
|
+
})
|
83
|
+
|
84
|
+
describe('when the app context data is invalid', () => {
|
85
|
+
beforeEach(() => {
|
86
|
+
// NOTE: AppContext has been mocked but we must first
|
87
|
+
// tell TS it's a mock before we can use it like one.
|
88
|
+
const AppContextMock = AppContext as jest.Mock
|
89
|
+
|
90
|
+
AppContextMock.mockImplementation(() => {
|
91
|
+
throw Error('INVALID')
|
92
|
+
})
|
93
|
+
})
|
94
|
+
|
95
|
+
it('calls the fallthrough function with the error', () => {
|
96
|
+
instance(request, response, next)
|
97
|
+
|
98
|
+
expect(next).toHaveBeenCalledWith(expect.any(Error))
|
99
|
+
})
|
100
|
+
})
|
101
|
+
})
|