debian-mirror-gitlab/spec/frontend/ide/components/new_dropdown/modal_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

420 lines
12 KiB
JavaScript
Raw Normal View History

2022-08-13 15:12:31 +05:30
import { GlButton, GlModal } from '@gitlab/ui';
import { nextTick } from 'vue';
2022-11-25 23:54:43 +05:30
import { createAlert } from '~/flash';
2022-08-13 15:12:31 +05:30
import Modal from '~/ide/components/new_dropdown/modal.vue';
2021-03-11 19:13:27 +05:30
import { createStore } from '~/ide/stores';
2022-08-13 15:12:31 +05:30
import { stubComponent } from 'helpers/stub_component';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { createEntriesFromPaths } from '../../helpers';
2020-05-24 23:13:21 +05:30
jest.mock('~/flash');
2022-08-13 15:12:31 +05:30
const NEW_NAME = 'babar';
2020-05-24 23:13:21 +05:30
describe('new file modal component', () => {
2022-08-13 15:12:31 +05:30
const showModal = jest.fn();
const toggleModal = jest.fn();
let store;
let wrapper;
const findForm = () => wrapper.findByTestId('file-name-form');
const findGlModal = () => wrapper.findComponent(GlModal);
const findInput = () => wrapper.findByTestId('file-name-field');
const findTemplateButtons = () => wrapper.findAllComponents(GlButton);
const findTemplateButtonsModel = () =>
findTemplateButtons().wrappers.map((x) => ({
text: x.text(),
variant: x.props('variant'),
category: x.props('category'),
}));
const open = (type, path) => {
// TODO: This component can not be passed props
// We have to interact with the open() method?
wrapper.vm.open(type, path);
};
const triggerSubmitForm = () => {
findForm().trigger('submit');
};
const triggerSubmitModal = () => {
findGlModal().vm.$emit('primary');
};
const triggerCancel = () => {
findGlModal().vm.$emit('cancel');
};
const mountComponent = () => {
const GlModalStub = stubComponent(GlModal);
jest.spyOn(GlModalStub.methods, 'show').mockImplementation(showModal);
jest.spyOn(GlModalStub.methods, 'toggle').mockImplementation(toggleModal);
wrapper = shallowMountExtended(Modal, {
store,
stubs: {
GlModal: GlModalStub,
},
// We need to attach to document for "focus" to work
attachTo: document.body,
});
};
beforeEach(() => {
store = createStore();
Object.assign(
store.state.entries,
createEntriesFromPaths([
'README.md',
'src',
'src/deleted.js',
'src/parent_dir',
'src/parent_dir/foo.js',
]),
);
Object.assign(store.state.entries['src/deleted.js'], { deleted: true });
jest.spyOn(store, 'dispatch').mockImplementation();
});
2020-05-24 23:13:21 +05:30
afterEach(() => {
2022-08-13 15:12:31 +05:30
store = null;
wrapper.destroy();
document.body.innerHTML = '';
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
describe('default', () => {
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2022-08-13 15:12:31 +05:30
mountComponent();
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
// Not necessarily needed, but used to ensure that nothing extra is happening after the tick
2022-04-04 11:22:00 +05:30
await nextTick();
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
it('renders modal', () => {
expect(findGlModal().props()).toMatchObject({
actionCancel: {
attributes: [{ variant: 'default' }],
text: 'Cancel',
},
actionPrimary: {
attributes: [{ variant: 'confirm' }],
text: 'Create file',
},
actionSecondary: null,
size: 'lg',
modalId: 'ide-new-entry',
title: 'Create new file',
});
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
it('renders name label', () => {
expect(wrapper.find('label').text()).toBe('Name');
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
it('renders template buttons', () => {
const actual = findTemplateButtonsModel();
expect(actual.length).toBeGreaterThan(0);
expect(actual).toEqual(
store.getters['fileTemplates/templateTypes'].map((template) => ({
category: 'secondary',
text: template.name,
variant: 'dashed',
})),
);
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
// These negative ".not.toHaveBeenCalled" assertions complement the positive "toHaveBeenCalled"
// assertions that show up later in this spec. Without these, we're not guaranteed the "act"
// actually caused the change in behavior.
it('does not dispatch actions by default', () => {
expect(store.dispatch).not.toHaveBeenCalled();
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
it('does not trigger modal by default', () => {
expect(showModal).not.toHaveBeenCalled();
expect(toggleModal).not.toHaveBeenCalled();
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
it('does not focus input by default', () => {
expect(document.activeElement).toBe(document.body);
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
describe.each`
entryType | path | modalTitle | btnTitle | showsFileTemplates | inputValue | inputPlaceholder
${'tree'} | ${''} | ${'Create new directory'} | ${'Create directory'} | ${false} | ${''} | ${'dir/'}
${'blob'} | ${''} | ${'Create new file'} | ${'Create file'} | ${true} | ${''} | ${'dir/file_name'}
${'blob'} | ${'foo/bar'} | ${'Create new file'} | ${'Create file'} | ${true} | ${'foo/bar/'} | ${'dir/file_name'}
`(
'when opened as $entryType with path "$path"',
({
entryType,
path,
modalTitle,
btnTitle,
showsFileTemplates,
inputValue,
inputPlaceholder,
}) => {
beforeEach(async () => {
mountComponent();
open(entryType, path);
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2022-08-13 15:12:31 +05:30
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
it('sets modal props', () => {
expect(findGlModal().props()).toMatchObject({
title: modalTitle,
actionPrimary: {
attributes: [{ variant: 'confirm' }],
text: btnTitle,
},
});
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
it('sets input attributes', () => {
expect(findInput().element.value).toBe(inputValue);
expect(findInput().attributes('placeholder')).toBe(inputPlaceholder);
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
it(`shows file templates: ${showsFileTemplates}`, () => {
const actual = findTemplateButtonsModel().length > 0;
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
expect(actual).toBe(showsFileTemplates);
});
it('shows modal', () => {
expect(showModal).toHaveBeenCalled();
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
it('focus on input', () => {
expect(document.activeElement).toBe(findInput().element);
});
it('resets when canceled', async () => {
triggerCancel();
await nextTick();
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
// Resets input value
expect(findInput().element.value).toBe('');
// Resets to blob mode
expect(findGlModal().props('title')).toBe('Create new file');
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
},
);
describe.each`
modalType | name | expectedName
${'blob'} | ${'foo/bar.js'} | ${'foo/bar.js'}
${'blob'} | ${'foo /bar.js'} | ${'foo/bar.js'}
${'tree'} | ${'foo/dir'} | ${'foo/dir'}
${'tree'} | ${'foo /dir'} | ${'foo/dir'}
`('when submitting as $modalType with "$name"', ({ modalType, name, expectedName }) => {
describe('when using the modal primary button', () => {
beforeEach(async () => {
mountComponent();
open(modalType, '');
await nextTick();
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
findInput().setValue(name);
triggerSubmitModal();
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
it('triggers createTempEntry action', () => {
expect(store.dispatch).toHaveBeenCalledWith('createTempEntry', {
name: expectedName,
type: modalType,
});
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
});
describe('when triggering form submit (pressing enter)', () => {
beforeEach(async () => {
mountComponent();
open(modalType, '');
await nextTick();
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
findInput().setValue(name);
triggerSubmitForm();
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
it('triggers createTempEntry action', () => {
expect(store.dispatch).toHaveBeenCalledWith('createTempEntry', {
name: expectedName,
type: modalType,
});
2020-05-24 23:13:21 +05:30
});
});
});
2022-08-13 15:12:31 +05:30
describe('when creating from template type', () => {
beforeEach(async () => {
mountComponent();
2020-06-23 00:09:42 +05:30
2022-08-13 15:12:31 +05:30
open('blob', 'some_dir');
2020-06-23 00:09:42 +05:30
2022-08-13 15:12:31 +05:30
await nextTick();
2020-06-23 00:09:42 +05:30
2022-08-13 15:12:31 +05:30
// Set input, then trigger button
findInput().setValue('some_dir/foo.js');
findTemplateButtons().at(1).vm.$emit('click');
2020-06-23 00:09:42 +05:30
});
2022-08-13 15:12:31 +05:30
it('triggers createTempEntry action', () => {
const { name: expectedName } = store.getters['fileTemplates/templateTypes'][1];
2020-06-23 00:09:42 +05:30
2022-08-13 15:12:31 +05:30
expect(store.dispatch).toHaveBeenCalledWith('createTempEntry', {
name: `some_dir/${expectedName}`,
type: 'blob',
});
});
2020-06-23 00:09:42 +05:30
2022-08-13 15:12:31 +05:30
it('toggles modal', () => {
expect(toggleModal).toHaveBeenCalled();
});
2020-06-23 00:09:42 +05:30
});
2022-08-13 15:12:31 +05:30
describe.each`
origPath | title | inputValue | inputSelectionStart
${'src/parent_dir'} | ${'Rename folder'} | ${'src/parent_dir'} | ${'src/'.length}
${'README.md'} | ${'Rename file'} | ${'README.md'} | ${0}
`('when renaming for $origPath', ({ origPath, title, inputValue, inputSelectionStart }) => {
beforeEach(async () => {
mountComponent();
open('rename', origPath);
await nextTick();
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
it('sets modal props for renaming', () => {
expect(findGlModal().props()).toMatchObject({
title,
actionPrimary: {
attributes: [{ variant: 'confirm' }],
text: title,
2020-05-24 23:13:21 +05:30
},
2022-08-13 15:12:31 +05:30
});
});
it('sets input value', () => {
expect(findInput().element.value).toBe(inputValue);
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
it(`does not show file templates`, () => {
expect(findTemplateButtonsModel()).toHaveLength(0);
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
it('shows modal when renaming', () => {
expect(showModal).toHaveBeenCalled();
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
it('focus on input when renaming', () => {
expect(document.activeElement).toBe(findInput().element);
});
it('selects name part of the input', () => {
expect(findInput().element.selectionStart).toBe(inputSelectionStart);
expect(findInput().element.selectionEnd).toBe(origPath.length);
});
describe('when renames is submitted successfully', () => {
describe('when using the modal primary button', () => {
beforeEach(() => {
findInput().setValue(NEW_NAME);
triggerSubmitModal();
});
it('dispatches renameEntry event', () => {
expect(store.dispatch).toHaveBeenCalledWith('renameEntry', {
path: origPath,
parentPath: '',
name: NEW_NAME,
});
});
it('does not trigger flash', () => {
2022-11-25 23:54:43 +05:30
expect(createAlert).not.toHaveBeenCalled();
2022-08-13 15:12:31 +05:30
});
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
describe('when triggering form submit (pressing enter)', () => {
beforeEach(() => {
findInput().setValue(NEW_NAME);
triggerSubmitForm();
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
it('dispatches renameEntry event', () => {
expect(store.dispatch).toHaveBeenCalledWith('renameEntry', {
path: origPath,
parentPath: '',
name: NEW_NAME,
});
});
it('does not trigger flash', () => {
2022-11-25 23:54:43 +05:30
expect(createAlert).not.toHaveBeenCalled();
2022-08-13 15:12:31 +05:30
});
});
});
});
describe('when renaming and file already exists', () => {
beforeEach(async () => {
mountComponent();
open('rename', 'src/parent_dir');
await nextTick();
// Set to something that already exists!
findInput().setValue('src');
triggerSubmitModal();
});
it('creates flash', () => {
2022-11-25 23:54:43 +05:30
expect(createAlert).toHaveBeenCalledWith({
2022-08-13 15:12:31 +05:30
message: 'The name "src" is already taken in this directory.',
2021-09-04 01:27:46 +05:30
fadeTransition: false,
addBodyClass: true,
});
2020-05-24 23:13:21 +05:30
});
2022-08-13 15:12:31 +05:30
it('does not dispatch event', () => {
expect(store.dispatch).not.toHaveBeenCalled();
});
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
describe('when renaming and file has been deleted', () => {
beforeEach(async () => {
mountComponent();
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
open('rename', 'src/parent_dir/foo.js');
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
await nextTick();
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
findInput().setValue('src/deleted.js');
triggerSubmitModal();
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
it('does not create flash', () => {
2022-11-25 23:54:43 +05:30
expect(createAlert).not.toHaveBeenCalled();
2022-08-13 15:12:31 +05:30
});
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
it('dispatches event', () => {
expect(store.dispatch).toHaveBeenCalledWith('renameEntry', {
path: 'src/parent_dir/foo.js',
name: 'deleted.js',
parentPath: 'src',
});
2020-05-24 23:13:21 +05:30
});
});
});