Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jest.unmock('../comp.js');
import React from 'react';
import ReactTestUtils from 'react-addons-test-utils';
import Comp from '../comp';
describe('Comp test', () => {
it('should pass', () => {
const type = 'some type';
const items = [{ text: 'first' }];
const comp = ReactTestUtils.renderIntoDocument(
<Comp
type={ type }
items={ items }
/>
);
const item = ReactTestUtils.scryRenderedDOMComponentsWithClass(
comp,
'list_item'
)[0];
expect(comp.props).toEqual({ type, items })
expect(item.props).not.toBeDefined();
expect(item.type).toBe(type);
expect(item.text).not.toBeDefined();
// ? if I set value as a number it will replace it
expect(item.value).toBe(0);
// I don't have a direct access to component, only by randomly generated key
expect(Object.keys(item).length).toBe(1);
expect(Object.keys(item)[0].indexOf('__reactInternalInstance')).toBe(0);
// I have to get manualy the element
const key = Object.keys(item).filter(key => (
key.indexOf('__reactInternalInstance') === 0)
)[0];
expect(item[key]._currentElement.props).toEqual({
className: 'list_item',
type: 'some type',
text: 'first',
value: 'asd',
children: 'first',
});
});
});
import React, { Component } from 'react';
class Comp extends Component {
constructor(props) {
super(props);
this.handleUpdate = this.handleUpdate.bind(this);
this.state = {
items: props.items || [],
};
}
handleUpdate() {
this.setState({
items: this.state.items.concat({
text: `item ${this.state.items.length + 1}`,
}),
});
}
render() {
return (
<div>
<ul>
{ this.state.items.map((item, index) => (
<li
key={ `item_${index}` }
className="list_item"
type={ this.props.type }
text={ item.text }
value={ 'asd' }
>
{ item.text }
</li>
)) }
</ul>
<button onClick={ this.handleUpdate }>add</button>
</div>
);
}
}
export default Comp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.