|
| 1 | +import React from 'react'; |
| 2 | +import { mount } from 'enzyme'; |
| 3 | +import { Notify } from 'components/Notify'; |
| 4 | +import { |
| 5 | + NOTIFICATION_TYPE_SUCCESS, |
| 6 | + NOTIFICATION_TYPE_INFO, |
| 7 | + NOTIFICATION_TYPE_ERROR, |
| 8 | +} from 'modules/Notifications'; |
| 9 | + |
| 10 | +describe('Notify', () => { |
| 11 | + const handleRemove = jest.fn(); |
| 12 | + const handleRemoveAll = jest.fn(); |
| 13 | + const notification1 = { |
| 14 | + id: 1, |
| 15 | + message: 'Notification 1!', |
| 16 | + type: NOTIFICATION_TYPE_SUCCESS, |
| 17 | + duration: 0, |
| 18 | + canDismiss: true, |
| 19 | + icon: <i className="fa fa-check" />, |
| 20 | + }; |
| 21 | + const notification2 = { |
| 22 | + id: 2, |
| 23 | + message: 'Notification 2!', |
| 24 | + type: NOTIFICATION_TYPE_INFO, |
| 25 | + duration: 0, |
| 26 | + canDismiss: true, |
| 27 | + icon: <i className="fa fa-check" />, |
| 28 | + }; |
| 29 | + const notification3 = { |
| 30 | + id: 3, |
| 31 | + message: 'Notification 3!', |
| 32 | + type: NOTIFICATION_TYPE_ERROR, |
| 33 | + duration: 0, |
| 34 | + canDismiss: true, |
| 35 | + icon: <i className="fa fa-check" />, |
| 36 | + }; |
| 37 | + const props = { |
| 38 | + notifications: [], |
| 39 | + remove: handleRemove, |
| 40 | + removeAll: handleRemoveAll, |
| 41 | + }; |
| 42 | + |
| 43 | + it('renders with required props', () => { |
| 44 | + const component = mount(<Notify {...props} />); |
| 45 | + expect(component).toMatchSnapshot(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('renders notifications', () => { |
| 49 | + const tProps = { |
| 50 | + ...props, |
| 51 | + notifications: [notification1, notification2], |
| 52 | + }; |
| 53 | + const component = mount(<Notify {...tProps} />); |
| 54 | + // TODO: Fix this, not sure whats going on here, but children's length |
| 55 | + // doesn't seem to be correct, even though the output in the |
| 56 | + // snapshot is correct. |
| 57 | + // expect(component.find(".wrapper").children()).toHaveLength(2); |
| 58 | + expect(component).toMatchSnapshot(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('renders custom notifications', () => { |
| 62 | + const MyCustomNotificationComponent = ({ |
| 63 | + message, |
| 64 | + canDismiss, |
| 65 | + id, |
| 66 | + handleDismiss, |
| 67 | + }) => { |
| 68 | + let styles = { |
| 69 | + margin: '5px 0', |
| 70 | + padding: '2px 5px', |
| 71 | + border: '1px solid #333', |
| 72 | + float: 'right', |
| 73 | + clear: 'right', |
| 74 | + width: '330px', |
| 75 | + boxSizing: 'border-box', |
| 76 | + }; |
| 77 | + if (canDismiss) { |
| 78 | + styles = Object.assign({}, styles, { cursor: 'pointer' }); |
| 79 | + } |
| 80 | + return ( |
| 81 | + <div |
| 82 | + onClick={() => { |
| 83 | + if (canDismiss) { |
| 84 | + handleDismiss(id); |
| 85 | + } |
| 86 | + }} |
| 87 | + style={styles} |
| 88 | + > |
| 89 | + {message} |
| 90 | + </div> |
| 91 | + ); |
| 92 | + }; |
| 93 | + const tProps = { |
| 94 | + ...props, |
| 95 | + notificationComponent: MyCustomNotificationComponent, |
| 96 | + notifications: [notification1], |
| 97 | + }; |
| 98 | + const component = mount(<Notify {...tProps} />); |
| 99 | + expect(component).toMatchSnapshot(); |
| 100 | + expect(component.contains(MyCustomNotificationComponent)).toEqual(true); |
| 101 | + }); |
| 102 | + |
| 103 | + it('renders with the correct className when customStyles is used', () => { |
| 104 | + const tProps = { |
| 105 | + ...props, |
| 106 | + customStyles: { |
| 107 | + containerCustomPosition: 'CustomPosition', |
| 108 | + }, |
| 109 | + position: 'CustomPosition', |
| 110 | + }; |
| 111 | + const component = mount(<Notify {...tProps} />); |
| 112 | + expect(component.find('.CustomPosition')).toHaveLength(1); |
| 113 | + expect(component).toMatchSnapshot(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('passes the correct props to each notification', () => { |
| 117 | + const tProps = { |
| 118 | + ...props, |
| 119 | + notifications: [notification1, notification2, notification3], |
| 120 | + }; |
| 121 | + const component = mount(<Notify {...tProps} />); |
| 122 | + expect(component.find({ ...notification1, isFirst: true })).toHaveLength(1); |
| 123 | + expect(component.find({ ...notification2, isFirst: false })).toHaveLength( |
| 124 | + 1 |
| 125 | + ); |
| 126 | + expect(component.find({ ...notification3, isFirst: false })).toHaveLength( |
| 127 | + 1 |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('calls removeAll with the correct arguments', () => { |
| 132 | + const tProps = { |
| 133 | + ...props, |
| 134 | + notifications: [notification1, notification2, notification3], |
| 135 | + }; |
| 136 | + const component = mount(<Notify {...tProps} />); |
| 137 | + component.find('.close-all').simulate('click'); |
| 138 | + expect(handleRemoveAll).toHaveBeenCalled(); |
| 139 | + component.setProps({ forceClose: true }); |
| 140 | + component.find('.close-all').simulate('click'); |
| 141 | + expect(handleRemoveAll).lastCalledWith(true); |
| 142 | + }); |
| 143 | + |
| 144 | + it('calls remove with the correct arguments', () => { |
| 145 | + const tProps = { ...props, notifications: [notification1] }; |
| 146 | + const component = mount(<Notify {...tProps} />); |
| 147 | + component |
| 148 | + .find('.close') |
| 149 | + .first() |
| 150 | + .simulate('click'); |
| 151 | + expect(handleRemove).toHaveBeenCalled(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('unmounts without error', () => { |
| 155 | + const component = mount(<Notify {...props} />); |
| 156 | + const instance = component.instance(); |
| 157 | + expect(instance.defaultNode).not.toEqual(null); |
| 158 | + component.unmount(); |
| 159 | + expect(instance.defaultNode).toEqual(null); |
| 160 | + expect(component).toMatchSnapshot(); |
| 161 | + }); |
| 162 | +}); |
0 commit comments