@financial-times/dotcom-ui-flags 7.3.1 → 7.3.3

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-flags",
3
- "version": "7.3.1",
3
+ "version": "7.3.3",
4
4
  "description": "",
5
5
  "main": "component.js",
6
6
  "browser": "browser.js",
@@ -25,13 +25,17 @@
25
25
  "npm": "7.x || 8.x"
26
26
  },
27
27
  "dependencies": {
28
- "@financial-times/dotcom-ui-data-embed": "file:../dotcom-ui-data-embed"
28
+ "@financial-times/dotcom-ui-data-embed": "^7.3.3"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "react": "16.x || 17.x"
32
32
  },
33
33
  "files": [
34
- "dist/"
34
+ "dist/",
35
+ "src/",
36
+ "browser.js",
37
+ "component.js",
38
+ "server.js"
35
39
  ],
36
40
  "repository": {
37
41
  "type": "git",
@@ -46,4 +50,4 @@
46
50
  "check-engine": "^1.10.1",
47
51
  "react": "^16.8.6"
48
52
  }
49
- }
53
+ }
package/server.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/node/server/index')
@@ -0,0 +1,32 @@
1
+ import Subject from '../../client/Flags'
2
+
3
+ const fixture = { foo: 1, bar: true }
4
+
5
+ describe('dotcom-ui-flags/src/client/Flags', () => {
6
+ let instance
7
+
8
+ beforeEach(() => {
9
+ instance = new Subject(fixture)
10
+ })
11
+
12
+ describe('.get()', () => {
13
+ it('returns the value of a flag which exists', () => {
14
+ expect(instance.get('foo')).toBe(1)
15
+ expect(instance.get('bar')).toBe(true)
16
+ })
17
+
18
+ it('returns undefined for flags which do not exist', () => {
19
+ expect(instance.get('baz')).toBeUndefined()
20
+ })
21
+ })
22
+
23
+ describe('.getAll()', () => {
24
+ it('returns all flags data', () => {
25
+ expect(instance.getAll()).toEqual(fixture)
26
+ })
27
+
28
+ it('freezes the flags data', () => {
29
+ expect(Object.isFrozen(instance.getAll())).toBe(true)
30
+ })
31
+ })
32
+ })
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ */
4
+
5
+ import subject from '../../client/loadFlags'
6
+ import { SCRIPT_ELEMENT_ID } from '../../constants'
7
+
8
+ describe('dotcom-ui-flags/src/client/loadFlags', () => {
9
+ describe('when there is a configuration object', () => {
10
+ beforeEach(() => {
11
+ document.body.innerHTML = `
12
+ <script id="${SCRIPT_ELEMENT_ID}">{"foo":1,"bar":true,"baz":"qux"}</script>
13
+ `
14
+ })
15
+
16
+ it('returns a frozen object', () => {
17
+ const result = subject()
18
+
19
+ expect(result).toEqual({ foo: 1, bar: true, baz: 'qux' })
20
+ expect(Object.isFrozen(result)).toBe(true)
21
+ })
22
+ })
23
+
24
+ describe('when there is no a configuration object', () => {
25
+ beforeEach(() => {
26
+ document.body.innerHTML = ''
27
+ })
28
+
29
+ it('returns a frozen empty object', () => {
30
+ const result = subject()
31
+
32
+ expect(result).toEqual({})
33
+ expect(Object.isFrozen(result)).toBe(true)
34
+ })
35
+ })
36
+ })
@@ -0,0 +1,14 @@
1
+ import renderer from 'react-test-renderer'
2
+ import { FlagsEmbed as subject } from '../../components/FlagsEmbed'
3
+
4
+ const flags = {
5
+ foo: 1,
6
+ baz: 'qux'
7
+ }
8
+
9
+ describe('dotcom-ui-flags/src/components/FlagsEmbed', () => {
10
+ it('renders a script element containing flags', () => {
11
+ const tree = renderer.create(subject({ flags }))
12
+ expect(tree).toMatchSnapshot()
13
+ })
14
+ })
@@ -0,0 +1,13 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`dotcom-ui-flags/src/components/FlagsEmbed renders a script element containing flags 1`] = `
4
+ <script
5
+ dangerouslySetInnerHTML={
6
+ Object {
7
+ "__html": "{\\"foo\\":1,\\"baz\\":\\"qux\\"}",
8
+ }
9
+ }
10
+ id="page-kit-flags-embed"
11
+ type="application/json"
12
+ />
13
+ `;
@@ -0,0 +1,10 @@
1
+ import subject from '../../server/formatFlagsJSON'
2
+
3
+ const fixture = Object.freeze({ foo: 1, bar: false, baz: 'qux' })
4
+
5
+ describe('dotcom-ui-flags/src/server/formatFlagsJSON', () => {
6
+ it('filters out properties with falsey values', () => {
7
+ const result = subject(fixture)
8
+ expect(result).toStrictEqual({ foo: 1, baz: 'qux' })
9
+ })
10
+ })
@@ -0,0 +1,16 @@
1
+ import { TFlagsData, TFlag } from '../types'
2
+
3
+ export default class Flags {
4
+ private flags: TFlagsData
5
+ constructor(flags: TFlagsData) {
6
+ this.flags = Object.freeze(flags)
7
+ }
8
+
9
+ get(flag: string): TFlag | undefined {
10
+ return this.flags.hasOwnProperty(flag) ? this.flags[flag] : undefined
11
+ }
12
+
13
+ getAll(): TFlagsData {
14
+ return this.flags
15
+ }
16
+ }
@@ -0,0 +1,9 @@
1
+ import loadFlags from './loadFlags'
2
+ import Flags from './Flags'
3
+
4
+ const init = () => {
5
+ const flagsData = loadFlags()
6
+ return new Flags(flagsData)
7
+ }
8
+
9
+ export { loadFlags, Flags, init }
@@ -0,0 +1,7 @@
1
+ import { loadDataEmbed } from '@financial-times/dotcom-ui-data-embed'
2
+ import { TFlagsData } from '../types'
3
+ import { SCRIPT_ELEMENT_ID } from '../constants'
4
+
5
+ export default function loadFlags(): TFlagsData {
6
+ return loadDataEmbed(SCRIPT_ELEMENT_ID)
7
+ }
@@ -0,0 +1,19 @@
1
+ import { DataEmbed } from '@financial-times/dotcom-ui-data-embed'
2
+ import { formatFlagsJSON } from '../server'
3
+ import { TFlagsData } from '../types'
4
+ import { SCRIPT_ELEMENT_ID } from '../constants'
5
+
6
+ type TFlagsEmbedProps = {
7
+ flags?: TFlagsData
8
+ }
9
+
10
+ function FlagsEmbed({ flags }: TFlagsEmbedProps) {
11
+ return DataEmbed({ id: SCRIPT_ELEMENT_ID, data: formatFlagsJSON(flags) })
12
+ }
13
+
14
+ FlagsEmbed.defaultProps = {
15
+ flags: {}
16
+ }
17
+
18
+ export { FlagsEmbed }
19
+ export type { TFlagsEmbedProps }
@@ -0,0 +1 @@
1
+ export const SCRIPT_ELEMENT_ID = 'page-kit-flags-embed'
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './components/FlagsEmbed'
2
+ export * from './constants'
@@ -0,0 +1,15 @@
1
+ import { TFlagsData } from '../types'
2
+
3
+ export default function formatFlagsJSON(flags: TFlagsData = {}): TFlagsData {
4
+ const output = {}
5
+
6
+ Object.keys(flags).forEach((key) => {
7
+ const value = flags[key]
8
+
9
+ if (value) {
10
+ output[key] = value
11
+ }
12
+ })
13
+
14
+ return output
15
+ }
@@ -0,0 +1,2 @@
1
+ import formatFlagsJSON from './formatFlagsJSON'
2
+ export { formatFlagsJSON }
package/src/types.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export type TFlag = string | boolean | number
2
+
3
+ export interface TFlagsData {
4
+ [key: string]: TFlag
5
+ }