Added scroll_helper tests

This commit is contained in:
Luke "Jared" Bennett 2017-07-19 19:46:34 +01:00
parent 929c66d620
commit 99193d824f
No known key found for this signature in database
GPG Key ID: 402ED51FB5D306C2
3 changed files with 42 additions and 23 deletions

View File

@ -54,7 +54,7 @@ import RefSelectDropdown from './ref_select_dropdown';
import GfmAutoComplete from './gfm_auto_complete';
import ShortcutsBlob from './shortcuts_blob';
import initSettingsPanels from './settings_panels';
import { setScrollWidth } from './helpers/scroll_helper';
import ScrollHelper from './helpers/scroll_helper';
(function() {
var Dispatcher;
@ -77,7 +77,7 @@ import { setScrollWidth } from './helpers/scroll_helper';
return false;
}
setScrollWidth();
ScrollHelper.setScrollWidth();
path = page.split(':');
shortcut_handler = null;

View File

@ -1,29 +1,30 @@
function getScrollWidth() {
const $rulerContainer = $('<div>').css({
visibility: 'hidden',
width: 100,
overflow: 'scroll',
});
import $ from 'jquery';
const $ruler = $('<div>').css({
width: '100%',
});
const ScrollHelper = {
getScrollWidth() {
const $rulerContainer = $('<div>').css({
visibility: 'hidden',
width: 100,
overflow: 'scroll',
});
$ruler.appendTo($rulerContainer);
$rulerContainer.appendTo('body');
const $ruler = $('<div>').css({
width: '100%',
});
const scrollWidth = $ruler.outerWidth();
$ruler.appendTo($rulerContainer);
$rulerContainer.appendTo('body');
$rulerContainer.remove();
const scrollWidth = $ruler.outerWidth();
return 100 - scrollWidth;
}
$rulerContainer.remove();
function setScrollWidth() {
$('body').attr('data-scroll-width', getScrollWidth());
}
return 100 - scrollWidth;
},
export {
getScrollWidth,
setScrollWidth,
setScrollWidth() {
$('body').attr('data-scroll-width', ScrollHelper.getScrollWidth());
},
};
export default ScrollHelper;

View File

@ -0,0 +1,18 @@
import $ from 'jquery';
import ScrollHelper from '~/helpers/scroll_helper';
describe('ScrollHelper', () => {
describe('setScrollWidth', () => {
it('calls getScrollWidth and sets data-scroll-width', () => {
const width = 10;
spyOn($.fn, 'attr');
spyOn(ScrollHelper, 'getScrollWidth').and.returnValue(width);
ScrollHelper.setScrollWidth();
expect(ScrollHelper.getScrollWidth).toHaveBeenCalled();
expect($.fn.attr).toHaveBeenCalledWith('data-scroll-width', width);
});
});
});