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

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

68 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-12-26 22:10:19 +05:30
/* eslint-disable no-return-assign */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2018-03-17 18:26:18 +05:30
import syntaxHighlight from '~/syntax_highlight';
2016-09-13 17:45:13 +05:30
2020-03-13 15:44:24 +05:30
describe('Syntax Highlighter', () => {
2021-03-08 18:12:59 +05:30
const stubUserColorScheme = (value) => {
2018-03-17 18:26:18 +05:30
if (window.gon == null) {
window.gon = {};
}
2018-12-13 13:39:08 +05:30
return (window.gon.user_color_scheme = value);
2018-03-17 18:26:18 +05:30
};
2018-12-13 13:39:08 +05:30
2021-10-27 15:23:28 +05:30
// We have to bind `document.querySelectorAll` to `document` to not mess up the fn's context
describe.each`
desc | fn
${'jquery'} | ${$}
${'vanilla all'} | ${document.querySelectorAll.bind(document)}
${'vanilla single'} | ${document.querySelector.bind(document)}
`('highlight using $desc syntax', ({ fn }) => {
describe('on a js-syntax-highlight element', () => {
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture('<div class="js-syntax-highlight"></div>');
});
afterEach(() => {
resetHTMLFixture();
2021-10-27 15:23:28 +05:30
});
it('applies syntax highlighting', () => {
stubUserColorScheme('monokai');
syntaxHighlight(fn('.js-syntax-highlight'));
expect(fn('.js-syntax-highlight')).toHaveClass('monokai');
});
2016-09-13 17:45:13 +05:30
});
2018-12-13 13:39:08 +05:30
2021-10-27 15:23:28 +05:30
describe('on a parent element', () => {
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(
2021-10-27 15:23:28 +05:30
'<div class="parent">\n <div class="js-syntax-highlight"></div>\n <div class="foo"></div>\n <div class="js-syntax-highlight"></div>\n</div>',
);
});
2018-12-13 13:39:08 +05:30
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2021-10-27 15:23:28 +05:30
it('applies highlighting to all applicable children', () => {
stubUserColorScheme('monokai');
syntaxHighlight(fn('.parent'));
2018-12-13 13:39:08 +05:30
2021-10-27 15:23:28 +05:30
expect(fn('.parent')).not.toHaveClass('monokai');
expect(fn('.foo')).not.toHaveClass('monokai');
expect(document.querySelectorAll('.monokai').length).toBe(2);
});
2018-12-13 13:39:08 +05:30
2021-10-27 15:23:28 +05:30
it('prevents an infinite loop when no matches exist', () => {
2022-07-16 23:28:13 +05:30
setHTMLFixture('<div></div>');
2021-10-27 15:23:28 +05:30
const highlight = () => syntaxHighlight(fn('div'));
2018-12-13 13:39:08 +05:30
2021-10-27 15:23:28 +05:30
expect(highlight).not.toThrow();
});
2018-03-17 18:26:18 +05:30
});
});
});