2022-05-13 12:08:49 +00:00
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2021-10-11 18:09:46 +00:00
|
|
|
import TemplateSelectorMediator from '~/blob/file_template_mediator';
|
|
|
|
|
|
|
|
describe('Template Selector Mediator', () => {
|
|
|
|
let mediator;
|
|
|
|
|
|
|
|
describe('setFilename', () => {
|
|
|
|
let input;
|
|
|
|
const newFileName = 'foo';
|
|
|
|
const editor = jest.fn().mockImplementationOnce(() => ({
|
|
|
|
getValue: jest.fn().mockImplementation(() => {}),
|
|
|
|
}))();
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-05-13 12:08:49 +00:00
|
|
|
setHTMLFixture('<div class="file-editor"><input class="js-file-path-name-input" /></div>');
|
2021-10-11 18:09:46 +00:00
|
|
|
input = document.querySelector('.js-file-path-name-input');
|
|
|
|
mediator = new TemplateSelectorMediator({
|
|
|
|
editor,
|
|
|
|
currentAction: jest.fn(),
|
|
|
|
projectId: jest.fn(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-13 12:08:49 +00:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2021-10-11 18:09:46 +00:00
|
|
|
it('fills out the input field', () => {
|
|
|
|
expect(input.value).toBe('');
|
|
|
|
mediator.setFilename(newFileName);
|
|
|
|
expect(input.value).toBe(newFileName);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
name | newName | shouldDispatch
|
|
|
|
${newFileName} | ${newFileName} | ${false}
|
|
|
|
${newFileName} | ${''} | ${true}
|
|
|
|
${newFileName} | ${undefined} | ${false}
|
|
|
|
${''} | ${''} | ${false}
|
|
|
|
${''} | ${newFileName} | ${true}
|
|
|
|
${''} | ${undefined} | ${false}
|
|
|
|
`(
|
|
|
|
'correctly reacts to the name change when current name is $name and newName is $newName',
|
|
|
|
({ name, newName, shouldDispatch }) => {
|
|
|
|
input.value = name;
|
|
|
|
const eventHandler = jest.fn();
|
|
|
|
input.addEventListener('change', eventHandler);
|
|
|
|
|
|
|
|
mediator.setFilename(newName);
|
|
|
|
if (shouldDispatch) {
|
|
|
|
expect(eventHandler).toHaveBeenCalledTimes(1);
|
|
|
|
} else {
|
|
|
|
expect(eventHandler).not.toHaveBeenCalled();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|