debian-mirror-gitlab/spec/frontend/issuable/issuable_form_spec.js
2022-08-13 15:12:31 +05:30

111 lines
3.1 KiB
JavaScript

import $ from 'jquery';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import IssuableForm from '~/issuable/issuable_form';
import setWindowLocation from 'helpers/set_window_location_helper';
describe('IssuableForm', () => {
let instance;
const createIssuable = (form) => {
instance = new IssuableForm(form);
};
beforeEach(() => {
setHTMLFixture(`
<form>
<input name="[title]" />
</form>
`);
createIssuable($('form'));
});
afterEach(() => {
resetHTMLFixture();
});
describe('initAutosave', () => {
it('creates autosave with the searchTerm included', () => {
setWindowLocation('https://gitlab.test/foo?bar=true');
const autosave = instance.initAutosave();
expect(autosave.key.includes('bar=true')).toBe(true);
});
it("creates autosave fields without the searchTerm if it's an issue new form", () => {
setHTMLFixture(`
<form data-new-issue-path="/issues/new">
<input name="[title]" />
</form>
`);
createIssuable($('form'));
setWindowLocation('https://gitlab.test/issues/new?bar=true');
const autosave = instance.initAutosave();
expect(autosave.key.includes('bar=true')).toBe(false);
});
});
describe('resetAutosave', () => {
it('resets autosave on elements with the .js-reset-autosave class', () => {
setHTMLFixture(`
<form>
<input name="[title]" />
<textarea name="[description]"></textarea>
<a class="js-reset-autosave">Cancel</a>
</form>
`);
const $form = $('form');
const resetAutosave = jest.spyOn(IssuableForm.prototype, 'resetAutosave');
createIssuable($form);
$form.find('.js-reset-autosave').trigger('click');
expect(resetAutosave).toHaveBeenCalled();
});
});
describe('removeWip', () => {
it.each`
prefix
${'draFT: '}
${' [DRaft] '}
${'drAft:'}
${'[draFT]'}
${'(draft) '}
${' (DrafT)'}
${'draft: [draft] (draft)'}
`('removes "$prefix" from the beginning of the title', ({ prefix }) => {
instance.titleField.val(`${prefix}The Issuable's Title Value`);
instance.removeWip();
expect(instance.titleField.val()).toBe("The Issuable's Title Value");
});
});
describe('addWip', () => {
it("properly adds the work in progress prefix to the Issuable's title", () => {
instance.titleField.val("The Issuable's Title Value");
instance.addWip();
expect(instance.titleField.val()).toBe("Draft: The Issuable's Title Value");
});
});
describe('workInProgress', () => {
it.each`
title | expected
${'draFT: something is happening'} | ${true}
${'draft something is happening'} | ${false}
${'something is happening to drafts'} | ${false}
${'something is happening'} | ${false}
`('returns $expected with "$title"', ({ title, expected }) => {
instance.titleField.val(title);
expect(instance.workInProgress()).toBe(expected);
});
});
});