2019-11-14 13:06:15 -05:00
|
|
|
/* eslint-disable no-return-assign */
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-12-13 04:26:44 -05:00
|
|
|
import syntaxHighlight from '~/syntax_highlight';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
describe('Syntax Highlighter', () => {
|
2020-12-23 16:10:24 -05:00
|
|
|
const stubUserColorScheme = (value) => {
|
2017-12-13 04:26:44 -05:00
|
|
|
if (window.gon == null) {
|
|
|
|
window.gon = {};
|
|
|
|
}
|
2018-10-17 03:13:26 -04:00
|
|
|
return (window.gon.user_color_scheme = value);
|
2017-12-13 04:26:44 -05:00
|
|
|
};
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2021-08-11 08:10:59 -04:00
|
|
|
// 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(() => {
|
|
|
|
setFixtures('<div class="js-syntax-highlight"></div>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('applies syntax highlighting', () => {
|
|
|
|
stubUserColorScheme('monokai');
|
|
|
|
syntaxHighlight(fn('.js-syntax-highlight'));
|
|
|
|
|
|
|
|
expect(fn('.js-syntax-highlight')).toHaveClass('monokai');
|
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2021-08-11 08:10:59 -04:00
|
|
|
describe('on a parent element', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setFixtures(
|
|
|
|
'<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-10-09 13:01:49 -04:00
|
|
|
|
2021-08-11 08:10:59 -04:00
|
|
|
it('applies highlighting to all applicable children', () => {
|
|
|
|
stubUserColorScheme('monokai');
|
|
|
|
syntaxHighlight(fn('.parent'));
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2021-08-11 08:10:59 -04:00
|
|
|
expect(fn('.parent')).not.toHaveClass('monokai');
|
|
|
|
expect(fn('.foo')).not.toHaveClass('monokai');
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.monokai').length).toBe(2);
|
|
|
|
});
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2021-08-11 08:10:59 -04:00
|
|
|
it('prevents an infinite loop when no matches exist', () => {
|
|
|
|
setFixtures('<div></div>');
|
|
|
|
const highlight = () => syntaxHighlight(fn('div'));
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2021-08-11 08:10:59 -04:00
|
|
|
expect(highlight).not.toThrow();
|
|
|
|
});
|
2017-12-13 04:26:44 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|