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

49 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-12-26 22:10:19 +05:30
/* eslint-disable no-return-assign */
2016-09-13 17:45:13 +05:30
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
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', () => {
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
};
2020-03-13 15:44:24 +05:30
describe('on a js-syntax-highlight element', () => {
beforeEach(() => {
setFixtures('<div class="js-syntax-highlight"></div>');
2016-09-13 17:45:13 +05:30
});
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
it('applies syntax highlighting', () => {
2018-03-17 18:26:18 +05:30
stubUserColorScheme('monokai');
syntaxHighlight($('.js-syntax-highlight'));
2018-12-13 13:39:08 +05:30
expect($('.js-syntax-highlight')).toHaveClass('monokai');
2016-09-13 17:45:13 +05:30
});
});
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
describe('on a parent element', () => {
beforeEach(() => {
setFixtures(
2018-12-13 13:39:08 +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-03-17 18:26:18 +05:30
});
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
it('applies highlighting to all applicable children', () => {
2018-03-17 18:26:18 +05:30
stubUserColorScheme('monokai');
syntaxHighlight($('.parent'));
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect($('.parent, .foo')).not.toHaveClass('monokai');
2018-12-13 13:39:08 +05:30
expect($('.monokai').length).toBe(2);
2018-03-17 18:26:18 +05:30
});
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
it('prevents an infinite loop when no matches exist', () => {
2018-03-17 18:26:18 +05:30
setFixtures('<div></div>');
2020-03-13 15:44:24 +05:30
const highlight = () => syntaxHighlight($('div'));
2018-12-13 13:39:08 +05:30
expect(highlight).not.toThrow();
2018-03-17 18:26:18 +05:30
});
});
});