Guest User

Untitled

a guest
Dec 17th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import React from 'react';
  2. import Item from './Item';
  3. class List extends React.Component {
  4.  
  5. constructor() {
  6. super();
  7. this.test = "Hello";
  8. this.state = {
  9. "items" : []
  10. }
  11. }
  12. render(){
  13. return (
  14. <div>
  15. {/*This is the bit that's playing up.*/}
  16. {this.state.items.map(object => {
  17. return <Item key={object.title}/>
  18. })}
  19.  
  20. {/*Below is used for testing purposes only at the moment*/}
  21. <input type="text" defaultValue={ this.test } />
  22. <button onClick = {() => this.add({"title":Math.random()})}>Test</button>
  23. </div>
  24. )
  25. }
  26.  
  27. add(_item) {
  28. let items = this.state.items;
  29.  
  30. items.forEach((item) => {
  31. if(item.title === _item.title){
  32. throw new Error('Item already exists');
  33. }
  34. })
  35.  
  36. items.push(_item);
  37. this.setState({"items" : items});
  38. }
  39.  
  40. }
  41.  
  42. export default List;
  43.  
  44. import React from 'react';
  45. import { shallow, mount } from 'enzyme';
  46. import { expect } from 'chai';
  47. import List from './List';
  48. import Item from './Item';
  49.  
  50. describe('List', () => {
  51. let wrapper, instance;
  52.  
  53. beforeEach(() =>{
  54. wrapper = shallow(<List/>);
  55. instance = wrapper.instance();
  56. });
  57.  
  58. it('Displays an item for each object in list', () =>{
  59. instance.add({"title":"Cut the grass"});
  60. instance.add({"title":"Mow the Lawn"});
  61. expect(wrapper.find(Item).length).to.equal(2);
  62. })
  63. })
  64.  
  65. render(){
  66. return (
  67. <div>
  68. <Item/>
  69.  
  70. <input type="text" defaultValue={ this.test } />
  71. <button onClick = {() => this.add({"title":Math.random()})}>Test</button>
  72. </div>
  73. )
  74. }
Add Comment
Please, Sign In to add comment