Skip to content

Commit c281aea

Browse files
author
Chris Sloop
committedApr 29, 2021
feat: {wip} update slate.js adapter
1 parent 6d28c01 commit c281aea

File tree

1 file changed

+120
-156
lines changed

1 file changed

+120
-156
lines changed
 

‎packages/contentful-slatejs-adapter/src/__test__/contentful-to-slatejs-adapter.test.ts

+120-156
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ describe('adapters', () => {
3939
testAdapters(
4040
'document with block',
4141
contentful.document(contentful.block(Contentful.BLOCKS.PARAGRAPH, contentful.text(''))),
42-
slate.document(slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.text(slate.leaf('')))),
42+
[
43+
{
44+
type: Contentful.BLOCKS.PARAGRAPH,
45+
children: [
46+
{ text: '' }
47+
]
48+
}
49+
],
4350
);
4451

4552
testAdapters(
@@ -50,19 +57,27 @@ describe('adapters', () => {
5057
contentful.inline(Contentful.INLINES.HYPERLINK),
5158
),
5259
),
53-
slate.document(
54-
slate.block(
55-
Contentful.BLOCKS.PARAGRAPH,
56-
false,
57-
slate.inline(Contentful.INLINES.HYPERLINK, false),
58-
),
59-
),
60+
[
61+
{
62+
type: Contentful.BLOCKS.PARAGRAPH,
63+
children: [
64+
{ type: Contentful.INLINES.HYPERLINK }
65+
]
66+
}
67+
],
6068
);
6169

6270
testAdapters(
6371
'paragraph with text',
6472
contentful.document(contentful.block(Contentful.BLOCKS.PARAGRAPH, contentful.text('hi'))),
65-
slate.document(slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.text(slate.leaf('hi')))),
73+
[
74+
{
75+
type: Contentful.BLOCKS.PARAGRAPH,
76+
children: [
77+
{ text: 'hi' }
78+
]
79+
}
80+
],
6681
);
6782

6883
testAdapters(
@@ -74,24 +89,26 @@ describe('adapters', () => {
7489
contentful.text('is', contentful.mark('bold')),
7590
),
7691
),
77-
slate.document(
78-
slate.block(
79-
Contentful.BLOCKS.PARAGRAPH,
80-
false,
81-
slate.text(slate.leaf('this')),
82-
slate.text(slate.leaf('is', slate.mark('bold'))),
83-
),
84-
),
92+
[
93+
{
94+
type: Contentful.BLOCKS.PARAGRAPH,
95+
children: [
96+
{ text: 'this' },
97+
{ text: 'is', bold: true },
98+
],
99+
},
100+
],
85101
);
86102

87103
it('adds a default value to marks if undefined', () => {
88-
const slateDoc = slate.document(
89-
slate.block(
90-
Contentful.BLOCKS.PARAGRAPH,
91-
false,
92-
slate.text({ marks: undefined, object: 'leaf', text: 'Hi' }),
93-
),
94-
);
104+
const slateDoc = [
105+
{
106+
type: Contentful.BLOCKS.PARAGRAPH,
107+
children: [
108+
{ text: 'Hi' }
109+
]
110+
}
111+
];
95112
const ctflDoc = toContentfulDocument({
96113
document: slateDoc,
97114
});
@@ -117,15 +134,16 @@ describe('adapters', () => {
117134
contentful.text('huge', contentful.mark('bold'), contentful.mark('italic')),
118135
),
119136
),
120-
slate.document(
121-
slate.block(
122-
Contentful.BLOCKS.PARAGRAPH,
123-
false,
124-
slate.text(slate.leaf('this')),
125-
slate.text(slate.leaf('is', slate.mark('bold'))),
126-
slate.text(slate.leaf('huge', slate.mark('bold'), slate.mark('italic'))),
127-
),
128-
),
137+
[
138+
{
139+
type: Contentful.BLOCKS.PARAGRAPH,
140+
children: [
141+
{ text: 'this' },
142+
{ text: 'is', bold: true },
143+
{ text: 'huge', bold: true, italic: true },
144+
],
145+
},
146+
],
129147
);
130148

131149
testAdapters(
@@ -134,26 +152,28 @@ describe('adapters', () => {
134152
contentful.block(
135153
Contentful.BLOCKS.PARAGRAPH,
136154
contentful.text('this is a test', contentful.mark('bold')),
137-
contentful.text(Contentful.BLOCKS.PARAGRAPH, contentful.mark('underline')),
155+
contentful.text('paragraph', contentful.mark('underline')),
138156
),
139157
contentful.block(
140158
Contentful.BLOCKS.QUOTE,
141159
contentful.block(Contentful.BLOCKS.PARAGRAPH, contentful.text('this is it')),
142160
),
143161
),
144-
slate.document(
145-
slate.block(
146-
Contentful.BLOCKS.PARAGRAPH,
147-
false,
148-
slate.text(slate.leaf('this is a test', slate.mark('bold'))),
149-
slate.text(slate.leaf(Contentful.BLOCKS.PARAGRAPH, slate.mark('underline'))),
150-
),
151-
slate.block(
152-
Contentful.BLOCKS.QUOTE,
153-
false,
154-
slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.text(slate.leaf('this is it'))),
155-
),
156-
),
162+
[
163+
{
164+
type: Contentful.BLOCKS.PARAGRAPH,
165+
children: [
166+
{ text: 'this is a test', bold: true },
167+
{ type: 'paragraph', underline: true },
168+
],
169+
},
170+
{
171+
type: Contentful.BLOCKS.PARAGRAPH,
172+
children: [
173+
{ text: 'this is it' },
174+
],
175+
},
176+
],
157177
);
158178
});
159179

