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

226 lines
6.9 KiB
JavaScript
Raw Normal View History

2019-07-07 11:18:12 +05:30
import MockAdapter from 'axios-mock-adapter';
2021-10-27 15:23:28 +05:30
import { useMockLocationHelper } from 'helpers/mock_window_location_helper';
2020-06-23 00:09:42 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2021-09-30 23:02:18 +05:30
import createFlash from '~/flash';
2019-07-07 11:18:12 +05:30
import axios from '~/lib/utils/axios_utils';
import PersistentUserCallout from '~/persistent_user_callout';
2020-06-23 00:09:42 +05:30
jest.mock('~/flash');
2019-07-07 11:18:12 +05:30
describe('PersistentUserCallout', () => {
const dismissEndpoint = '/dismiss';
const featureName = 'feature';
function createFixture() {
const fixture = document.createElement('div');
fixture.innerHTML = `
<div
class="container"
data-dismiss-endpoint="${dismissEndpoint}"
data-feature-id="${featureName}"
>
<button type="button" class="js-close"></button>
</div>
`;
return fixture;
}
2019-10-12 21:52:04 +05:30
function createDeferredLinkFixture() {
const fixture = document.createElement('div');
fixture.innerHTML = `
<div
class="container"
data-dismiss-endpoint="${dismissEndpoint}"
data-feature-id="${featureName}"
data-defer-links="true"
>
<button type="button" class="js-close"></button>
<a href="/somewhere-pleasant" target="_blank" class="deferred-link">A link</a>
<a href="/somewhere-else" target="_blank" class="normal-link">Another link</a>
</div>
`;
return fixture;
}
2020-07-28 23:09:34 +05:30
function createFollowLinkFixture() {
const fixture = document.createElement('div');
fixture.innerHTML = `
<ul>
<li
class="container"
data-dismiss-endpoint="${dismissEndpoint}"
data-feature-id="${featureName}"
>
<a class="js-follow-link" href="/somewhere-pleasant">A Link</a>
</li>
</ul>
`;
return fixture;
}
2019-07-07 11:18:12 +05:30
describe('dismiss', () => {
let button;
let mockAxios;
let persistentUserCallout;
beforeEach(() => {
const fixture = createFixture();
const container = fixture.querySelector('.container');
button = fixture.querySelector('.js-close');
mockAxios = new MockAdapter(axios);
persistentUserCallout = new PersistentUserCallout(container);
2020-06-23 00:09:42 +05:30
jest.spyOn(persistentUserCallout.container, 'remove').mockImplementation(() => {});
2019-07-07 11:18:12 +05:30
});
afterEach(() => {
mockAxios.restore();
});
2020-06-23 00:09:42 +05:30
it('POSTs endpoint and removes container when clicking close', () => {
2019-07-07 11:18:12 +05:30
mockAxios.onPost(dismissEndpoint).replyOnce(200);
button.click();
2020-06-23 00:09:42 +05:30
return waitForPromises().then(() => {
expect(persistentUserCallout.container.remove).toHaveBeenCalled();
expect(mockAxios.history.post[0].data).toBe(JSON.stringify({ feature_name: featureName }));
});
2019-07-07 11:18:12 +05:30
});
2020-06-23 00:09:42 +05:30
it('invokes Flash when the dismiss request fails', () => {
2019-07-07 11:18:12 +05:30
mockAxios.onPost(dismissEndpoint).replyOnce(500);
button.click();
2020-06-23 00:09:42 +05:30
return waitForPromises().then(() => {
expect(persistentUserCallout.container.remove).not.toHaveBeenCalled();
2021-09-30 23:02:18 +05:30
expect(createFlash).toHaveBeenCalledWith({
message: 'An error occurred while dismissing the alert. Refresh the page and try again.',
});
2020-06-23 00:09:42 +05:30
});
2019-07-07 11:18:12 +05:30
});
});
2019-10-12 21:52:04 +05:30
describe('deferred links', () => {
let button;
let deferredLink;
let normalLink;
let mockAxios;
let persistentUserCallout;
let windowSpy;
beforeEach(() => {
const fixture = createDeferredLinkFixture();
const container = fixture.querySelector('.container');
button = fixture.querySelector('.js-close');
deferredLink = fixture.querySelector('.deferred-link');
normalLink = fixture.querySelector('.normal-link');
mockAxios = new MockAdapter(axios);
persistentUserCallout = new PersistentUserCallout(container);
2020-06-23 00:09:42 +05:30
jest.spyOn(persistentUserCallout.container, 'remove').mockImplementation(() => {});
windowSpy = jest.spyOn(window, 'open').mockImplementation(() => {});
2019-10-12 21:52:04 +05:30
});
afterEach(() => {
mockAxios.restore();
});
2020-06-23 00:09:42 +05:30
it('defers loading of a link until callout is dismissed', () => {
2019-10-12 21:52:04 +05:30
const { href, target } = deferredLink;
mockAxios.onPost(dismissEndpoint).replyOnce(200);
deferredLink.click();
2020-06-23 00:09:42 +05:30
return waitForPromises().then(() => {
expect(windowSpy).toHaveBeenCalledWith(href, target);
expect(persistentUserCallout.container.remove).toHaveBeenCalled();
expect(mockAxios.history.post[0].data).toBe(JSON.stringify({ feature_name: featureName }));
});
2019-10-12 21:52:04 +05:30
});
2020-06-23 00:09:42 +05:30
it('does not dismiss callout on non-deferred links', () => {
2019-10-12 21:52:04 +05:30
normalLink.click();
2020-06-23 00:09:42 +05:30
return waitForPromises().then(() => {
expect(windowSpy).not.toHaveBeenCalled();
expect(persistentUserCallout.container.remove).not.toHaveBeenCalled();
});
2019-10-12 21:52:04 +05:30
});
2020-06-23 00:09:42 +05:30
it('does not follow link when notification is closed', () => {
2019-10-12 21:52:04 +05:30
mockAxios.onPost(dismissEndpoint).replyOnce(200);
button.click();
2020-06-23 00:09:42 +05:30
return waitForPromises().then(() => {
expect(windowSpy).not.toHaveBeenCalled();
expect(persistentUserCallout.container.remove).toHaveBeenCalled();
});
2019-10-12 21:52:04 +05:30
});
});
2020-07-28 23:09:34 +05:30
describe('follow links', () => {
let link;
let mockAxios;
let persistentUserCallout;
2021-10-27 15:23:28 +05:30
useMockLocationHelper();
2020-07-28 23:09:34 +05:30
beforeEach(() => {
const fixture = createFollowLinkFixture();
const container = fixture.querySelector('.container');
link = fixture.querySelector('.js-follow-link');
mockAxios = new MockAdapter(axios);
persistentUserCallout = new PersistentUserCallout(container);
jest.spyOn(persistentUserCallout.container, 'remove').mockImplementation(() => {});
});
afterEach(() => {
mockAxios.restore();
});
it('uses a link to trigger callout and defers following until callout is finished', () => {
const { href } = link;
mockAxios.onPost(dismissEndpoint).replyOnce(200);
link.click();
return waitForPromises().then(() => {
expect(window.location.assign).toBeCalledWith(href);
expect(mockAxios.history.post[0].data).toBe(JSON.stringify({ feature_name: featureName }));
expect(persistentUserCallout.container.remove).not.toHaveBeenCalled();
});
});
it('invokes Flash when the dismiss request fails', () => {
mockAxios.onPost(dismissEndpoint).replyOnce(500);
link.click();
return waitForPromises().then(() => {
expect(window.location.assign).not.toHaveBeenCalled();
2021-09-30 23:02:18 +05:30
expect(createFlash).toHaveBeenCalledWith({
message:
'An error occurred while acknowledging the notification. Refresh the page and try again.',
});
2020-07-28 23:09:34 +05:30
});
});
});
2019-07-07 11:18:12 +05:30
describe('factory', () => {
it('returns an instance of PersistentUserCallout with the provided container property', () => {
const fixture = createFixture();
expect(PersistentUserCallout.factory(fixture) instanceof PersistentUserCallout).toBe(true);
});
it('returns undefined if container is falsey', () => {
expect(PersistentUserCallout.factory()).toBe(undefined);
});
});
});