@financial-times/dotcom-ui-app-context 7.3.1 → 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,6 +1,6 @@
1
1
  {
2
2
  "name": "@financial-times/dotcom-ui-app-context",
3
- "version": "7.3.1",
3
+ "version": "7.3.2",
4
4
  "description": "",
5
5
  "main": "component.js",
6
6
  "browser": "browser.js",
@@ -25,7 +25,10 @@
25
25
  "npm": "7.x || 8.x"
26
26
  },
27
27
  "files": [
28
- "dist/"
28
+ "dist/",
29
+ "src/",
30
+ "browser.js",
31
+ "component.js"
29
32
  ],
30
33
  "repository": {
31
34
  "type": "git",
@@ -0,0 +1,41 @@
1
+ import subject from '../client/AppContext'
2
+
3
+ const fakeContext = {
4
+ appContext: {
5
+ appName: 'app-name',
6
+ appVersion: '123',
7
+ edition: 'uk',
8
+ product: 'next',
9
+ abTestState: 'someCohort:on',
10
+ isProduction: true
11
+ }
12
+ }
13
+
14
+ describe('dotcom-ui-app-context/src/client/AppContext', () => {
15
+ let instance
16
+
17
+ beforeEach(() => {
18
+ instance = new subject(fakeContext)
19
+ })
20
+
21
+ describe('.get()', () => {
22
+ it('returns the value of an existing app context', () => {
23
+ expect(instance.get('appName')).toBe('app-name')
24
+ expect(instance.get('isProduction')).toBe(true)
25
+ })
26
+
27
+ it('returns undefined for contexts which do not exist', () => {
28
+ expect(instance.get('foo')).toBeUndefined()
29
+ })
30
+ })
31
+
32
+ describe('.getAll()', () => {
33
+ it('returns all app context data', () => {
34
+ expect(instance.getAll()).toEqual(fakeContext.appContext)
35
+ })
36
+
37
+ it('freezes the app context data', () => {
38
+ expect(Object.isFrozen(instance.getAll())).toBe(true)
39
+ })
40
+ })
41
+ })
@@ -0,0 +1,19 @@
1
+ import renderer from 'react-test-renderer'
2
+ import { AppContextEmbed as subject } from '../components/AppContextEmbed'
3
+ import { TAppContext } from '../types'
4
+
5
+ const fakeContext: TAppContext = {
6
+ appName: 'app-name',
7
+ appVersion: '123',
8
+ edition: 'uk',
9
+ product: 'next',
10
+ abTestState: 'someCohort:on',
11
+ isProduction: true
12
+ }
13
+
14
+ describe('dotcom-ui-app-context/src/components/AppContextEmbed', () => {
15
+ it('renders a script element containing app context properties', () => {
16
+ const tree = renderer.create(subject({ appContext: fakeContext }))
17
+ expect(tree).toMatchSnapshot()
18
+ })
19
+ })
@@ -0,0 +1,13 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`dotcom-ui-app-context/src/components/AppContextEmbed renders a script element containing app context properties 1`] = `
4
+ <script
5
+ dangerouslySetInnerHTML={
6
+ Object {
7
+ "__html": "{\\"appName\\":\\"app-name\\",\\"appVersion\\":\\"123\\",\\"edition\\":\\"uk\\",\\"product\\":\\"next\\",\\"abTestState\\":\\"someCohort:on\\",\\"isProduction\\":true}",
8
+ }
9
+ }
10
+ id="page-kit-app-context"
11
+ type="application/json"
12
+ />
13
+ `;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ */
4
+
5
+ import subject from '../client/loadAppContext'
6
+
7
+ describe('dotcom-ui-app-context/src/client/loadAppContext', () => {
8
+ describe('when there is a configuration object', () => {
9
+ beforeEach(() => {
10
+ document.body.innerHTML = `
11
+ <script type="application/json" id="page-kit-app-context">{"appName":"app-name","appVersion":"123"}</script>
12
+ `
13
+ })
14
+
15
+ it('returns a frozen object', () => {
16
+ const result = subject()
17
+
18
+ expect(result).toEqual({ appName: 'app-name', appVersion: '123' })
19
+ expect(Object.isFrozen(result)).toBe(true)
20
+ })
21
+ })
22
+
23
+ describe('when there is no a configuration object', () => {
24
+ beforeEach(() => {
25
+ document.body.innerHTML = ''
26
+ })
27
+
28
+ it('returns a frozen empty object', () => {
29
+ const result = subject()
30
+
31
+ expect(result).toEqual({})
32
+ expect(Object.isFrozen(result)).toBe(true)
33
+ })
34
+ })
35
+ })
@@ -0,0 +1,20 @@
1
+ import { TAppContext } from '../types'
2
+
3
+ export type TAppContextOptions = {
4
+ appContext: TAppContext
5
+ }
6
+ export default class AppContext {
7
+ private appContext: TAppContext
8
+
9
+ constructor(options: TAppContextOptions) {
10
+ this.appContext = Object.freeze(options.appContext)
11
+ }
12
+
13
+ get(property: string): any {
14
+ return this.appContext.hasOwnProperty(property) ? this.appContext[property] : undefined
15
+ }
16
+
17
+ getAll(): TAppContext {
18
+ return this.appContext
19
+ }
20
+ }
@@ -0,0 +1,11 @@
1
+ import AppContext from './AppContext'
2
+ import loadEmbeddedAppContext from './loadAppContext'
3
+
4
+ export function init() {
5
+ const appContext = loadEmbeddedAppContext()
6
+ const client = new AppContext({ appContext })
7
+
8
+ console.log('Page Kit app context:', client.getAll()) // eslint-disable-line no-console
9
+
10
+ return client
11
+ }
@@ -0,0 +1,7 @@
1
+ import { loadDataEmbed } from '@financial-times/dotcom-ui-data-embed'
2
+ import { TAppContext } from '../types'
3
+ import { APP_CONTEXT_ELEMENT_ID } from '../constants'
4
+
5
+ export default function loadEmbeddedAppContext(): TAppContext {
6
+ return loadDataEmbed(APP_CONTEXT_ELEMENT_ID) as TAppContext
7
+ }
@@ -0,0 +1,11 @@
1
+ import { DataEmbed } from '@financial-times/dotcom-ui-data-embed'
2
+ import { TAppContext } from '../types'
3
+ import { APP_CONTEXT_ELEMENT_ID } from '../constants'
4
+
5
+ export type TAppContextProps = {
6
+ appContext?: TAppContext
7
+ }
8
+
9
+ export function AppContextEmbed({ appContext }: TAppContextProps) {
10
+ return DataEmbed({ id: APP_CONTEXT_ELEMENT_ID, data: appContext })
11
+ }
@@ -0,0 +1 @@
1
+ export const APP_CONTEXT_ELEMENT_ID = 'page-kit-app-context'
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './components/AppContextEmbed'
2
+ export * from './constants'
package/src/types.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ export interface TAppContext {
2
+ appName: string
3
+ appVersion: string
4
+ product: string
5
+ edition: string
6
+ abTestState: string
7
+ contentId?: string
8
+ contentType?: string
9
+ conceptId?: string
10
+ conceptType?: string
11
+ isProduction: boolean
12
+ publishReference?: string
13
+ [key: string]: any
14
+ }