Commit c84d184d authored by Nicolò Maria Mezzopera's avatar Nicolò Maria Mezzopera Committed by Denys Mishunov

Migrate feature highlight to jest

- feature_highlight_helper
- feature_highlight
parent 5407ed81
import $ from 'jquery'; import $ from 'jquery';
import MockAdapter from 'axios-mock-adapter';
import getSetTimeoutPromise from 'spec/helpers/set_timeout_promise_helper';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { getSelector, dismiss, inserted } from '~/feature_highlight/feature_highlight_helper'; import { getSelector, dismiss, inserted } from '~/feature_highlight/feature_highlight_helper';
import { togglePopover } from '~/shared/popover'; import { togglePopover } from '~/shared/popover';
...@@ -17,34 +15,23 @@ describe('feature highlight helper', () => { ...@@ -17,34 +15,23 @@ describe('feature highlight helper', () => {
}); });
describe('dismiss', () => { describe('dismiss', () => {
let mock;
const context = { const context = {
hide: () => {}, hide: () => {},
attr: () => '/-/callouts/dismiss', attr: () => '/-/callouts/dismiss',
}; };
beforeEach(() => { beforeEach(() => {
mock = new MockAdapter(axios); jest.spyOn(axios, 'post').mockResolvedValue();
jest.spyOn(togglePopover, 'call').mockImplementation(() => {});
spyOn(togglePopover, 'call').and.callFake(() => {}); jest.spyOn(context, 'hide').mockImplementation(() => {});
spyOn(context, 'hide').and.callFake(() => {});
dismiss.call(context); dismiss.call(context);
}); });
afterEach(() => { it('calls persistent dismissal endpoint', () => {
mock.restore(); expect(axios.post).toHaveBeenCalledWith(
}); '/-/callouts/dismiss',
expect.objectContaining({ feature_name: undefined }),
it('calls persistent dismissal endpoint', done => { );
const spy = jasmine.createSpy('dismiss-endpoint-hit');
mock.onPost('/-/callouts/dismiss').reply(spy);
getSetTimeoutPromise()
.then(() => {
expect(spy).toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
}); });
it('calls hide popover', () => { it('calls hide popover', () => {
...@@ -65,7 +52,7 @@ describe('feature highlight helper', () => { ...@@ -65,7 +52,7 @@ describe('feature highlight helper', () => {
}, },
}; };
spyOn($.fn, 'on').and.callFake(event => { jest.spyOn($.fn, 'on').mockImplementation(event => {
expect(event).toEqual('click'); expect(event).toEqual('click');
done(); done();
}); });
......
...@@ -3,34 +3,20 @@ import domContentLoaded from '~/feature_highlight/feature_highlight_options'; ...@@ -3,34 +3,20 @@ import domContentLoaded from '~/feature_highlight/feature_highlight_options';
describe('feature highlight options', () => { describe('feature highlight options', () => {
describe('domContentLoaded', () => { describe('domContentLoaded', () => {
it('should not call highlightFeatures when breakpoint is xs', () => { it.each`
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('xs'); breakPoint | shouldCall
${'xs'} | ${false}
expect(domContentLoaded()).toBe(false); ${'sm'} | ${false}
}); ${'md'} | ${false}
${'lg'} | ${false}
it('should not call highlightFeatures when breakpoint is sm', () => { ${'xl'} | ${true}
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('sm'); `(
'when breakpoint is $breakPoint should call highlightFeatures is $shouldCall',
expect(domContentLoaded()).toBe(false); ({ breakPoint, shouldCall }) => {
}); jest.spyOn(bp, 'getBreakpointSize').mockReturnValue(breakPoint);
it('should not call highlightFeatures when breakpoint is md', () => { expect(domContentLoaded()).toBe(shouldCall);
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('md'); },
);
expect(domContentLoaded()).toBe(false);
});
it('should not call highlightFeatures when breakpoint is not xl', () => {
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('lg');
expect(domContentLoaded()).toBe(false);
});
it('should call highlightFeatures when breakpoint is xl', () => {
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('xl');
expect(domContentLoaded()).toBe(true);
});
}); });
}); });
...@@ -4,6 +4,8 @@ import * as featureHighlight from '~/feature_highlight/feature_highlight'; ...@@ -4,6 +4,8 @@ import * as featureHighlight from '~/feature_highlight/feature_highlight';
import * as popover from '~/shared/popover'; import * as popover from '~/shared/popover';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
jest.mock('~/shared/popover');
describe('feature highlight', () => { describe('feature highlight', () => {
beforeEach(() => { beforeEach(() => {
setFixtures(` setFixtures(`
...@@ -28,7 +30,7 @@ describe('feature highlight', () => { ...@@ -28,7 +30,7 @@ describe('feature highlight', () => {
beforeEach(() => { beforeEach(() => {
mock = new MockAdapter(axios); mock = new MockAdapter(axios);
mock.onGet('/test').reply(200); mock.onGet('/test').reply(200);
spyOn(window, 'addEventListener'); jest.spyOn(window, 'addEventListener').mockImplementation(() => {});
featureHighlight.setupFeatureHighlightPopover('test', 0); featureHighlight.setupFeatureHighlightPopover('test', 0);
}); });
...@@ -44,27 +46,21 @@ describe('feature highlight', () => { ...@@ -44,27 +46,21 @@ describe('feature highlight', () => {
}); });
it('setup mouseenter', () => { it('setup mouseenter', () => {
const toggleSpy = spyOn(popover.togglePopover, 'call');
$(selector).trigger('mouseenter'); $(selector).trigger('mouseenter');
expect(toggleSpy).toHaveBeenCalledWith(jasmine.any(Object), true); expect(popover.mouseenter).toHaveBeenCalledWith(expect.any(Object));
}); });
it('setup debounced mouseleave', done => { it('setup debounced mouseleave', () => {
const toggleSpy = spyOn(popover.togglePopover, 'call');
$(selector).trigger('mouseleave'); $(selector).trigger('mouseleave');
// Even though we've set the debounce to 0ms, setTimeout is needed for the debounce expect(popover.debouncedMouseleave).toHaveBeenCalled();
setTimeout(() => {
expect(toggleSpy).toHaveBeenCalledWith(jasmine.any(Object), false);
done();
}, 0);
}); });
it('setup show.bs.popover', () => { it('setup show.bs.popover', () => {
$(selector).trigger('show.bs.popover'); $(selector).trigger('show.bs.popover');
expect(window.addEventListener).toHaveBeenCalledWith('scroll', jasmine.any(Function), { expect(window.addEventListener).toHaveBeenCalledWith('scroll', expect.any(Function), {
once: true, once: true,
}); });
}); });
...@@ -72,23 +68,6 @@ describe('feature highlight', () => { ...@@ -72,23 +68,6 @@ describe('feature highlight', () => {
it('removes disabled attribute', () => { it('removes disabled attribute', () => {
expect($('.js-feature-highlight').is(':disabled')).toEqual(false); expect($('.js-feature-highlight').is(':disabled')).toEqual(false);
}); });
it('displays popover', () => {
expect(document.querySelector(selector).getAttribute('aria-describedby')).toBeFalsy();
$(selector).trigger('mouseenter');
expect(document.querySelector(selector).getAttribute('aria-describedby')).toBeTruthy();
});
it('toggles when clicked', () => {
$(selector).trigger('mouseenter');
const popoverId = $(selector).attr('aria-describedby');
const toggleSpy = spyOn(popover.togglePopover, 'call');
$(`#${popoverId} .dismiss-feature-highlight`).click();
expect(toggleSpy).toHaveBeenCalled();
});
}); });
describe('findHighestPriorityFeature', () => { describe('findHighestPriorityFeature', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment