reactjs - ReactComponent is not rendering properly -
i have react component simple to-do app
where i've edit button on each todo list, whenever click edit button, function set state of todo new property called "edit" true whenever todo has edit=true, i'm displaying edit form,the functionality works expected, but, i'm not sure way test using jest + enzyme
import react 'react'; import { shallow, mount } 'enzyme'; import renderer 'react-test-renderer'; import app '../components/app'; import task '../components/task'; import edittaskform '../components/edittaskform'; describe(task, () => { // added allow lifecycles, bydefault enzyme wont execute lifecylce methods const options = { lifecycleexperimental: true, disablelifecyclemethods: false }; const removetask = jest.fn(); const markcomplete = jest.fn(); const setcurrenttaskeditable = jest.fn(); const updatetask = jest.fn(); const index = "task1"; const task = { task: 'everyones favorite white task. cut size need , ship it.', status: 'in_progress' }; const appcomponent = shallow(<app/>, options); const currenttaskkey = object.keys(appcomponent.state('tasks'))[0]; let currenttask = appcomponent.state('tasks')[object.keys(appcomponent.state('tasks'))[0]]; const component = mount(<task key={currenttaskkey} index={currenttaskkey} task={currenttask } removefromtask={removetask} markasdone={markcomplete} setcurrenttaskeditable={setcurrenttaskeditable} updatetask={updatetask}/>); it('renders , matches snapshot', () => { const component = renderer.create(<task key={index} index={index} task={ task } removefromtask={removetask} markasdone={markcomplete} setcurrenttaskeditable={setcurrenttaskeditable} updatetask={updatetask}/>); const tree = component.tojson(); expect(tree).tomatchsnapshot(); }); it('should not contain edittaskform subcomponent', () => { expect(component.find(edittaskform)).tohavelength(0); }); it('should contain edit, markcomplet , remove task buttons', () => { expect(component.find('button')).tohavelength(3); }); it('should display editform when click edit button', () => { component.find('button.edittask').simulate('click'); expect(setcurrenttaskeditable).tobecalled(); appcomponent.instance().setcurrenttaskeditable(index); // state updated expect expect(component.find(edittaskform)).tohavelength(1); // here component not updating }); }); source code: https://github.com/loganathan-s/todoappwithreact/commits/develop
Comments
Post a Comment