Skip to content

Commit cb2e3ea

Browse files
committed
Refactor tests
1 parent 66eb6db commit cb2e3ea

File tree

4 files changed

+192
-186
lines changed

4 files changed

+192
-186
lines changed

src/fixtures.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@ import { serializeNodeToHtmlString } from './utils';
66
import toDOM from './index';
77

88
describe('fixtures', () => {
9-
const FIXTURES_PATH = path.join(__dirname, '__fixtures__');
10-
const fixturePaths = glob.sync(path.join(FIXTURES_PATH, '**/*/'));
9+
const root = path.join(__dirname, '__fixtures__');
10+
const fixturePaths = glob.sync(path.join(root, '**/*/'));
11+
1112
fixturePaths.forEach((fixturePath) => {
12-
const fixture = path.relative(FIXTURES_PATH, fixturePath);
13+
const fixture = path.relative(root, fixturePath);
1314
const fixtureInput = path.join(fixturePath, 'index.json');
1415
const fixtureOutput = path.join(fixturePath, 'index.html');
1516

1617
test(fixture, () => {
1718
const fixtureData = JSON.parse(fs.readFileSync(fixtureInput));
1819
const parsedActual = serializeNodeToHtmlString(toDOM(fixtureData));
20+
1921
let parsedExpected;
22+
2023
try {
2124
parsedExpected = fs.readFileSync(fixtureOutput).toString();
2225
} catch (e) {
2326
fs.writeFileSync(fixtureOutput, parsedActual);
2427
return;
2528
}
29+
2630
expect(parsedActual).toEqual(parsedExpected);
2731
});
2832
});

src/index.js

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,39 @@
11
import info from 'property-information';
22

3-
const ROOT_NODE = 'root';
4-
const TEXT_NODE = 'text';
5-
const ELEMENT_NODE = 'element';
6-
const DOCUMENT_TYPE_NODE = 'doctype';
7-
const COMMENT_NODE = 'comment';
8-
93
function transform(node, options = {}) {
104
switch (node.type) {
11-
case ROOT_NODE:
5+
case 'root':
126
return root(node, options);
13-
case TEXT_NODE:
7+
case 'text':
148
return text(node, options);
15-
case ELEMENT_NODE:
9+
case 'element':
1610
return element(node, options);
17-
case DOCUMENT_TYPE_NODE:
11+
case 'doctype':
1812
return doctype(node, options);
19-
case COMMENT_NODE:
13+
case 'comment':
2014
return comment(node, options);
2115
default:
2216
return element(node, options);
2317
}
2418
}
2519

26-
/**
27-
* Transform a document
28-
*/
20+
// Create a document.
2921
function root(node, options = {}) {
30-
const {
31-
fragment,
32-
namespace: optionsNamespace,
33-
} = options;
22+
const { fragment, namespace: optionsNamespace } = options;
3423
const { children = [] } = node;
3524
const { length: childrenLength } = children;
3625

3726
let namespace = optionsNamespace;
3827
let rootIsDocument = childrenLength === 0;
3928

4029
for (let i = 0; i < childrenLength; i += 1) {
41-
const {
42-
tagName,
43-
properties: {
44-
xmlns,
45-
} = {},
46-
} = children[i];
30+
const { tagName, properties: { xmlns } = {} } = children[i];
31+
4732
if (tagName === 'html') {
48-
// If we have a root HTML node, we don't need to render as a fragment
33+
// If we have a root HTML node, we dont need to render as a fragment.
4934
rootIsDocument = true;
50-
// Take namespace of first child
35+
36+
// Take namespace of the first child.
5137
if (typeof optionsNamespace === 'undefined') {
5238
if (xmlns) {
5339
namespace = xmlns;
@@ -58,8 +44,9 @@ function root(node, options = {}) {
5844
}
5945
}
6046

61-
// The root node will be a Document, DocumentFragment, or HTMLElement
47+
// The root node will be a Document, DocumentFragment, or HTMLElement.
6248
let el;
49+
6350
if (rootIsDocument) {
6451
el = document.implementation.createDocument(namespace, '', null);
6552
} else if (fragment) {
@@ -68,10 +55,12 @@ function root(node, options = {}) {
6855
el = document.createElement('html');
6956
}
7057

71-
// Transform children
58+
// Transform children.
7259
const childOptions = Object.assign({ fragment, namespace }, options);
60+
7361
for (let i = 0; i < childrenLength; i += 1) {
7462
const childEl = transform(children[i], childOptions);
63+
7564
if (childEl) {
7665
el.appendChild(childEl);
7766
}
@@ -80,9 +69,7 @@ function root(node, options = {}) {
8069
return el;
8170
}
8271

83-
/**
84-
* Transform a DOCTYPE
85-
*/
72+
// Create a `doctype`.
8673
function doctype(node) {
8774
return document.implementation.createDocumentType(
8875
node.name || 'html',
@@ -91,35 +78,31 @@ function doctype(node) {
9178
);
9279
}
9380

94-
/**
95-
* Transform text node
96-
*/
81+
// Create a `text`.
9782
function text(node) {
9883
return document.createTextNode(node.value);
9984
}
10085

101-
/**
102-
* Transform a comment node
103-
*/
86+
// Create a `comment`.
10487
function comment(node) {
10588
return document.createComment(node.value);
10689
}
10790

108-
/**
109-
* Transform an element
110-
*/
91+
// Create an `element`.
11192
function element(node, options = {}) {
11293
const { namespace } = options;
11394
const { tagName, properties, children = [] } = node;
11495
const el = typeof namespace !== 'undefined'
11596
? document.createElementNS(namespace, tagName)
11697
: document.createElement(tagName);
11798

118-
// Add HTML attributes
99+
// Add HTML attributes.
119100
const props = Object.keys(properties);
120101
const { length } = props;
102+
121103
for (let i = 0; i < length; i += 1) {
122104
const key = props[i];
105+
123106
const {
124107
attribute,
125108
property,
@@ -128,22 +111,18 @@ function element(node, options = {}) {
128111
boolean,
129112
booleanish,
130113
overloadedBoolean,
131-
// number,
132-
// defined,
114+
// `number`,
115+
// `defined`,
133116
commaSeparated,
134-
spaceSeparated,
135-
// commaOrSpaceSeparated,
136-
} = info.find(info.html, key) || {
137-
attribute: key,
138-
property: key,
139-
};
117+
// `spaceSeparated`,
118+
// `commaOrSpaceSeparated`,
119+
} = info.find(info.html, key) || { attribute: key, property: key };
140120

141121
let value = properties[key];
122+
142123
if (Array.isArray(value)) {
143124
if (commaSeparated) {
144125
value = value.join(', ');
145-
} else if (spaceSeparated) {
146-
value = value.join(' ');
147126
} else {
148127
value = value.join(' ');
149128
}
@@ -153,6 +132,7 @@ function element(node, options = {}) {
153132
if (mustUseProperty) {
154133
el[property] = value;
155134
}
135+
156136
if (boolean || (overloadedBoolean && typeof value === 'boolean')) {
157137
if (value) {
158138
el.setAttribute(attribute, '');
@@ -170,14 +150,17 @@ function element(node, options = {}) {
170150
if (!mustUseAttribute && property) {
171151
el[property] = value;
172152
}
173-
// Otherwise silently ignore
153+
154+
// Otherwise silently ignore.
174155
}
175156
}
176157

177-
// Transform children
158+
// Transform children.
178159
const { length: childrenLength } = children;
160+
179161
for (let i = 0; i < childrenLength; i += 1) {
180162
const childEl = transform(children[i], options);
163+
181164
if (childEl) {
182165
el.appendChild(childEl);
183166
}

0 commit comments

Comments
 (0)