@@ -171,19 +191,12 @@ describe('adapters', () => {
171191
},
172192
],
173193
},
174-
{
175-
object: 'document',
176-
data: {},
177-
nodes: [
178-
{
179-
object: 'block',
180-
type: Contentful.BLOCKS.PARAGRAPH,
181-
isVoid: false,
182-
data: { a: 1 },
183-
nodes: [],
184-
},
185-
],
186-
},
194+
[
195+
{
196+
type: Contentful.BLOCKS.PARAGRAPH,
197+
data: { a: 1 },
198+
},
199+
],
187200
);
188201

189202
testAdapters(
@@ -205,29 +218,18 @@ describe('adapters', () => {
205218
},
206219
],
207220
},
208-
{
209-
object: 'document',
210-
data: {},
211-
nodes: [
212-
{
213-
object: 'block',
214-
type: Contentful.BLOCKS.PARAGRAPH,
215-
isVoid: false,
216-
data: { a: 1 },
217-
nodes: [
218-
{
219-
object: 'inline',
220-
type: Contentful.INLINES.HYPERLINK,
221-
isVoid: false,
222-
data: {
223-
a: 2,
224-
},
225-
nodes: [],
226-
},
227-
],
228-
},
229-
],
230-
},
221+
[
222+
{
223+
type: Contentful.BLOCKS.PARAGRAPH,
224+
data: { a: 1 },
225+
children: [
226+
{
227+
type: Contentful.INLINES.HYPERLINK,
228+
data: { a: 2 },
229+
},
230+
],
231+
},
232+
],
231233
);
232234

233235
testAdapters(
@@ -255,40 +257,23 @@ describe('adapters', () => {
255257
},
256258
],
257259
},
258-
{
259-
object: 'document',
260-
data: {},
261-
nodes: [
262-
{
263-
object: 'block',
264-
type: Contentful.BLOCKS.PARAGRAPH,
265-
isVoid: false,
266-
data: { a: 1 },
267-
nodes: [
268-
{
269-
object: 'inline',
270-
type: Contentful.INLINES.HYPERLINK,
271-
isVoid: false,
272-
data: {
273-
a: 2,
274-
},
275-
nodes: [],
276-
},
277-
{
278-
object: 'text',
279-
data: { a: 3 },
280-
leaves: [
281-
{
282-
object: 'leaf',
283-
marks: [],
284-
text: 'YO',
285-
},
286-
],
287-
},
288-
],
289-
},
290-
],
291-
},
260+
[
261+
{
262+
type: Contentful.BLOCKS.PARAGRAPH,
263+
data: { a: 1 },
264+
children: [
265+
{
266+
type: Contentful.INLINES.HYPERLINK,
267+
data: { a: 2 },
268+
children: [
269+
{
270+
text: 'YO',
271+
data: { a: 3 },
272+
]
273+
},
274+
],
275+
},
276+
],
292277
);
293278
});
294279

@@ -306,20 +291,15 @@ describe('adapters', () => {
306291
},
307292
],
308293
},
309-
{
310-
object: 'document',
311-
data: {},
312-
nodes: [
313-
{
314-
object: 'block',
315-
type: Contentful.BLOCKS.EMBEDDED_ENTRY,
316-
isVoid: true,
317-
data: { a: 1 },
318-
nodes: [],
319-
},
320-
],
321-
},
294+
[
295+
{
296+
type: Contentful.BLOCKS.EMBEDDED_ENTRY,
297+
data: { a: 1 },
298+
isVoid: true,
299+
},
300+
],
322301
);
302+
323303
test('removes empty text nodes from void nodes content', () => {
324304
const contentfulDoc: Contentful.Document = {
325305
nodeType: Contentful.BLOCKS.DOCUMENT,
@@ -333,32 +313,16 @@ describe('adapters', () => {
333313
],
334314
};
335315

336-
const slateDoc: Slate.Document = {
337-
object: 'document',
338-
type: 'document',
339-
data: {},
340-
nodes: [
341-
{
342-
object: 'block',
343-
type: Contentful.BLOCKS.EMBEDDED_ENTRY,
344-
isVoid: true,
345-
data: { a: 1 },
346-
nodes: [
347-
{
348-
object: 'text',
349-
data: {},
350-
leaves: [
351-
{
352-
object: 'leaf',
353-
marks: [],
354-
text: '',
355-
},
356-
],
357-
},
358-
],
359-
},
360-
],
361-
};
316+
const slateDoc: Slate.Document = [
317+
{
318+
type: Contentful.BLOCKS.EMBEDDED_ENTRY,
319+
data: { a: 1 },
320+
isVoid: true,
321+
children: [
322+
{ text: '' },
323+
]
324+
},
325+
];
362326

363327
const actualContentfulDoc = toContentfulDocument({
364328
document: slateDoc,

0 commit comments

Comments
 (0)
Please sign in to comment.