|
| 1 | +import { mount, shallow } from 'enzyme'; |
| 2 | +import 'jest'; |
| 3 | +import * as React from 'react'; |
| 4 | +import { TagMenu, ITagMenuProps } from '../../components/TagMenu'; |
| 5 | +import * as git from '../../git'; |
| 6 | +import { Logger } from '../../logger'; |
| 7 | +import { GitExtension } from '../../model'; |
| 8 | +import { IGitExtension } from '../../tokens'; |
| 9 | +import { listItemClass } from '../../style/BranchMenu'; |
| 10 | +import { |
| 11 | + mockedRequestAPI, |
| 12 | + defaultMockedResponses, |
| 13 | + DEFAULT_REPOSITORY_PATH |
| 14 | +} from '../utils'; |
| 15 | +import ClearIcon from '@material-ui/icons/Clear'; |
| 16 | +import { nullTranslator } from '@jupyterlab/translation'; |
| 17 | + |
| 18 | +jest.mock('../../git'); |
| 19 | +jest.mock('@jupyterlab/apputils'); |
| 20 | + |
| 21 | +const TAGS = [ |
| 22 | + { |
| 23 | + name: '1.0.0' |
| 24 | + }, |
| 25 | + { |
| 26 | + name: 'feature-1' |
| 27 | + }, |
| 28 | + { |
| 29 | + name: 'feature-2' |
| 30 | + }, |
| 31 | + { |
| 32 | + name: 'patch-007' |
| 33 | + } |
| 34 | +]; |
| 35 | + |
| 36 | +async function createModel() { |
| 37 | + const model = new GitExtension(); |
| 38 | + model.pathRepository = DEFAULT_REPOSITORY_PATH; |
| 39 | + |
| 40 | + await model.ready; |
| 41 | + return model; |
| 42 | +} |
| 43 | + |
| 44 | +describe('TagMenu', () => { |
| 45 | + let model: GitExtension; |
| 46 | + const trans = nullTranslator.load('jupyterlab_git'); |
| 47 | + |
| 48 | + beforeEach(async () => { |
| 49 | + jest.restoreAllMocks(); |
| 50 | + |
| 51 | + const mock = git as jest.Mocked<typeof git>; |
| 52 | + mock.requestAPI.mockImplementation( |
| 53 | + mockedRequestAPI({ |
| 54 | + responses: { |
| 55 | + ...defaultMockedResponses, |
| 56 | + 'tags/delete': { |
| 57 | + body: () => { |
| 58 | + return { code: 0 }; |
| 59 | + } |
| 60 | + }, |
| 61 | + checkout: { |
| 62 | + body: () => { |
| 63 | + return { |
| 64 | + code: 0 |
| 65 | + }; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + }) |
| 70 | + ); |
| 71 | + |
| 72 | + model = await createModel(); |
| 73 | + }); |
| 74 | + |
| 75 | + function createProps(props?: Partial<ITagMenuProps>): ITagMenuProps { |
| 76 | + return { |
| 77 | + branching: false, |
| 78 | + pastCommits: [], |
| 79 | + logger: new Logger(), |
| 80 | + model: model as IGitExtension, |
| 81 | + tagsList: TAGS.map(tag => tag.name), |
| 82 | + trans: trans, |
| 83 | + ...props |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + describe('constructor', () => { |
| 88 | + it('should return a new instance', () => { |
| 89 | + const menu = shallow(<TagMenu {...createProps()} />); |
| 90 | + expect(menu.instance()).toBeInstanceOf(TagMenu); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should set the default menu filter to an empty string', () => { |
| 94 | + const menu = shallow(<TagMenu {...createProps()} />); |
| 95 | + expect(menu.state('filter')).toEqual(''); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should set the default flag indicating whether to show a dialog to create a new tag to `false`', () => { |
| 99 | + const menu = shallow(<TagMenu {...createProps()} />); |
| 100 | + expect(menu.state('tagDialog')).toEqual(false); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('render', () => { |
| 105 | + it('should display placeholder text for the menu filter', () => { |
| 106 | + const component = shallow(<TagMenu {...createProps()} />); |
| 107 | + const node = component.find('input[type="text"]').first(); |
| 108 | + expect(node.prop('placeholder')).toEqual('Filter'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should set a `title` attribute on the input element to filter a tag menu', () => { |
| 112 | + const component = shallow(<TagMenu {...createProps()} />); |
| 113 | + const node = component.find('input[type="text"]').first(); |
| 114 | + expect(node.prop('title').length > 0).toEqual(true); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should display a button to clear the menu filter once a filter is provided', () => { |
| 118 | + const component = shallow(<TagMenu {...createProps()} />); |
| 119 | + component.setState({ |
| 120 | + filter: 'foo' |
| 121 | + }); |
| 122 | + const nodes = component.find(ClearIcon); |
| 123 | + expect(nodes.length).toEqual(1); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should set a `title` on the button to clear the menu filter', () => { |
| 127 | + const component = shallow(<TagMenu {...createProps()} />); |
| 128 | + component.setState({ |
| 129 | + filter: 'foo' |
| 130 | + }); |
| 131 | + const html = component.find(ClearIcon).first().html(); |
| 132 | + expect(html.includes('<title>')).toEqual(true); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should display a button to create a new tag', () => { |
| 136 | + const component = shallow(<TagMenu {...createProps()} />); |
| 137 | + const node = component.find('input[type="button"]').first(); |
| 138 | + expect(node.prop('value')).toEqual('New Tag'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should set a `title` attribute on the button to create a new tag', () => { |
| 142 | + const component = shallow(<TagMenu {...createProps()} />); |
| 143 | + const node = component.find('input[type="button"]').first(); |
| 144 | + expect(node.prop('title').length > 0).toEqual(true); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should not, by default, show a dialog to create a new tag', () => { |
| 148 | + const component = shallow(<TagMenu {...createProps()} />); |
| 149 | + const node = component.find('NewTagDialogBox').first(); |
| 150 | + expect(node.prop('open')).toEqual(false); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should show a dialog to create a new tag when the flag indicating whether to show the dialog is `true`', () => { |
| 154 | + const component = shallow(<TagMenu {...createProps()} />); |
| 155 | + component.setState({ |
| 156 | + tagDialog: true |
| 157 | + }); |
| 158 | + const node = component.find('NewTagDialogBox').first(); |
| 159 | + expect(node.prop('open')).toEqual(true); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('switch tag', () => { |
| 164 | + it('should not switch to a specified tag upon clicking its corresponding element when branching is disabled', () => { |
| 165 | + const spy = jest.spyOn(GitExtension.prototype, 'checkoutTag'); |
| 166 | + |
| 167 | + const component = mount(<TagMenu {...createProps()} />); |
| 168 | + const nodes = component.find( |
| 169 | + `.${listItemClass}[title*="${TAGS[1].name}"]` |
| 170 | + ); |
| 171 | + nodes.at(0).simulate('click'); |
| 172 | + |
| 173 | + expect(spy).toHaveBeenCalledTimes(0); |
| 174 | + spy.mockRestore(); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should switch to a specified tag upon clicking its corresponding element when branching is enabled', () => { |
| 178 | + const spy = jest.spyOn(GitExtension.prototype, 'checkoutTag'); |
| 179 | + |
| 180 | + const component = mount( |
| 181 | + <TagMenu {...createProps({ branching: true })} /> |
| 182 | + ); |
| 183 | + const nodes = component.find( |
| 184 | + `.${listItemClass}[title*="${TAGS[1].name}"]` |
| 185 | + ); |
| 186 | + nodes.at(0).simulate('click'); |
| 187 | + |
| 188 | + expect(spy).toHaveBeenCalledTimes(1); |
| 189 | + expect(spy).toHaveBeenCalledWith(TAGS[1].name); |
| 190 | + |
| 191 | + spy.mockRestore(); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + describe('create tag', () => { |
| 196 | + it('should not allow creating a new tag when branching is disabled', () => { |
| 197 | + const spy = jest.spyOn(GitExtension.prototype, 'setTag'); |
| 198 | + |
| 199 | + const component = shallow(<TagMenu {...createProps()} />); |
| 200 | + |
| 201 | + const node = component.find('input[type="button"]').first(); |
| 202 | + node.simulate('click'); |
| 203 | + |
| 204 | + expect(component.state('tagDialog')).toEqual(false); |
| 205 | + expect(spy).toHaveBeenCalledTimes(0); |
| 206 | + spy.mockRestore(); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should display a dialog to create a new tag when branching is enabled and the new tag button is clicked', () => { |
| 210 | + const spy = jest.spyOn(GitExtension.prototype, 'setTag'); |
| 211 | + |
| 212 | + const component = shallow( |
| 213 | + <TagMenu {...createProps({ branching: true })} /> |
| 214 | + ); |
| 215 | + |
| 216 | + const node = component.find('input[type="button"]').first(); |
| 217 | + node.simulate('click'); |
| 218 | + |
| 219 | + expect(component.state('tagDialog')).toEqual(true); |
| 220 | + expect(spy).toHaveBeenCalledTimes(0); |
| 221 | + spy.mockRestore(); |
| 222 | + }); |
| 223 | + }); |
| 224 | +}); |
0 commit comments