UNPKG

1.66 kBJavaScriptView Raw
1'use strict';
2
3var React = require('react');
4
5var tags = Object.keys(React.DOM);
6var originalValues = tags.map(function (tag) { return React.DOM[tag]; });
7var originalCreateElement = React.createElement;
8
9exports.mock = mock;
10function mock() {
11 for (var i = 0; i < tags.length; i++) {
12 React.DOM[tags[i]] = mockFor(tags[i]);
13 }
14 React.createElement = function() {
15 var args = Array.prototype.slice.call(arguments, 0);
16 var tag = args.shift();
17 return mockFor(tag).apply(null, args);
18 }
19 function mockFor(name) {
20 return function (attribs) {
21 var children = Array.prototype.slice.call(arguments, 1);
22 if (attribs) {
23 if ('class' in attribs) throw new Error('Cannot have an attribute named "class", perhaps you meant "className"');
24 if ('className' in attribs) {
25 attribs['class'] = attribs.className;
26 delete attribs.className;
27 }
28 if (attribs['class'] === '') delete attribs['class'];
29 Object.keys(attribs).forEach(function (key) {
30 if (attribs[key] === true) {
31 attribs[key] = key;
32 } else if (attribs[key] === false || attribs[key] === null || attribs[key] === undefined) {
33 delete attribs[key];
34 } else {
35 attribs[key] = attribs[key] + '';
36 }
37 });
38 }
39 return {
40 type: 'tag',
41 name: name,
42 attribs: attribs,
43 children: Array.isArray(children) ? children : (children ? [children] : [])
44 };
45 }
46 }
47}
48
49exports.reset = reset;
50function reset() {
51 for (var i = 0; i < tags.length; i++) {
52 React.DOM[tags[i]] = originalValues[i];
53 }
54 React.createElement = originalCreateElement;
55}