-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml2datocms.test.js
More file actions
310 lines (270 loc) · 11.2 KB
/
html2datocms.test.js
File metadata and controls
310 lines (270 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const { HTML2DatoCMS } = require('./html2datocms.js')
function addRootNode (structuredTextNode) {
return {
schema: 'dast',
document: {
type: 'root',
children: structuredTextNode
}
}
}
test('converts heading tags to structured text', async () => {
const html = '<h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3><h4>Heading 4</h4><h5>Heading 5</h5><h6>Heading 6</h6>'
const structuredText = await new HTML2DatoCMS().html2block(html)
const expectedChildren = []
for (let level = 1; level <= 6; level++) {
expectedChildren.push({ type: 'heading', level, children: [{ marks: [], type: 'span', value: `Heading ${level}` }] })
}
expect(structuredText).toStrictEqual(addRootNode(expectedChildren))
})
test('converts link tags to structured text', async () => {
const html = '<a href="https://www.example.com/">Example Link</a>'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([{
type: 'paragraph',
children: [
{
type: 'link',
url: 'https://www.example.com/',
children: [{ type: 'span', marks: [], value: 'Example Link' }],
meta: [{ id: 'target', value: '_blank' }]
}
]
}])
)
})
test('converts bold, italic, and underline tags to structured text', async () => {
const html = '<p><strong>Bold Text</strong><i>Italic Text</i><u>Underlined Text</u></p>'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([{
type: 'paragraph',
children: [
{ type: 'span', marks: ['strong'], value: 'Bold Text' },
{ type: 'span', marks: ['emphasis'], value: 'Italic Text' },
{ type: 'span', marks: ['underline'], value: 'Underlined Text' }
]
}])
)
})
test('converts list tags to structured text', async () => {
const html = '<ul><li>Item 1</li><li>Item 2</li></ul><ol><li>Item 1</li><li>Item 2</li></ol>'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([
{
type: 'list',
style: 'bulleted',
children: [
{ type: 'listItem', children: [{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Item 1' }] }] },
{ type: 'listItem', children: [{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Item 2' }] }] }
]
},
{
type: 'list',
style: 'numbered',
children: [
{ type: 'listItem', children: [{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Item 1' }] }] },
{ type: 'listItem', children: [{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Item 2' }] }] }
]
}
])
)
})
test('converts <code> tag to structured text', async () => {
const html = '<code>console.log("Hello, World!");</code>'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([{ type: 'code', code: 'console.log("Hello, World!");' }])
)
})
test('converts <hr> tag to structured text', async () => {
const html = '<hr>'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([{ type: 'thematicBreak' }])
)
})
test('converts <aside> tag to structured text', async () => {
const html = '<p><aside>Example aside text</aside></p>'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([
{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Example aside text' }] }
])
)
})
test('converts <blockquote> tag to structured text', async () => {
const html = '<blockquote>Example blockquote text</blockquote>'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([
{ type: 'blockquote', children: [{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Example blockquote text' }] }] }
])
)
})
test('handles unknown tag and generates error block', async () => {
const html = '<unknown>Unknown content</unknown>'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toMatchObject(
addRootNode([{
type: 'paragraph',
children: [
{ type: 'span', marks: ['strong'], value: expect.stringContaining('!!!!!!!! Error !!!!!!!! Unknown node: UNKNOWN') }
]
}])
)
})
test('converts <img> tag to structured text', async () => {
const clientMock = { uploads: { createFromUrl: jest.fn() } }
clientMock.uploads.createFromUrl.mockResolvedValue({ id: 'mock_image_id' })
const html = '<img src="https://example.com/image.jpg" alt="Example image">'
const sampleBlockId = '123456'
const structuredText = await new HTML2DatoCMS(clientMock, sampleBlockId).html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([
{
type: 'block',
item: {
type: 'item',
attributes: { image: { upload_id: 'mock_image_id' } },
relationships: { item_type: { data: { id: sampleBlockId, type: 'item_type' } } }
}
}
])
)
expect(clientMock.uploads.createFromUrl).toHaveBeenCalledWith({
url: 'https://example.com/image.jpg',
skipCreationIfAlreadyExists: true
})
})
test('handles <img> tag without \'src\' attribute and generates error block', async () => {
const html = '<img alt="Example image">'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toMatchObject(
addRootNode([{
type: 'paragraph',
children: [
{ type: 'span', marks: ['strong'], value: expect.stringContaining('!!!!!!!! Error !!!!!!!! Image upload failed: ...') }
]
}])
)
})
test('handles <img> tag with broken \'src\' attribute and generates error block', async () => {
const html = '<img src="https://example.com/image.jpg" alt="Example image">'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toMatchObject(
addRootNode([{
type: 'paragraph',
children: [
{ type: 'span', marks: ['strong'], value: expect.stringContaining('!!!!!!!! Error !!!!!!!! Image upload failed: https://example.com/image.jpg...') }
]
}])
)
})
test('converts normal text to structured text', async () => {
const html = 'Example text'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Example text' }] }])
)
})
test('removes empty tags', async () => {
const html = '<p></p><p>Example text</p><p class="test"><p></p></p><h1></h1><span></span><b></b><blockquote></blockquote>'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Example text' }] }])
)
})
test('ignores <div>, <main>, <br>', async () => {
const html = '<div>Example text</div> <main>Example text</main><br>'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Example text Example text' }] }])
)
})
test('handles empty string', async () => {
const html = ''
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([])
)
})
test('handles empty new line', async () => {
const html = '\n\nExample text\n\n'
const structuredText = await new HTML2DatoCMS().html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Example text' }] }])
)
})
test('boolToDatoCMS converts string boolean values to boolean', () => {
expect(new HTML2DatoCMS().boolToDatoCMS('true')).toBe(true)
expect(new HTML2DatoCMS().boolToDatoCMS('false')).toBe(false)
})
test('uploadToDatoCMS uploads image and returns upload id', async () => {
const clientMock = { uploads: { createFromUrl: jest.fn() } }
clientMock.uploads.createFromUrl.mockResolvedValue({ id: 'mock_image_id' })
const imageUrl = 'https://example.com/image.jpg'
const result = await new HTML2DatoCMS(clientMock).uploadToDatoCMS(imageUrl)
expect(result).toStrictEqual({ upload_id: 'mock_image_id' })
expect(clientMock.uploads.createFromUrl).toHaveBeenCalledWith({
url: imageUrl,
skipCreationIfAlreadyExists: true
})
})
test('uploadToDatoCMS handles upload errors', async () => {
const clientMock = { uploads: { createFromUrl: jest.fn() } }
clientMock.uploads.createFromUrl.mockRejectedValue(new Error('Upload failed'))
const result = await new HTML2DatoCMS(clientMock).uploadToDatoCMS('')
expect(result).toStrictEqual(null)
expect(clientMock.uploads.createFromUrl).toHaveBeenCalledWith({
url: '',
skipCreationIfAlreadyExists: true
})
})
test('fetchRecords fetches records from DatoCMS', async () => {
const clientMock = { items: { list: jest.fn() } }
const mockRecords = [{ id: 'record1' }, { id: 'record2' }]
clientMock.items.list.mockResolvedValue(mockRecords)
const type = 'sample_type'
const field = 'sample_field'
const value = 'sample_value'
const records = await new HTML2DatoCMS(clientMock).fetchRecords(type, field, value)
expect(records).toStrictEqual(mockRecords)
expect(clientMock.items.list).toHaveBeenCalledWith({
filter: {
type,
fields: { [field]: { eq: value } }
},
page: { limit: 100 },
version: 'current'
})
})
test('using same instance of HTML2DatoCMS for multiple conversions', async () => {
const clientMock = { uploads: { createFromUrl: jest.fn() } }
clientMock.uploads.createFromUrl.mockResolvedValue({ id: 'mock_image_id' })
const sampleBlockId = '123456'
const html2DatoCMS = new HTML2DatoCMS(clientMock, sampleBlockId)
const html = '<p>Example text</p>'
const structuredText = await html2DatoCMS.html2block(html)
expect(structuredText).toStrictEqual(
addRootNode([{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Example text' }] }])
)
const html2 = '<p>Example text 2</p>'
const structuredText2 = await html2DatoCMS.html2block(html2)
expect(structuredText2).toStrictEqual(
addRootNode([{ type: 'paragraph', children: [{ marks: [], type: 'span', value: 'Example text 2' }] }])
)
expect(clientMock.uploads.createFromUrl).toHaveBeenCalledTimes(0)
const html3 = '<img src="https://example.com/image.jpg" alt="Example image">'
const structuredText3 = await html2DatoCMS.html2block(html3)
expect(structuredText3).toStrictEqual(
addRootNode([{ type: 'block', item: { type: 'item', attributes: { image: { upload_id: 'mock_image_id' } }, relationships: { item_type: { data: { id: sampleBlockId, type: 'item_type' } } } } }])
)
expect(clientMock.uploads.createFromUrl).toHaveBeenCalledTimes(1)
const img = 'https://example.com/image.jpg'
const uploadId = await html2DatoCMS.uploadToDatoCMS(img)
expect(uploadId).toStrictEqual({ upload_id: 'mock_image_id' })
expect(clientMock.uploads.createFromUrl).toHaveBeenCalledTimes(2)
})