@financial-times/dotcom-ui-data-embed 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 +7 -3
- package/src/__test__/client/DataEmbedStore.spec.ts +37 -0
- package/src/__test__/client/index.spec.ts +16 -0
- package/src/__test__/client/loadDataEmbed.spec.ts +36 -0
- package/src/__test__/components/DataEmbed.spec.ts +18 -0
- package/src/__test__/components/__snapshots__/DataEmbed.spec.ts.snap +13 -0
- package/src/client/DataEmbedStore.ts +15 -0
- package/src/client/index.ts +9 -0
- package/src/client/loadDataEmbed.ts +15 -0
- package/src/components/DataEmbed.tsx +5 -0
- package/src/index.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@financial-times/dotcom-ui-data-embed",
|
|
3
|
-
"version": "7.3.
|
|
3
|
+
"version": "7.3.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "component.js",
|
|
6
6
|
"browser": "browser.js",
|
|
@@ -28,7 +28,11 @@
|
|
|
28
28
|
"react": "16.x || 17.x"
|
|
29
29
|
},
|
|
30
30
|
"files": [
|
|
31
|
-
"dist/"
|
|
31
|
+
"dist/",
|
|
32
|
+
"src/",
|
|
33
|
+
"browser.js",
|
|
34
|
+
"component.js",
|
|
35
|
+
"server.js"
|
|
32
36
|
],
|
|
33
37
|
"repository": {
|
|
34
38
|
"type": "git",
|
|
@@ -43,4 +47,4 @@
|
|
|
43
47
|
"check-engine": "^1.10.1",
|
|
44
48
|
"react": "^16.8.6"
|
|
45
49
|
}
|
|
46
|
-
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import subject from '../../client/DataEmbedStore'
|
|
2
|
+
|
|
3
|
+
const fakeData = {
|
|
4
|
+
foo: 1,
|
|
5
|
+
bar: true,
|
|
6
|
+
baz: 'qux'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe('dotcom-ui-data-embed/src/client/DataEmbedStore', () => {
|
|
10
|
+
let instance
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
instance = new subject(fakeData)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
describe('.get()', () => {
|
|
17
|
+
it('returns the value of an existing data', () => {
|
|
18
|
+
expect(instance.get('foo')).toBe(1)
|
|
19
|
+
expect(instance.get('bar')).toBe(true)
|
|
20
|
+
expect(instance.get('baz')).toBe('qux')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('returns undefined for properties that do not exist', () => {
|
|
24
|
+
expect(instance.get('buzz')).toBeUndefined()
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
describe('.getAll()', () => {
|
|
29
|
+
it('returns all data', () => {
|
|
30
|
+
expect(instance.getAll()).toEqual(fakeData)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('freezes the data', () => {
|
|
34
|
+
expect(Object.isFrozen(instance.getAll())).toBe(true)
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
})
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { init as subject } from '../../client'
|
|
2
|
+
import loadDataEmbed from '../../client/loadDataEmbed'
|
|
3
|
+
import DataEmbedStore from '../../client/DataEmbedStore'
|
|
4
|
+
jest.mock('../../client/loadDataEmbed.ts')
|
|
5
|
+
jest.mock('../../client/DataEmbedStore.ts')
|
|
6
|
+
|
|
7
|
+
describe('dotcom-ui-data-embed/src/client/index', () => {
|
|
8
|
+
describe('.init()', () => {
|
|
9
|
+
it('returns the value of an existing data embed store', () => {
|
|
10
|
+
const result = subject({ id: 'data-embed' })
|
|
11
|
+
expect(result).toBeInstanceOf(DataEmbedStore)
|
|
12
|
+
expect(loadDataEmbed).toBeCalledWith('data-embed')
|
|
13
|
+
expect(DataEmbedStore).toHaveBeenCalledTimes(1)
|
|
14
|
+
})
|
|
15
|
+
})
|
|
16
|
+
})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import subject from '../../client/loadDataEmbed'
|
|
6
|
+
const SCRIPT_ELEMENT_ID = 'data-embed'
|
|
7
|
+
|
|
8
|
+
describe('dotcom-ui-data-embed/src/client/loadDataEmbed', () => {
|
|
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(SCRIPT_ELEMENT_ID)
|
|
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(SCRIPT_ELEMENT_ID)
|
|
31
|
+
|
|
32
|
+
expect(result).toEqual({})
|
|
33
|
+
expect(Object.isFrozen(result)).toBe(true)
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
})
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import renderer from 'react-test-renderer'
|
|
2
|
+
import { DataEmbed as subject } from '../../components/DataEmbed'
|
|
3
|
+
|
|
4
|
+
const data = {
|
|
5
|
+
id: 'data-embed',
|
|
6
|
+
data: {
|
|
7
|
+
foo: 1,
|
|
8
|
+
bar: true,
|
|
9
|
+
baz: 'qux'
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('dotcom-ui-data-embed/src/components/dataEmbed', () => {
|
|
14
|
+
it('renders a script element containing data', () => {
|
|
15
|
+
const tree = renderer.create(subject(data))
|
|
16
|
+
expect(tree).toMatchSnapshot()
|
|
17
|
+
})
|
|
18
|
+
})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`dotcom-ui-data-embed/src/components/dataEmbed renders a script element containing data 1`] = `
|
|
4
|
+
<script
|
|
5
|
+
dangerouslySetInnerHTML={
|
|
6
|
+
Object {
|
|
7
|
+
"__html": "{\\"foo\\":1,\\"bar\\":true,\\"baz\\":\\"qux\\"}",
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
id="data-embed"
|
|
11
|
+
type="application/json"
|
|
12
|
+
/>
|
|
13
|
+
`;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default class DataEmbedStore {
|
|
2
|
+
private data
|
|
3
|
+
|
|
4
|
+
constructor(data) {
|
|
5
|
+
this.data = Object.freeze(data)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
get(key: string) {
|
|
9
|
+
return this.data.hasOwnProperty(key) ? this.data[key] : undefined
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
getAll() {
|
|
13
|
+
return this.data
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default function loadDataEmbed(id: string) {
|
|
2
|
+
const dataEmbedElement = document.getElementById(id)
|
|
3
|
+
|
|
4
|
+
let data = {}
|
|
5
|
+
|
|
6
|
+
if (dataEmbedElement) {
|
|
7
|
+
try {
|
|
8
|
+
data = JSON.parse(dataEmbedElement.innerHTML)
|
|
9
|
+
} catch (error) {
|
|
10
|
+
console.error('Data embed error', error) // eslint-disable-line no-console
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return Object.freeze(data)
|
|
15
|
+
}
|
package/src/index.ts
ADDED