gitlab-org--gitlab-foss/spec/javascripts/syntax_highlight_spec.js

51 lines
1.5 KiB
JavaScript
Raw Normal View History

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