Skip to content

Commit 452f6be

Browse files
test(dom-to-react): tidy tests and add case for single node replace
This gets coverage to 100%.
1 parent 77a108b commit 452f6be

File tree

1 file changed

+50
-37
lines changed

1 file changed

+50
-37
lines changed

test/dom-to-react.js

+50-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const assert = require('assert');
22
const React = require('react');
3-
const Preact = require('preact');
43
const htmlToDOM = require('html-dom-parser');
54
const domToReact = require('../lib/dom-to-react');
65
const { data, render } = require('./helpers/');
@@ -122,7 +121,7 @@ describe('dom-to-react', () => {
122121
]);
123122
});
124123

125-
it("handles svg's with a viewBox", () => {
124+
it('converts SVG element with viewBox attribute', () => {
126125
const html = data.svg.simple;
127126
const reactElement = domToReact(
128127
htmlToDOM(html, { lowerCaseAttributeNames: false })
@@ -151,47 +150,61 @@ describe('dom-to-react', () => {
151150
);
152151
});
153152

154-
it('handles using a custom component library', () => {
155-
const html = data.html.single;
156-
const preactElement = domToReact(htmlToDOM(html), { library: Preact });
153+
describe('library', () => {
154+
const Preact = require('preact');
155+
156+
it('converts with Preact instead of React', () => {
157+
const html = data.html.single;
158+
const preactElement = domToReact(htmlToDOM(html), { library: Preact });
157159

158-
assert.deepEqual(preactElement, Preact.createElement('p', {}, 'foo'));
160+
assert.deepEqual(preactElement, Preact.createElement('p', {}, 'foo'));
161+
});
159162
});
160163

161-
it('does not modify keys for replacement if it has one', () => {
162-
const html = [data.html.single, data.html.customElement].join('');
164+
describe('replace', () => {
165+
it("does not set key if there's 1 node", () => {
166+
const replaceElement = React.createElement('p');
167+
const reactElement = domToReact(htmlToDOM(data.html.single), {
168+
replace: () => replaceElement
169+
});
170+
assert.deepEqual(reactElement, replaceElement);
171+
});
163172

164-
const reactElements = domToReact(htmlToDOM(html), {
165-
replace: node => {
166-
if (node.name === 'p') {
167-
return React.createElement('p', {}, 'replaced foo');
168-
}
169-
if (node.name === 'custom-button') {
170-
return React.createElement(
171-
'custom-button',
172-
{
173-
key: 'myKey',
174-
class: 'myClass',
175-
'custom-attribute': 'replaced value'
176-
},
177-
null
178-
);
173+
it("does not modify keys if it's already set", () => {
174+
const html = [data.html.single, data.html.customElement].join('');
175+
176+
const reactElements = domToReact(htmlToDOM(html), {
177+
replace: node => {
178+
if (node.name === 'p') {
179+
return React.createElement('p', {}, 'replaced foo');
180+
}
181+
if (node.name === 'custom-button') {
182+
return React.createElement(
183+
'custom-button',
184+
{
185+
key: 'myKey',
186+
class: 'myClass',
187+
'custom-attribute': 'replaced value'
188+
},
189+
null
190+
);
191+
}
179192
}
180-
}
193+
});
194+
195+
assert.deepEqual(reactElements, [
196+
React.createElement('p', { key: 0 }, 'replaced foo'),
197+
React.createElement(
198+
'custom-button',
199+
{
200+
key: 'myKey',
201+
class: 'myClass',
202+
'custom-attribute': 'replaced value'
203+
},
204+
null
205+
)
206+
]);
181207
});
182-
183-
assert.deepEqual(reactElements, [
184-
React.createElement('p', { key: 0 }, 'replaced foo'),
185-
React.createElement(
186-
'custom-button',
187-
{
188-
key: 'myKey',
189-
class: 'myClass',
190-
'custom-attribute': 'replaced value'
191-
},
192-
null
193-
)
194-
]);
195208
});
196209

197210
describe('when React <16', () => {

0 commit comments

Comments
 (0)