debian-mirror-gitlab/spec/javascripts/shared/popover_spec.js

167 lines
4.5 KiB
JavaScript
Raw Normal View History

2018-10-15 14:42:47 +05:30
import $ from 'jquery';
2018-12-13 13:39:08 +05:30
import { togglePopover, mouseleave, mouseenter } from '~/shared/popover';
2018-10-15 14:42:47 +05:30
describe('popover', () => {
describe('togglePopover', () => {
describe('togglePopover(true)', () => {
it('returns true when popover is shown', () => {
const context = {
hasClass: () => false,
popover: () => {},
toggleClass: () => {},
};
expect(togglePopover.call(context, true)).toEqual(true);
});
it('returns false when popover is already shown', () => {
const context = {
hasClass: () => true,
};
expect(togglePopover.call(context, true)).toEqual(false);
});
2018-12-13 13:39:08 +05:30
it('shows popover', done => {
2018-10-15 14:42:47 +05:30
const context = {
hasClass: () => false,
popover: () => {},
toggleClass: () => {},
};
2018-12-13 13:39:08 +05:30
spyOn(context, 'popover').and.callFake(method => {
2018-10-15 14:42:47 +05:30
expect(method).toEqual('show');
done();
});
togglePopover.call(context, true);
});
2018-12-13 13:39:08 +05:30
it('adds disable-animation and js-popover-show class', done => {
2018-10-15 14:42:47 +05:30
const context = {
hasClass: () => false,
popover: () => {},
toggleClass: () => {},
};
spyOn(context, 'toggleClass').and.callFake((classNames, show) => {
expect(classNames).toEqual('disable-animation js-popover-show');
expect(show).toEqual(true);
done();
});
togglePopover.call(context, true);
});
});
describe('togglePopover(false)', () => {
it('returns true when popover is hidden', () => {
const context = {
hasClass: () => true,
popover: () => {},
toggleClass: () => {},
};
expect(togglePopover.call(context, false)).toEqual(true);
});
it('returns false when popover is already hidden', () => {
const context = {
hasClass: () => false,
};
expect(togglePopover.call(context, false)).toEqual(false);
});
2018-12-13 13:39:08 +05:30
it('hides popover', done => {
2018-10-15 14:42:47 +05:30
const context = {
hasClass: () => true,
popover: () => {},
toggleClass: () => {},
};
2018-12-13 13:39:08 +05:30
spyOn(context, 'popover').and.callFake(method => {
2018-10-15 14:42:47 +05:30
expect(method).toEqual('hide');
done();
});
togglePopover.call(context, false);
});
2018-12-13 13:39:08 +05:30
it('removes disable-animation and js-popover-show class', done => {
2018-10-15 14:42:47 +05:30
const context = {
hasClass: () => true,
popover: () => {},
toggleClass: () => {},
};
spyOn(context, 'toggleClass').and.callFake((classNames, show) => {
expect(classNames).toEqual('disable-animation js-popover-show');
expect(show).toEqual(false);
done();
});
togglePopover.call(context, false);
});
});
});
describe('mouseleave', () => {
it('calls hide popover if .popover:hover is false', () => {
const fakeJquery = {
length: 0,
};
2018-12-23 12:14:25 +05:30
spyOn($.fn, 'init').and.callFake(selector =>
selector === '.popover:hover' ? fakeJquery : $.fn,
2018-12-13 13:39:08 +05:30
);
2018-10-15 14:42:47 +05:30
spyOn(togglePopover, 'call');
mouseleave();
2018-12-13 13:39:08 +05:30
2018-10-15 14:42:47 +05:30
expect(togglePopover.call).toHaveBeenCalledWith(jasmine.any(Object), false);
});
it('does not call hide popover if .popover:hover is true', () => {
const fakeJquery = {
length: 1,
};
2018-12-23 12:14:25 +05:30
spyOn($.fn, 'init').and.callFake(selector =>
selector === '.popover:hover' ? fakeJquery : $.fn,
2018-12-13 13:39:08 +05:30
);
2018-10-15 14:42:47 +05:30
spyOn(togglePopover, 'call');
mouseleave();
2018-12-13 13:39:08 +05:30
2018-10-15 14:42:47 +05:30
expect(togglePopover.call).not.toHaveBeenCalledWith(false);
});
});
describe('mouseenter', () => {
const context = {};
it('shows popover', () => {
spyOn(togglePopover, 'call').and.returnValue(false);
mouseenter.call(context);
2018-12-13 13:39:08 +05:30
2018-10-15 14:42:47 +05:30
expect(togglePopover.call).toHaveBeenCalledWith(jasmine.any(Object), true);
});
2018-12-13 13:39:08 +05:30
it('registers mouseleave event if popover is showed', done => {
2018-10-15 14:42:47 +05:30
spyOn(togglePopover, 'call').and.returnValue(true);
2018-12-13 13:39:08 +05:30
spyOn($.fn, 'on').and.callFake(eventName => {
2018-10-15 14:42:47 +05:30
expect(eventName).toEqual('mouseleave');
done();
});
mouseenter.call(context);
});
it('does not register mouseleave event if popover is not showed', () => {
spyOn(togglePopover, 'call').and.returnValue(false);
const spy = spyOn($.fn, 'on').and.callFake(() => {});
mouseenter.call(context);
2018-12-13 13:39:08 +05:30
2018-10-15 14:42:47 +05:30
expect(spy).not.toHaveBeenCalled();
});
});
});