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.

139 lines
3.4 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';
2023-07-09 08:55:56 +05:30
import htmlSnippetsShow from 'test_fixtures/snippets/show.html';
import { Mousetrap } from '~/lib/mousetrap';
import { setHTMLFixture, 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';
2023-04-23 21:23:45 +05:30
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
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;
2018-03-17 18:26:18 +05:30
2023-05-27 22:25:52 +05:30
const getTextarea = () => $('.notes-form textarea');
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() {
2023-05-27 22:25:52 +05:30
getTextarea().trigger(
2018-11-08 19:23:39 +05:30
$.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);
2023-04-23 21:23:45 +05:30
mock.onGet().reply(HTTP_STATUS_OK);
2020-06-23 00:09:42 +05:30
2023-07-09 08:55:56 +05:30
setHTMLFixture(htmlSnippetsShow);
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(() => {
2023-05-27 22:25:52 +05:30
$(document).off('click', '.js-zen-enter');
$(document).off('click', '.js-zen-leave');
$(document).off('zen_mode:enter');
$(document).off('zen_mode:leave');
$(document).off('keydown');
2022-07-16 23:28:13 +05:30
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
2023-05-27 22:25:52 +05:30
expect(dropzoneForElementSpy).not.toHaveBeenCalled();
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
2023-05-27 22:25:52 +05:30
expect(dropzoneForElementSpy).toHaveBeenCalledTimes(1);
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', () => {
2023-05-27 22:25:52 +05:30
getTextarea().attr('style', 'height: 400px');
2018-03-17 18:26:18 +05:30
enterZen();
2018-12-13 13:39:08 +05:30
2023-05-27 22:25:52 +05:30
expect(getTextarea()).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
});
});
2023-05-27 22:25:52 +05:30
it('restores textarea style', () => {
const style = 'color: red; overflow-y: hidden;';
getTextarea().attr('style', style);
expect(getTextarea()).toHaveAttr('style', style);
enterZen();
exitZen();
expect(getTextarea()).toHaveAttr('style', style);
});
2018-03-17 18:26:18 +05:30
});