debian-mirror-gitlab/spec/frontend/blob_edit/blob_bundle_spec.js

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

128 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-02-15 15:39:39 +05:30
import $ from 'jquery';
2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2020-04-22 19:07:51 +05:30
import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
2021-01-03 14:25:43 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2020-10-24 23:57:45 +05:30
import blobBundle from '~/blob_edit/blob_bundle';
2019-02-15 15:39:39 +05:30
2021-09-30 23:02:18 +05:30
import SourceEditor from '~/blob_edit/edit_blob';
2023-05-27 22:25:52 +05:30
import { createAlert } from '~/alert';
2021-01-03 14:25:43 +05:30
2020-04-08 14:13:33 +05:30
jest.mock('~/blob_edit/edit_blob');
2023-05-27 22:25:52 +05:30
jest.mock('~/alert');
2020-04-08 14:13:33 +05:30
2019-02-15 15:39:39 +05:30
describe('BlobBundle', () => {
2023-07-09 08:55:56 +05:30
beforeAll(() => {
// HACK: Workaround readonly property in Jest
Object.defineProperty(window, 'onbeforeunload', {
writable: true,
});
});
2021-09-30 23:02:18 +05:30
it('does not load SourceEditor by default', () => {
2021-01-03 14:25:43 +05:30
blobBundle();
2021-09-30 23:02:18 +05:30
expect(SourceEditor).not.toHaveBeenCalled();
2021-01-03 14:25:43 +05:30
});
2021-09-30 23:02:18 +05:30
it('loads SourceEditor for the edit screen', async () => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(`<div class="js-edit-blob-form"></div>`);
2021-01-03 14:25:43 +05:30
blobBundle();
await waitForPromises();
2021-09-30 23:02:18 +05:30
expect(SourceEditor).toHaveBeenCalled();
2022-07-16 23:28:13 +05:30
resetHTMLFixture();
2021-01-03 14:25:43 +05:30
});
2020-04-22 19:07:51 +05:30
describe('No Suggest Popover', () => {
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(`
2019-02-15 15:39:39 +05:30
<div class="js-edit-blob-form" data-blob-filename="blah">
<button class="js-commit-button"></button>
2022-05-07 20:08:51 +05:30
<button id='cancel-changes'></button>
2019-02-15 15:39:39 +05:30
</div>`);
2020-04-22 19:07:51 +05:30
blobBundle();
});
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2020-04-22 19:07:51 +05:30
it('sets the window beforeunload listener to a function returning a string', () => {
expect(window.onbeforeunload()).toBe('');
});
2019-02-15 15:39:39 +05:30
2020-04-22 19:07:51 +05:30
it('removes beforeunload listener if commit button is clicked', () => {
$('.js-commit-button').click();
2019-02-15 15:39:39 +05:30
2020-04-22 19:07:51 +05:30
expect(window.onbeforeunload).toBeNull();
});
it('removes beforeunload listener when cancel link is clicked', () => {
2022-05-07 20:08:51 +05:30
$('#cancel-changes').click();
2020-04-22 19:07:51 +05:30
expect(window.onbeforeunload).toBeNull();
});
2019-02-15 15:39:39 +05:30
});
2020-04-22 19:07:51 +05:30
describe('Suggest Popover', () => {
let trackingSpy;
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(`
2020-04-22 19:07:51 +05:30
<div class="js-edit-blob-form" data-blob-filename="blah" id="target">
<div class="js-suggest-gitlab-ci-yml"
data-target="#target"
data-track-label="suggest_gitlab_ci_yml"
data-dismiss-key="1"
2020-11-24 15:15:51 +05:30
data-human-access="owner"
data-merge-request-path="path/to/mr">
2020-04-22 19:07:51 +05:30
<button id='commit-changes' class="js-commit-button"></button>
2022-05-07 20:08:51 +05:30
<button id='cancel-changes'></button>
2020-04-22 19:07:51 +05:30
</div>
</div>`);
trackingSpy = mockTracking('_category_', $('#commit-changes').element, jest.spyOn);
document.body.dataset.page = 'projects:blob:new';
blobBundle();
});
afterEach(() => {
unmockTracking();
2022-07-16 23:28:13 +05:30
resetHTMLFixture();
2020-04-22 19:07:51 +05:30
});
it('sends a tracking event when the commit button is clicked', () => {
$('#commit-changes').click();
2019-02-15 15:39:39 +05:30
2020-04-22 19:07:51 +05:30
expect(trackingSpy).toHaveBeenCalledTimes(1);
expect(trackingSpy).toHaveBeenCalledWith(undefined, undefined, {
label: 'suggest_gitlab_ci_yml',
property: 'owner',
});
});
2019-02-15 15:39:39 +05:30
});
2023-03-04 22:38:38 +05:30
describe('Error handling', () => {
let message;
beforeEach(() => {
setHTMLFixture(`<div class="js-edit-blob-form" data-blob-filename="blah"></div>`);
message = 'Foo';
SourceEditor.mockImplementation(() => {
throw new Error(message);
});
});
afterEach(() => {
resetHTMLFixture();
SourceEditor.mockClear();
});
it('correctly outputs error message when it occurs', async () => {
blobBundle();
await waitForPromises();
expect(createAlert).toHaveBeenCalledWith({ message });
});
});
2019-02-15 15:39:39 +05:30
});