react-datocms 3.0.11 → 3.0.14

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.
@@ -0,0 +1,331 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`StructuredText simple dast /2 with custom mark rules renders the document 1`] = `
4
+ <StructuredText
5
+ customMarkRules={
6
+ Array [
7
+ Object {
8
+ "appliable": [Function],
9
+ "apply": [Function],
10
+ },
11
+ ]
12
+ }
13
+ data={
14
+ Object {
15
+ "document": Object {
16
+ "children": Array [
17
+ Object {
18
+ "children": Array [
19
+ Object {
20
+ "type": "span",
21
+ "value": "This
22
+ is a ",
23
+ },
24
+ Object {
25
+ "marks": Array [
26
+ "strong",
27
+ ],
28
+ "type": "span",
29
+ "value": "title",
30
+ },
31
+ ],
32
+ "level": 1,
33
+ "type": "heading",
34
+ },
35
+ ],
36
+ "type": "root",
37
+ },
38
+ "schema": "dast",
39
+ }
40
+ }
41
+ >
42
+ <h1
43
+ key="t-0"
44
+ >
45
+ This
46
+ <br
47
+ key="t-0-br-0"
48
+ />
49
+ is a
50
+ <b
51
+ key="t-1"
52
+ >
53
+ title
54
+ </b>
55
+ </h1>
56
+ </StructuredText>
57
+ `;
58
+
59
+ exports[`StructuredText simple dast /2 with default rules renders the document 1`] = `
60
+ <StructuredText
61
+ data={
62
+ Object {
63
+ "document": Object {
64
+ "children": Array [
65
+ Object {
66
+ "children": Array [
67
+ Object {
68
+ "type": "span",
69
+ "value": "This
70
+ is a ",
71
+ },
72
+ Object {
73
+ "marks": Array [
74
+ "strong",
75
+ ],
76
+ "type": "span",
77
+ "value": "title",
78
+ },
79
+ ],
80
+ "level": 1,
81
+ "type": "heading",
82
+ },
83
+ ],
84
+ "type": "root",
85
+ },
86
+ "schema": "dast",
87
+ }
88
+ }
89
+ >
90
+ <h1
91
+ key="t-0"
92
+ >
93
+ This
94
+ <br
95
+ key="t-0-br-0"
96
+ />
97
+ is a
98
+ <strong
99
+ key="t-1"
100
+ >
101
+ title
102
+ </strong>
103
+ </h1>
104
+ </StructuredText>
105
+ `;
106
+
107
+ exports[`StructuredText simple dast with no links/blocks with custom rules renders the document 1`] = `
108
+ <StructuredText
109
+ customNodeRules={
110
+ Array [
111
+ Object {
112
+ "appliable": [Function],
113
+ "apply": [Function],
114
+ },
115
+ ]
116
+ }
117
+ data={
118
+ Object {
119
+ "value": Object {
120
+ "document": Object {
121
+ "children": Array [
122
+ Object {
123
+ "children": Array [
124
+ Object {
125
+ "type": "span",
126
+ "value": "This
127
+ is a ",
128
+ },
129
+ Object {
130
+ "marks": Array [
131
+ "strong",
132
+ ],
133
+ "type": "span",
134
+ "value": "title",
135
+ },
136
+ ],
137
+ "level": 1,
138
+ "type": "heading",
139
+ },
140
+ ],
141
+ "type": "root",
142
+ },
143
+ "schema": "dast",
144
+ },
145
+ }
146
+ }
147
+ renderText={[Function]}
148
+ >
149
+ <h2
150
+ key="t-0"
151
+ >
152
+ That
153
+ <br
154
+ key="t-0-br-0"
155
+ />
156
+ is a
157
+ <strong
158
+ key="t-1"
159
+ >
160
+ title
161
+ </strong>
162
+ </h2>
163
+ </StructuredText>
164
+ `;
165
+
166
+ exports[`StructuredText simple dast with no links/blocks with default rules renders the document 1`] = `
167
+ <StructuredText
168
+ data={
169
+ Object {
170
+ "value": Object {
171
+ "document": Object {
172
+ "children": Array [
173
+ Object {
174
+ "children": Array [
175
+ Object {
176
+ "type": "span",
177
+ "value": "This
178
+ is a ",
179
+ },
180
+ Object {
181
+ "marks": Array [
182
+ "strong",
183
+ ],
184
+ "type": "span",
185
+ "value": "title",
186
+ },
187
+ ],
188
+ "level": 1,
189
+ "type": "heading",
190
+ },
191
+ ],
192
+ "type": "root",
193
+ },
194
+ "schema": "dast",
195
+ },
196
+ }
197
+ }
198
+ >
199
+ <h1
200
+ key="t-0"
201
+ >
202
+ This
203
+ <br
204
+ key="t-0-br-0"
205
+ />
206
+ is a
207
+ <strong
208
+ key="t-1"
209
+ >
210
+ title
211
+ </strong>
212
+ </h1>
213
+ </StructuredText>
214
+ `;
215
+
216
+ exports[`StructuredText with links/blocks with default rules renders the document 1`] = `
217
+ <StructuredText
218
+ data={
219
+ Object {
220
+ "blocks": Array [
221
+ Object {
222
+ "__typename": "QuoteRecord",
223
+ "author": "Mark Smith",
224
+ "id": "456",
225
+ "quote": "Foo bar.",
226
+ },
227
+ ],
228
+ "links": Array [
229
+ Object {
230
+ "__typename": "DocPageRecord",
231
+ "id": "123",
232
+ "slug": "how-to-code",
233
+ "title": "How to code",
234
+ },
235
+ ],
236
+ "value": Object {
237
+ "document": Object {
238
+ "children": Array [
239
+ Object {
240
+ "children": Array [
241
+ Object {
242
+ "type": "span",
243
+ "value": "This is a",
244
+ },
245
+ Object {
246
+ "marks": Array [
247
+ "highlight",
248
+ ],
249
+ "type": "span",
250
+ "value": "title",
251
+ },
252
+ Object {
253
+ "item": "123",
254
+ "type": "inlineItem",
255
+ },
256
+ Object {
257
+ "children": Array [
258
+ Object {
259
+ "type": "span",
260
+ "value": "here!",
261
+ },
262
+ ],
263
+ "item": "123",
264
+ "meta": Array [
265
+ Object {
266
+ "id": "target",
267
+ "value": "_blank",
268
+ },
269
+ ],
270
+ "type": "itemLink",
271
+ },
272
+ ],
273
+ "level": 1,
274
+ "type": "heading",
275
+ },
276
+ Object {
277
+ "item": "456",
278
+ "type": "block",
279
+ },
280
+ ],
281
+ "type": "root",
282
+ },
283
+ "schema": "dast",
284
+ },
285
+ }
286
+ }
287
+ renderBlock={[Function]}
288
+ renderInlineRecord={[Function]}
289
+ renderLinkToRecord={[Function]}
290
+ >
291
+ <h1
292
+ key="t-0"
293
+ >
294
+ This is a
295
+ <mark
296
+ key="t-1"
297
+ >
298
+ title
299
+ </mark>
300
+ <a
301
+ href="/docs/how-to-code"
302
+ key="t-2"
303
+ >
304
+ How to code
305
+ </a>
306
+ <a
307
+ href="/docs/how-to-code"
308
+ key="t-3"
309
+ target="_blank"
310
+ >
311
+ here!
312
+ </a>
313
+ </h1>
314
+ <figure
315
+ key="t-1"
316
+ >
317
+ <blockquote>
318
+ Foo bar.
319
+ </blockquote>
320
+ <figcaption>
321
+ Mark Smith
322
+ </figcaption>
323
+ </figure>
324
+ </StructuredText>
325
+ `;
326
+
327
+ exports[`StructuredText with no value renders null 1`] = `
328
+ <StructuredText
329
+ data={null}
330
+ />
331
+ `;
@@ -0,0 +1,271 @@
1
+ import { mount, shallow, render } from 'enzyme';
2
+ import * as React from 'react';
3
+ import {
4
+ StructuredText,
5
+ StructuredTextGraphQlResponse,
6
+ StructuredTextDocument,
7
+ RenderError,
8
+ renderNodeRule,
9
+ } from '../index';
10
+ import { isHeading } from 'datocms-structured-text-utils';
11
+ import { renderMarkRule } from 'datocms-structured-text-generic-html-renderer';
12
+
13
+ describe('StructuredText', () => {
14
+ describe('with no value', () => {
15
+ it('renders null', () => {
16
+ const wrapper = mount(<StructuredText data={null} />);
17
+ expect(wrapper).toMatchSnapshot();
18
+ });
19
+ });
20
+
21
+ describe('simple dast /2', () => {
22
+ const structuredText: StructuredTextDocument = {
23
+ schema: 'dast',
24
+ document: {
25
+ type: 'root',
26
+ children: [
27
+ {
28
+ type: 'heading',
29
+ level: 1,
30
+ children: [
31
+ {
32
+ type: 'span',
33
+ value: 'This\nis a ',
34
+ },
35
+ {
36
+ type: 'span',
37
+ marks: ['strong'],
38
+ value: 'title',
39
+ },
40
+ ],
41
+ },
42
+ ],
43
+ },
44
+ };
45
+
46
+ describe('with default rules', () => {
47
+ it('renders the document', () => {
48
+ const wrapper = mount(<StructuredText data={structuredText} />);
49
+ expect(wrapper).toMatchSnapshot();
50
+ });
51
+ });
52
+
53
+ describe('with custom mark rules', () => {
54
+ it('renders the document', () => {
55
+ const wrapper = mount(
56
+ <StructuredText
57
+ data={structuredText}
58
+ customMarkRules={[
59
+ renderMarkRule('strong', ({ children, key }) => (
60
+ <b key={key}>{children}</b>
61
+ )),
62
+ ]}
63
+ />,
64
+ );
65
+ expect(wrapper).toMatchSnapshot();
66
+ });
67
+ });
68
+ });
69
+
70
+ describe('simple dast with no links/blocks', () => {
71
+ const structuredText: StructuredTextGraphQlResponse = {
72
+ value: {
73
+ schema: 'dast',
74
+ document: {
75
+ type: 'root',
76
+ children: [
77
+ {
78
+ type: 'heading',
79
+ level: 1,
80
+ children: [
81
+ {
82
+ type: 'span',
83
+ value: 'This\nis a ',
84
+ },
85
+ {
86
+ type: 'span',
87
+ marks: ['strong'],
88
+ value: 'title',
89
+ },
90
+ ],
91
+ },
92
+ ],
93
+ },
94
+ },
95
+ };
96
+
97
+ describe('with default rules', () => {
98
+ it('renders the document', () => {
99
+ const wrapper = mount(<StructuredText data={structuredText} />);
100
+ expect(wrapper).toMatchSnapshot();
101
+ });
102
+ });
103
+
104
+ describe('with custom rules', () => {
105
+ it('renders the document', () => {
106
+ const wrapper = mount(
107
+ <StructuredText
108
+ data={structuredText}
109
+ renderText={(text, key) => {
110
+ return (
111
+ <React.Fragment key={key}>
112
+ {text.replace(/This/, 'That')}
113
+ </React.Fragment>
114
+ );
115
+ }}
116
+ customNodeRules={[
117
+ renderNodeRule(
118
+ isHeading,
119
+ ({ adapter: { renderNode }, node, children, key }) => {
120
+ return renderNode(`h${node.level + 1}`, { key }, children);
121
+ },
122
+ ),
123
+ ]}
124
+ />,
125
+ );
126
+ expect(wrapper).toMatchSnapshot();
127
+ });
128
+ });
129
+ });
130
+
131
+ describe('with links/blocks', () => {
132
+ type QuoteRecord = {
133
+ id: string;
134
+ __typename: 'QuoteRecord';
135
+ quote: string;
136
+ author: string;
137
+ };
138
+
139
+ type DocPageRecord = {
140
+ id: string;
141
+ __typename: 'DocPageRecord';
142
+ slug: string;
143
+ title: string;
144
+ };
145
+
146
+ const structuredText: StructuredTextGraphQlResponse<
147
+ QuoteRecord, DocPageRecord
148
+ > = {
149
+ value: {
150
+ schema: 'dast',
151
+ document: {
152
+ type: 'root',
153
+ children: [
154
+ {
155
+ type: 'heading',
156
+ level: 1,
157
+ children: [
158
+ {
159
+ type: 'span',
160
+ value: 'This is a',
161
+ },
162
+ {
163
+ type: 'span',
164
+ marks: ['highlight'],
165
+ value: 'title',
166
+ },
167
+ {
168
+ type: 'inlineItem',
169
+ item: '123',
170
+ },
171
+ {
172
+ type: 'itemLink',
173
+ item: '123',
174
+ meta: [{ id: 'target', value: '_blank' }],
175
+ children: [{ type: 'span', value: 'here!' }],
176
+ },
177
+ ],
178
+ },
179
+ {
180
+ type: 'block',
181
+ item: '456',
182
+ },
183
+ ],
184
+ },
185
+ },
186
+ blocks: [
187
+ {
188
+ id: '456',
189
+ __typename: 'QuoteRecord',
190
+ quote: 'Foo bar.',
191
+ author: 'Mark Smith',
192
+ },
193
+ ],
194
+ links: [
195
+ {
196
+ id: '123',
197
+ __typename: 'DocPageRecord',
198
+ title: 'How to code',
199
+ slug: 'how-to-code',
200
+ },
201
+ ],
202
+ };
203
+
204
+ describe('with default rules', () => {
205
+ it('renders the document', () => {
206
+ const wrapper = mount(
207
+ <StructuredText
208
+ data={structuredText}
209
+ renderInlineRecord={({ record }) => {
210
+ switch (record.__typename) {
211
+ case 'DocPageRecord':
212
+ return <a href={`/docs/${record.slug}`}>{record.title}</a>;
213
+ default:
214
+ return null;
215
+ }
216
+ }}
217
+ renderLinkToRecord={({ record, children, transformedMeta }) => {
218
+ switch (record.__typename) {
219
+ case 'DocPageRecord':
220
+ return (
221
+ <a {...transformedMeta} href={`/docs/${record.slug}`}>
222
+ {children}
223
+ </a>
224
+ );
225
+ default:
226
+ return null;
227
+ }
228
+ }}
229
+ renderBlock={({ record }) => {
230
+ switch (record.__typename) {
231
+ case 'QuoteRecord':
232
+ return (
233
+ <figure>
234
+ <blockquote>{record.quote}</blockquote>
235
+ <figcaption>{record.author}</figcaption>
236
+ </figure>
237
+ );
238
+ default:
239
+ return null;
240
+ }
241
+ }}
242
+ />,
243
+ );
244
+ expect(wrapper).toMatchSnapshot();
245
+ });
246
+ });
247
+
248
+ describe('with missing renderInlineRecord prop', () => {
249
+ it('raises an error', () => {
250
+ expect(() => {
251
+ shallow(<StructuredText data={structuredText} />);
252
+ }).toThrow(RenderError);
253
+ });
254
+ });
255
+
256
+ describe('with missing record', () => {
257
+ it('raises an error', () => {
258
+ expect(() => {
259
+ shallow(
260
+ <StructuredText
261
+ data={{ ...structuredText, links: [] }}
262
+ renderInlineRecord={() => {
263
+ return null;
264
+ }}
265
+ />,
266
+ );
267
+ }).toThrow(RenderError);
268
+ });
269
+ });
270
+ });
271
+ });