|
1 | 1 | const assert = require('assert'); |
2 | 2 | const React = require('react'); |
3 | | -const Preact = require('preact'); |
4 | 3 | const htmlToDOM = require('html-dom-parser'); |
5 | 4 | const domToReact = require('../lib/dom-to-react'); |
6 | 5 | const { data, render } = require('./helpers/'); |
@@ -122,7 +121,7 @@ describe('dom-to-react', () => { |
122 | 121 | ]); |
123 | 122 | }); |
124 | 123 |
|
125 | | - it("handles svg's with a viewBox", () => { |
| 124 | + it('converts SVG element with viewBox attribute', () => { |
126 | 125 | const html = data.svg.simple; |
127 | 126 | const reactElement = domToReact( |
128 | 127 | htmlToDOM(html, { lowerCaseAttributeNames: false }) |
@@ -151,47 +150,61 @@ describe('dom-to-react', () => { |
151 | 150 | ); |
152 | 151 | }); |
153 | 152 |
|
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 }); |
157 | 159 |
|
158 | | - assert.deepEqual(preactElement, Preact.createElement('p', {}, 'foo')); |
| 160 | + assert.deepEqual(preactElement, Preact.createElement('p', {}, 'foo')); |
| 161 | + }); |
159 | 162 | }); |
160 | 163 |
|
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 | + }); |
163 | 172 |
|
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 | + } |
179 | 192 | } |
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 | + ]); |
181 | 207 | }); |
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 | | - ]); |
195 | 208 | }); |
196 | 209 |
|
197 | 210 | describe('when React <16', () => { |
|
0 commit comments