debian-mirror-gitlab/spec/frontend/zen_mode_spec.js

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

119 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-06-23 00:09:42 +05:30
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
2018-03-17 18:26:18 +05:30
import Dropzone from 'dropzone';
2021-03-11 19:13:27 +05:30
import $ from 'jquery';
2018-11-08 19:23:39 +05:30
import Mousetrap from 'mousetrap';
2022-07-16 23:28:13 +05:30
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2021-11-11 11:23:49 +05:30
import GLForm from '~/gl_form';
2021-03-08 18:12:59 +05:30
import * as utils from '~/lib/utils/common_utils';
2017-09-10 17:25:29 +05:30
import ZenMode from '~/zen_mode';
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
describe('ZenMode', () => {
2020-06-23 00:09:42 +05:30
let mock;
2018-03-17 18:26:18 +05:30
let zen;
2018-11-08 19:23:39 +05:30
let dropzoneForElementSpy;
2019-07-07 11:18:12 +05:30
const fixtureName = 'snippets/show.html';
2018-03-17 18:26:18 +05:30
function enterZen() {
$('.notes-form .js-zen-enter').click();
}
function exitZen() {
$('.notes-form .js-zen-leave').click();
}
function escapeKeydown() {
2018-11-08 19:23:39 +05:30
$('.notes-form textarea').trigger(
$.Event('keydown', {
keyCode: 27,
}),
);
2018-03-17 18:26:18 +05:30
}
beforeEach(() => {
2020-06-23 00:09:42 +05:30
mock = new MockAdapter(axios);
mock.onGet().reply(200);
2022-07-16 23:28:13 +05:30
loadHTMLFixture(fixtureName);
2021-11-11 11:23:49 +05:30
const form = $('.js-new-note-form');
new GLForm(form); // eslint-disable-line no-new
2018-03-17 18:26:18 +05:30
2020-06-23 00:09:42 +05:30
dropzoneForElementSpy = jest.spyOn(Dropzone, 'forElement').mockImplementation(() => ({
2018-03-17 18:26:18 +05:30
enable: () => true,
}));
zen = new ZenMode();
// Set this manually because we can't actually scroll the window
zen.scroll_position = 456;
2022-07-16 23:28:13 +05:30
});
2022-04-04 11:22:00 +05:30
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
2018-03-17 18:26:18 +05:30
});
2018-11-08 19:23:39 +05:30
describe('enabling dropzone', () => {
beforeEach(() => {
enterZen();
});
it('should not call dropzone if element is not dropzone valid', () => {
$('.div-dropzone').addClass('js-invalid-dropzone');
exitZen();
2018-12-13 13:39:08 +05:30
2020-06-23 00:09:42 +05:30
expect(dropzoneForElementSpy.mock.calls.length).toEqual(0);
2018-11-08 19:23:39 +05:30
});
it('should call dropzone if element is dropzone valid', () => {
$('.div-dropzone').removeClass('js-invalid-dropzone');
exitZen();
2018-12-13 13:39:08 +05:30
2020-06-23 00:09:42 +05:30
expect(dropzoneForElementSpy.mock.calls.length).toEqual(2);
2018-11-08 19:23:39 +05:30
});
});
2018-03-17 18:26:18 +05:30
describe('on enter', () => {
it('pauses Mousetrap', () => {
2020-06-23 00:09:42 +05:30
const mouseTrapPauseSpy = jest.spyOn(Mousetrap, 'pause');
2018-03-17 18:26:18 +05:30
enterZen();
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(mouseTrapPauseSpy).toHaveBeenCalled();
2018-03-17 18:26:18 +05:30
});
it('removes textarea styling', () => {
$('.notes-form textarea').attr('style', 'height: 400px');
enterZen();
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect($('.notes-form textarea')).not.toHaveAttr('style');
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
});
describe('in use', () => {
beforeEach(enterZen);
it('exits on Escape', () => {
escapeKeydown();
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect($('.notes-form .zen-backdrop')).not.toHaveClass('fullscreen');
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
});
describe('on exit', () => {
beforeEach(enterZen);
it('unpauses Mousetrap', () => {
2020-06-23 00:09:42 +05:30
const mouseTrapUnpauseSpy = jest.spyOn(Mousetrap, 'unpause');
2018-03-17 18:26:18 +05:30
exitZen();
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(mouseTrapUnpauseSpy).toHaveBeenCalled();
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
it('restores the scroll position', () => {
2021-03-08 18:12:59 +05:30
jest.spyOn(utils, 'scrollToElement');
2018-03-17 18:26:18 +05:30
exitZen();
2018-12-13 13:39:08 +05:30
2021-03-08 18:12:59 +05:30
expect(utils.scrollToElement).toHaveBeenCalled();
2016-09-13 17:45:13 +05:30
});
});
2018-03-17 18:26:18 +05:30
});