index.test.js (1905B)
1 import {shallow, mount} from 'enzyme'; 2 import {ErrorNotice} from '../'; 3 4 describe('Error Notice', () => { 5 // 1. Snapshot testing 6 it('renders correctly', () => { 7 const onButtonClickMock = jest.fn(); 8 const wrapper = shallow(<ErrorNotice errorMessages={['Some Error Message']} discardAllErrorMessages={onButtonClickMock} />); 9 expect(wrapper).toMatchSnapshot(); 10 }); 11 12 it('has always one p tag', () => { 13 const props = { 14 errorMessages: [] 15 }, 16 ErrorNoticeComponent = mount(<ErrorNotice {...props} />); 17 expect(ErrorNoticeComponent.find('p').length).toBe(1); 18 }); 19 20 // 2. props check: proper rendering of error message 21 it('should show the message properly within p tag', () => { 22 const props = { 23 errorMessages: ['Test Error'] 24 }, 25 ErrorNoticeComponent = mount(<ErrorNotice {...props} />); 26 expect(ErrorNoticeComponent.find('p').text()).toBe('An error occurred:Test Error'); 27 }); 28 29 // 3. prop data type testing 30 it('check the type of props', () => { 31 const props = { 32 errorMessages: [], 33 discardAllErrorMessages: jest.fn() 34 }, 35 ErrorNoticeComponent = mount(<ErrorNotice {...props} />); 36 expect(Array.isArray(ErrorNoticeComponent.prop('errorMessages'))).toBe(true); 37 expect(typeof ErrorNoticeComponent.prop('discardAllErrorMessages')).toBe('function'); 38 }); 39 40 // 4. Event 41 it('click close to call discardAllErrorMessages', () => { 42 const props = { 43 errorMessages: [], 44 discardAllErrorMessages: jest.fn() 45 }, 46 ErrorNoticeComponent = mount(<ErrorNotice {...props} />).find('button'); 47 ErrorNoticeComponent.simulate('click'); 48 expect(props.discardAllErrorMessages).toHaveBeenCalled(); 49 50 }) 51 52 53 });