@financial-times/dotcom-server-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/package.json CHANGED
@@ -1,18 +1,17 @@
1
1
  {
2
2
  "name": "@financial-times/dotcom-server-app-context",
3
- "version": "7.3.0",
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
  "generate-schema": "node scripts/schemaToMarkdown.js > schema.md",
9
9
  "test": "echo \"Error: no test specified\" && exit 1",
10
- "tsc": "../../node_modules/.bin/tsc --incremental",
11
10
  "clean": "npm run clean:dist && npm run clean:node_modules",
12
11
  "clean:dist": "rm -rf dist",
13
12
  "clean:node_modules": "rm -rf node_modules",
13
+ "build:node": "tsc",
14
14
  "build": "npm run build:node",
15
- "build:node": "npm run tsc -- --module commonjs --outDir ./dist/node",
16
15
  "dev": "npm run build:node -- --watch",
17
16
  "preinstall": "[ \"$INIT_CWD\" != \"$PWD\" ] || npm_config_yes=true npx check-engine"
18
17
  },
@@ -30,6 +29,10 @@
30
29
  "node": ">= 14.0.0",
31
30
  "npm": "7.x || 8.x"
32
31
  },
32
+ "files": [
33
+ "dist/",
34
+ "src/"
35
+ ],
33
36
  "repository": {
34
37
  "type": "git",
35
38
  "repository": "https://github.com/Financial-Times/dotcom-page-kit.git",
@@ -39,4 +42,4 @@
39
42
  "volta": {
40
43
  "extends": "../../package.json"
41
44
  }
42
- }
45
+ }
@@ -0,0 +1,75 @@
1
+ import { AppContext } from '../AppContext'
2
+ import * as fixtures from './__fixtures__/contextData'
3
+
4
+ describe('dotcom-server-app-context/src/AppContext', () => {
5
+ describe('constructor', () => {
6
+ let instance
7
+
8
+ beforeAll(() => {
9
+ instance = new AppContext({ appContext: fixtures.validAppContext })
10
+ })
11
+
12
+ it('sets the given app context data', () => {
13
+ expect(instance.data).toEqual(fixtures.validAppContext)
14
+ })
15
+
16
+ describe('invalid data', () => {
17
+ it('throws if any app context data is invalid', () => {
18
+ const init = () =>
19
+ new AppContext({
20
+ appContext: fixtures.invalidAppContext as any
21
+ })
22
+
23
+ expect(init).toThrow()
24
+ })
25
+ })
26
+ })
27
+
28
+ describe('.get()', () => {
29
+ let instance
30
+
31
+ beforeEach(() => {
32
+ instance = new AppContext({ appContext: fixtures.validAppContext })
33
+ })
34
+
35
+ it('returns the value of the requested app context property', () => {
36
+ const result = instance.get('appVersion')
37
+ expect(result).toBe(fixtures.validAppContext.appVersion)
38
+ })
39
+ })
40
+
41
+ describe('.set()', () => {
42
+ let instance
43
+
44
+ beforeEach(() => {
45
+ instance = new AppContext()
46
+ })
47
+
48
+ it('sets the value of the specified property', () => {
49
+ instance.set('appVersion', 'v12')
50
+ expect(instance.data.appVersion).toBe('v12')
51
+ })
52
+
53
+ it('throws if the given value is invalid', () => {
54
+ expect(() => instance.set('conceptId', 123)).toThrow()
55
+ })
56
+ })
57
+
58
+ describe('.getAll()', () => {
59
+ let instance
60
+
61
+ beforeEach(() => {
62
+ instance = new AppContext({ appContext: fixtures.validAppContext })
63
+ })
64
+
65
+ it('returns a clone of the app context data', () => {
66
+ const result = instance.getAll()
67
+ expect(result).not.toBe(instance.data)
68
+ })
69
+
70
+ it('freezes the app context data clone', () => {
71
+ const result = instance.getAll()
72
+ expect(Object.isFrozen(result)).toBe(true)
73
+ })
74
+ })
75
+ })
@@ -0,0 +1,18 @@
1
+ export const validAppContext = Object.freeze({
2
+ appName: 'article',
3
+ appVersion: '882797258625531f20d604f6441ef8cfcb2d772b',
4
+ edition: 'uk',
5
+ product: 'next',
6
+ abTestState: 'subscriberCohort:on,premiumCohort:on,topicTracker_UIDemo:100-percent',
7
+ contentId: 'c5935758-7730-11e9-bbad-7c18c0ea0201',
8
+ contentType: 'article',
9
+ conceptId: 'c5935738-7730-11e9-bbad-7c18c0ea8201',
10
+ conceptType: 'http://www.ft.com/ontology/Location',
11
+ isProduction: true,
12
+ publishReference: 'tid_17wmwszvk3'
13
+ })
14
+
15
+ export const invalidAppContext = {
16
+ ...validAppContext,
17
+ isProduction: 'yes'
18
+ }
@@ -0,0 +1,32 @@
1
+ import subject from '../filterEmptyData'
2
+
3
+ const fixture = Object.freeze({
4
+ appName: 'article',
5
+ abTest: ' ',
6
+ appVersion: null,
7
+ isProduction: false
8
+ })
9
+
10
+ describe('dotcom-server-app-context/src/filterEmptyData', () => {
11
+ it('returns a new object', () => {
12
+ const result = subject(fixture)
13
+ expect(result).not.toBe(fixture)
14
+ })
15
+
16
+ it('removes null values', () => {
17
+ const result = subject(fixture)
18
+ expect(result).not.toHaveProperty('appVersion')
19
+ })
20
+
21
+ it('removes empty string values', () => {
22
+ const result = subject(fixture)
23
+ expect(result).not.toHaveProperty('abTest')
24
+ })
25
+
26
+ it('copies all defined values', () => {
27
+ const result = subject(fixture)
28
+
29
+ expect(result).toHaveProperty('appName', 'article')
30
+ expect(result).toHaveProperty('isProduction', false)
31
+ })
32
+ })
@@ -0,0 +1,20 @@
1
+ import subject from '../validate'
2
+
3
+ describe('dotcom-server-app-context/src/validate', () => {
4
+ it('returns true when given a valid property/value', () => {
5
+ expect(subject('isProduction', true)).toBe(true)
6
+ })
7
+
8
+ it('throws an error when given an invalid property/value', () => {
9
+ expect(() => subject('isProduction', 'yes')).toThrow(
10
+ 'Validation error: data.isProduction should be boolean'
11
+ )
12
+ })
13
+
14
+ it('throws an error when given an unknown property/value', () => {
15
+ expect(() => subject('thisProperty', 'isNotInTheSchema')).toThrow(
16
+ 'Validation error: data should NOT have additional properties, received "isNotInTheSchema"' +
17
+ '\nIf you want to share application specific data with the client, consider using @financial-times/dotcom-ui-data-embed.'
18
+ )
19
+ })
20
+ })
package/schema.md DELETED
@@ -1,97 +0,0 @@
1
- # FT App Context Schema
2
-
3
- The schema defines the following properties:
4
-
5
- ## `abTestState` (string)
6
-
7
- The A/B test flags data as a comma delimited string
8
-
9
- Additional restrictions:
10
-
11
- * Regex pattern: `^,*([0-9A-Za-z-_]+:[0-9A-Za-z-_]+,*)+$`
12
-
13
- ## `appName` (string)
14
-
15
- The name of the application
16
-
17
- Additional restrictions:
18
-
19
- * Regex pattern: `^.+$`
20
-
21
- ## `appVersion` (string)
22
-
23
- The running version of the app (usually a Git commit hash)
24
-
25
- Additional restrictions:
26
-
27
- * Regex pattern: `^.+$`
28
-
29
- ## `conceptId` (string)
30
-
31
- The UUID of the concept on the current page
32
-
33
- Additional restrictions:
34
-
35
- * Regex pattern: `^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$`
36
-
37
- ## `conceptType` (string)
38
-
39
- The type of concept on the current page
40
-
41
- Additional restrictions:
42
-
43
- * Regex pattern: `^http://www.ft.com/ontology/.+$`
44
-
45
- ## `contentId` (string)
46
-
47
- The UUID of the content on the current page
48
-
49
- Additional restrictions:
50
-
51
- * Regex pattern: `^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$`
52
-
53
- ## `contentType` (string)
54
-
55
- The type or sub-type of the content on the current page
56
-
57
- Additional restrictions:
58
-
59
- * Regex pattern: `^(article|video|audio|podcast|package|live-blog)$`
60
-
61
- ## `edition` (string)
62
-
63
- The selected FT edition
64
-
65
- Additional restrictions:
66
-
67
- * Regex pattern: `^(uk|international)$`
68
-
69
- ## `isProduction` (boolean)
70
-
71
- If the app is currently running in a production environment
72
-
73
- Default: `false`
74
-
75
- ## `isUserLoggedIn` (boolean)
76
-
77
- If the visitor is signed in to an FT account
78
-
79
- Default: `false`
80
-
81
- ## `product` (string)
82
-
83
- The product name
84
-
85
- Default: `"next"`
86
-
87
- Additional restrictions:
88
-
89
- * Regex pattern: `^.+$`
90
-
91
- ## `publishReference` (string)
92
-
93
- The publish reference of the content on the current page
94
-
95
- Additional restrictions:
96
-
97
- * Regex pattern: `^.+$`
@@ -1,4 +0,0 @@
1
- const parse = require('json-schema-to-markdown')
2
- const schema = require('../src/schema.json')
3
-
4
- console.log(parse(schema)) // eslint-disable-line no-console