2019-02-19 16:26:07 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-08-02 14:08:48 -04:00
|
|
|
import { useMockLocationHelper } from 'helpers/mock_window_location_helper';
|
2020-05-20 08:07:52 -04:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2021-06-20 23:10:27 -04:00
|
|
|
import createFlash from '~/flash';
|
2019-02-19 16:26:07 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import PersistentUserCallout from '~/persistent_user_callout';
|
2020-05-20 08:07:52 -04:00
|
|
|
|
|
|
|
jest.mock('~/flash');
|
2019-02-19 16:26:07 -05:00
|
|
|
|
|
|
|
describe('PersistentUserCallout', () => {
|
|
|
|
const dismissEndpoint = '/dismiss';
|
|
|
|
const featureName = 'feature';
|
2022-02-04 13:17:50 -05:00
|
|
|
const groupId = '5';
|
2019-02-19 16:26:07 -05:00
|
|
|
|
|
|
|
function createFixture() {
|
|
|
|
const fixture = document.createElement('div');
|
|
|
|
fixture.innerHTML = `
|
|
|
|
<div
|
|
|
|
class="container"
|
|
|
|
data-dismiss-endpoint="${dismissEndpoint}"
|
|
|
|
data-feature-id="${featureName}"
|
2022-02-04 13:17:50 -05:00
|
|
|
data-group-id="${groupId}"
|
2019-02-19 16:26:07 -05:00
|
|
|
>
|
2022-03-07 13:19:30 -05:00
|
|
|
<button type="button" class="js-close js-close-primary"></button>
|
|
|
|
<button type="button" class="js-close js-close-secondary"></button>
|
2019-02-19 16:26:07 -05:00
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
|
|
|
return fixture;
|
|
|
|
}
|
|
|
|
|
2019-08-05 05:00:34 -04:00
|
|
|
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-06-26 08:08:51 -04:00
|
|
|
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-02-19 16:26:07 -05:00
|
|
|
describe('dismiss', () => {
|
2022-03-07 13:19:30 -05:00
|
|
|
const buttons = {};
|
2019-02-19 16:26:07 -05:00
|
|
|
let mockAxios;
|
|
|
|
let persistentUserCallout;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
const fixture = createFixture();
|
|
|
|
const container = fixture.querySelector('.container');
|
2022-03-07 13:19:30 -05:00
|
|
|
buttons.primary = fixture.querySelector('.js-close-primary');
|
|
|
|
buttons.secondary = fixture.querySelector('.js-close-secondary');
|
2019-02-19 16:26:07 -05:00
|
|
|
mockAxios = new MockAdapter(axios);
|
|
|
|
persistentUserCallout = new PersistentUserCallout(container);
|
2020-05-20 08:07:52 -04:00
|
|
|
jest.spyOn(persistentUserCallout.container, 'remove').mockImplementation(() => {});
|
2019-02-19 16:26:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mockAxios.restore();
|
|
|
|
});
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
it.each`
|
|
|
|
button
|
|
|
|
${'primary'}
|
|
|
|
${'secondary'}
|
|
|
|
`('POSTs endpoint and removes container when clicking $button close', async ({ button }) => {
|
2019-02-19 16:26:07 -05:00
|
|
|
mockAxios.onPost(dismissEndpoint).replyOnce(200);
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
buttons[button].click();
|
2019-02-19 16:26:07 -05:00
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(persistentUserCallout.container.remove).toHaveBeenCalled();
|
|
|
|
expect(mockAxios.history.post[0].data).toBe(
|
|
|
|
JSON.stringify({ feature_name: featureName, group_id: groupId }),
|
|
|
|
);
|
2019-02-19 16:26:07 -05:00
|
|
|
});
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
it('invokes Flash when the dismiss request fails', async () => {
|
2019-02-19 16:26:07 -05:00
|
|
|
mockAxios.onPost(dismissEndpoint).replyOnce(500);
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
buttons.primary.click();
|
2019-02-19 16:26:07 -05:00
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(persistentUserCallout.container.remove).not.toHaveBeenCalled();
|
|
|
|
expect(createFlash).toHaveBeenCalledWith({
|
|
|
|
message: 'An error occurred while dismissing the alert. Refresh the page and try again.',
|
2020-05-20 08:07:52 -04:00
|
|
|
});
|
2019-02-19 16:26:07 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-05 05:00:34 -04:00
|
|
|
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-05-20 08:07:52 -04:00
|
|
|
jest.spyOn(persistentUserCallout.container, 'remove').mockImplementation(() => {});
|
|
|
|
windowSpy = jest.spyOn(window, 'open').mockImplementation(() => {});
|
2019-08-05 05:00:34 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mockAxios.restore();
|
|
|
|
});
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
it('defers loading of a link until callout is dismissed', async () => {
|
2019-08-05 05:00:34 -04:00
|
|
|
const { href, target } = deferredLink;
|
|
|
|
mockAxios.onPost(dismissEndpoint).replyOnce(200);
|
|
|
|
|
|
|
|
deferredLink.click();
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(windowSpy).toHaveBeenCalledWith(href, target);
|
|
|
|
expect(persistentUserCallout.container.remove).toHaveBeenCalled();
|
|
|
|
expect(mockAxios.history.post[0].data).toBe(JSON.stringify({ feature_name: featureName }));
|
2019-08-05 05:00:34 -04:00
|
|
|
});
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
it('does not dismiss callout on non-deferred links', async () => {
|
2019-08-05 05:00:34 -04:00
|
|
|
normalLink.click();
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(windowSpy).not.toHaveBeenCalled();
|
|
|
|
expect(persistentUserCallout.container.remove).not.toHaveBeenCalled();
|
2019-08-05 05:00:34 -04:00
|
|
|
});
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
it('does not follow link when notification is closed', async () => {
|
2019-08-05 05:00:34 -04:00
|
|
|
mockAxios.onPost(dismissEndpoint).replyOnce(200);
|
|
|
|
|
|
|
|
button.click();
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(windowSpy).not.toHaveBeenCalled();
|
|
|
|
expect(persistentUserCallout.container.remove).toHaveBeenCalled();
|
2019-08-05 05:00:34 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-26 08:08:51 -04:00
|
|
|
describe('follow links', () => {
|
|
|
|
let link;
|
|
|
|
let mockAxios;
|
|
|
|
let persistentUserCallout;
|
|
|
|
|
2021-08-02 14:08:48 -04:00
|
|
|
useMockLocationHelper();
|
|
|
|
|
2020-06-26 08:08:51 -04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
it('uses a link to trigger callout and defers following until callout is finished', async () => {
|
2020-06-26 08:08:51 -04:00
|
|
|
const { href } = link;
|
|
|
|
mockAxios.onPost(dismissEndpoint).replyOnce(200);
|
|
|
|
|
|
|
|
link.click();
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(window.location.assign).toBeCalledWith(href);
|
|
|
|
expect(persistentUserCallout.container.remove).not.toHaveBeenCalled();
|
|
|
|
expect(mockAxios.history.post[0].data).toBe(JSON.stringify({ feature_name: featureName }));
|
2020-06-26 08:08:51 -04:00
|
|
|
});
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
it('invokes Flash when the dismiss request fails', async () => {
|
2020-06-26 08:08:51 -04:00
|
|
|
mockAxios.onPost(dismissEndpoint).replyOnce(500);
|
|
|
|
|
|
|
|
link.click();
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(window.location.assign).not.toHaveBeenCalled();
|
|
|
|
expect(createFlash).toHaveBeenCalledWith({
|
|
|
|
message:
|
|
|
|
'An error occurred while acknowledging the notification. Refresh the page and try again.',
|
2020-06-26 08:08:51 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-02-19 16:26:07 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|