Last active
March 19, 2017 15:03
-
-
Save eezhal92/01e3b12233015a1415c317c8d6279999 to your computer and use it in GitHub Desktop.
TodoForm.spec.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { shallow } from 'enzyme'; | |
import TodoForm from './TodoForm'; | |
it('should not call onSubmit callback when the input less than 5 characters', () => { | |
const onSubmitSpy = jest.fn(); | |
const preventDefaultSpy = jest.fn(); | |
const resetSpy = jest.fn(); | |
const subject = shallow( | |
<TodoForm onSubmit={onSubmitSpy} /> | |
); | |
subject.find('.todo-form').simulate('submit', { | |
preventDefault: preventDefaultSpy, | |
target: { | |
todo: { | |
value: 'some' // simulasi input hanya 4 karakter | |
}, | |
reset: resetSpy, | |
} | |
}); | |
// ketika di submit: | |
// - preventDefault harus terpanggil | |
// - onSubmit tidak boleh terpanggil | |
// - reset tidak boleh terpanggil | |
expect(preventDefaultSpy).toHaveBeenCalled(); | |
expect(onSubmitSpy).not.toHaveBeenCalled(); | |
expect(resetSpy).not.toHaveBeenCalled(); | |
}); | |
it('should call onSubmit callback when the input equals or more than 5 characters', () => { | |
const onSubmitSpy = jest.fn(); | |
const preventDefaultSpy = jest.fn(); | |
const resetSpy = jest.fn(); | |
const subject = shallow( | |
<TodoForm onSubmit={onSubmitSpy} /> | |
); | |
subject.find('.todo-form').simulate('submit', { | |
preventDefault: preventDefaultSpy, | |
target: { | |
todo: { | |
value: 'somes' | |
}, | |
reset: resetSpy, | |
} | |
}); | |
// ketika di submit: | |
// - preventDefault harus terpanggil | |
// - onSubmit harus terpanggil dengan argument 'somes' | |
// - reset harus terpanggil | |
expect(preventDefaultSpy).toHaveBeenCalled(); | |
expect(onSubmitSpy).toHaveBeenCalledWith('somes'); | |
expect(resetSpy).toHaveBeenCalled(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment