|
| 1 | +import React from 'react'; |
| 2 | +import {shallow} from 'enzyme'; |
| 3 | + |
| 4 | +import CreateIssueModal from './create_issue'; |
| 5 | + |
| 6 | +jest.mock('utils/user_utils', () => ({ |
| 7 | + getErrorMessage: jest.fn(() => 'Error occurred'), |
| 8 | +})); |
| 9 | + |
| 10 | +describe('CreateIssueModal', () => { |
| 11 | + const defaultProps = { |
| 12 | + close: jest.fn(), |
| 13 | + create: jest.fn(() => Promise.resolve({})), |
| 14 | + post: null, |
| 15 | + theme: { |
| 16 | + centerChannelColor: '#000', |
| 17 | + centerChannelBg: '#fff', |
| 18 | + }, |
| 19 | + visible: true, |
| 20 | + }; |
| 21 | + |
| 22 | + it('should render correctly with default props', () => { |
| 23 | + const wrapper = shallow(<CreateIssueModal {...defaultProps}/>); |
| 24 | + expect(wrapper).toMatchSnapshot(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should call close prop when handleClose is called', () => { |
| 28 | + const wrapper = shallow(<CreateIssueModal {...defaultProps}/>); |
| 29 | + wrapper.instance().handleClose(); |
| 30 | + expect(defaultProps.close).toHaveBeenCalled(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should call create prop when form is submitted with valid data', async () => { |
| 34 | + const wrapper = shallow(<CreateIssueModal {...defaultProps}/>); |
| 35 | + wrapper.setState({issueTitle: 'Test Issue'}); |
| 36 | + |
| 37 | + await wrapper.instance().handleCreate({preventDefault: jest.fn()}); |
| 38 | + expect(defaultProps.create).toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should display error message when create returns an error', async () => { |
| 42 | + const mockCreateFunction = jest.fn().mockResolvedValue({error: {message: 'Some error'}}); |
| 43 | + const errorProps = { |
| 44 | + ...defaultProps, |
| 45 | + create: mockCreateFunction, |
| 46 | + }; |
| 47 | + |
| 48 | + const wrapper = shallow(<CreateIssueModal {...errorProps}/>); |
| 49 | + wrapper.setState({issueTitle: 'Test Issue'}); |
| 50 | + |
| 51 | + await wrapper.instance().handleCreate({preventDefault: jest.fn()}); |
| 52 | + wrapper.update(); |
| 53 | + |
| 54 | + expect(wrapper.find('.help-text.error-text').text()).toEqual('Error occurred'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should show validation error when issueTitle is empty', async () => { |
| 58 | + const wrapper = shallow(<CreateIssueModal {...defaultProps}/>); |
| 59 | + wrapper.setState({issueTitle: ''}); |
| 60 | + |
| 61 | + await wrapper.instance().handleCreate({preventDefault: jest.fn()}); |
| 62 | + expect(wrapper.state('issueTitleValid')).toBe(false); |
| 63 | + expect(wrapper.state('showErrors')).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should update repo state when handleRepoChange is called', () => { |
| 67 | + const wrapper = shallow(<CreateIssueModal {...defaultProps}/>); |
| 68 | + const repo = {name: 'repo-name'}; |
| 69 | + |
| 70 | + wrapper.instance().handleRepoChange(repo); |
| 71 | + expect(wrapper.state('repo')).toEqual(repo); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should update labels state when handleLabelsChange is called', () => { |
| 75 | + const wrapper = shallow(<CreateIssueModal {...defaultProps}/>); |
| 76 | + const labels = ['label1', 'label2']; |
| 77 | + |
| 78 | + wrapper.instance().handleLabelsChange(labels); |
| 79 | + expect(wrapper.state('labels')).toEqual(labels); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should update assignees state when handleAssigneesChange is called', () => { |
| 83 | + const wrapper = shallow(<CreateIssueModal {...defaultProps}/>); |
| 84 | + const assignees = ['user1', 'user2']; |
| 85 | + |
| 86 | + wrapper.instance().handleAssigneesChange(assignees); |
| 87 | + expect(wrapper.state('assignees')).toEqual(assignees); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should set issueDescription state when post prop is updated', () => { |
| 91 | + const wrapper = shallow(<CreateIssueModal {...defaultProps}/>); |
| 92 | + const post = {message: 'test post'}; |
| 93 | + wrapper.setProps({post}); |
| 94 | + |
| 95 | + expect(wrapper.state('issueDescription')).toEqual(post.message); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should not display attribute selectors when repo does not have push permissions', () => { |
| 99 | + const wrapper = shallow(<CreateIssueModal {...defaultProps}/>); |
| 100 | + wrapper.setState({repo: {name: 'repo-name', permissions: {push: false}}}); |
| 101 | + |
| 102 | + expect(wrapper.instance().renderIssueAttributeSelectors()).toBeNull(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should display attribute selectors when repo has push permissions', () => { |
| 106 | + const wrapper = shallow(<CreateIssueModal {...defaultProps}/>); |
| 107 | + wrapper.setState({repo: {name: 'repo-name', permissions: {push: true}}}); |
| 108 | + |
| 109 | + const selectors = wrapper.instance().renderIssueAttributeSelectors(); |
| 110 | + expect(selectors).not.toBeNull(); |
| 111 | + }); |
| 112 | +}); |
0 commit comments