Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit d66f304

Browse files
committed
fix(props): Simplified fix and corrected test code that was getting a false positive.
Reordered tests so that built-in React props come first. #26
1 parent aadcc16 commit d66f304

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

src/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export default function (CustomElement, opts) {
4141
static get propTypes() {
4242
return {
4343
children: React.PropTypes.any,
44+
style: React.PropTypes.any,
4445
};
4546
}
4647
componentDidMount() {
@@ -49,21 +50,19 @@ export default function (CustomElement, opts) {
4950
componentWillReceiveProps(props) {
5051
const node = ReactDOM.findDOMNode(this);
5152
Object.keys(props).forEach(name => {
52-
if (name === 'children') {
53+
if (name === 'children' || name === 'style') {
5354
return;
5455
}
5556

5657
if (name.indexOf('on') === 0) {
5758
syncEvent(node, name.substring(2), props[name]);
58-
} else if (name === 'style') {
59-
node.setAttribute('style', props[name]);
6059
} else {
6160
node[name] = props[name];
6261
}
6362
});
6463
}
6564
render() {
66-
return React.createElement(tagName, null, this.props.children);
65+
return React.createElement(tagName, { style: this.props.style }, this.props.children);
6766
}
6867
};
6968
}

test/unit/props.js

+35-27
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,59 @@ import React from 'react';
33
import ReactDOM from 'react-dom';
44

55
let x = 0;
6-
function createComponent(name, done) {
6+
function createComponentWithOpts(opts) {
77
return reactify(document.registerElement(`x-props-${x++}`, {
8-
prototype: Object.create(HTMLElement.prototype, {
9-
[name]: {
10-
get() {
11-
return 'test';
12-
},
13-
set(value) {
14-
if (done) {
15-
done(this, value);
16-
}
17-
},
18-
},
19-
}),
8+
prototype: Object.create(HTMLElement.prototype, opts),
209
}), { React, ReactDOM });
2110
}
11+
function createComponentWithProp(name, done) {
12+
return createComponentWithOpts({
13+
[name]: {
14+
get() {
15+
return 'test';
16+
},
17+
set(value) {
18+
if (done) {
19+
done(this, value);
20+
}
21+
},
22+
},
23+
});
24+
}
2225

2326
describe('props', () => {
27+
it('should set style (object)', () => {
28+
const Comp = createComponentWithOpts({});
29+
ReactDOM.render(<Comp style={{ backgroundColor: 'black', width: 1 }} />, window.fixture);
30+
const elem = window.fixture.firstChild;
31+
expect(elem.style.backgroundColor).to.equal('black');
32+
expect(elem.style.width).to.equal('1px');
33+
});
34+
35+
it('should set className', () => {
36+
const Comp = createComponentWithOpts({});
37+
ReactDOM.render(<Comp className="test" />, window.fixture);
38+
const elem = window.fixture.firstChild;
39+
expect(elem.getAttribute('class')).to.equal('test');
40+
});
41+
2442
it('should not set children', () => {
25-
const Comp = createComponent('children', () => { throw new Error('set children'); });
43+
const Comp = createComponentWithProp('children', () => { throw new Error('set children'); });
2644
ReactDOM.render(<Comp><div /></Comp>, window.fixture);
2745
});
2846

2947
it('should not set events', () => {
30-
const Comp = createComponent('oncustomevent', () => { throw new Error('set oncustomevent'); });
48+
const Comp = createComponentWithProp('oncustomevent', () => { throw new Error('set oncustomevent'); });
3149
ReactDOM.render(<Comp oncustomevent="test" />, window.fixture);
3250
});
3351

3452
it('should not set attributes', () => {
35-
const Comp = createComponent('test', elem => expect(elem.hasAttribute('test')).to.equal(false));
53+
const Comp = createComponentWithProp('test', elem => expect(elem.hasAttribute('test')).to.equal(false));
3654
ReactDOM.render(<Comp test="test" />, window.fixture);
3755
});
3856

39-
it('should set style as an attribute (object)', () => {
40-
const Comp = createComponent('style', elem => expect(elem.style.display).to.equal('block'));
41-
ReactDOM.render(<Comp style={{ display: 'block' }} />, window.fixture);
42-
});
43-
44-
it('should set style as an attribute (string)', () => {
45-
const Comp = createComponent('style', elem => expect(elem.style.display).to.equal('block'));
46-
ReactDOM.render(<Comp style="style: block" />, window.fixture);
47-
});
48-
4957
it('should set properties for anything else', () => {
50-
const Comp = createComponent('test', (elem, value) => expect(value).to.equal('test'));
58+
const Comp = createComponentWithProp('test', (elem, value) => expect(value).to.equal('test'));
5159
ReactDOM.render(<Comp test="test" />, window.fixture);
5260
});
5361
});

0 commit comments

Comments
 (0)