2022-01-24 10:11:33 -05:00
|
|
|
import { nextTick } from 'vue';
|
2022-05-13 08:08:49 -04:00
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2020-01-30 13:08:57 -05:00
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2020-08-17 17:09:56 -04:00
|
|
|
import initVueAlerts from '~/vue_alerts';
|
2020-01-30 13:08:57 -05:00
|
|
|
|
|
|
|
describe('VueAlerts', () => {
|
|
|
|
const alerts = [
|
|
|
|
{
|
|
|
|
title: 'Lorem',
|
|
|
|
html: 'Lorem <strong>Ipsum</strong>',
|
|
|
|
dismissible: true,
|
|
|
|
primaryButtonText: 'Okay!',
|
|
|
|
primaryButtonLink: `${TEST_HOST}/okay`,
|
|
|
|
variant: 'tip',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Hello',
|
|
|
|
html: 'Hello <strong>World</strong>',
|
|
|
|
dismissible: false,
|
|
|
|
primaryButtonText: 'No!',
|
|
|
|
primaryButtonLink: `${TEST_HOST}/no`,
|
|
|
|
variant: 'info',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
setHTMLFixture(
|
|
|
|
alerts
|
|
|
|
.map(
|
2020-12-23 16:10:24 -05:00
|
|
|
(x) => `
|
2021-06-24 14:07:15 -04:00
|
|
|
<div class="js-vue-alert"
|
|
|
|
data-dismissible="${x.dismissible}"
|
2020-01-30 13:08:57 -05:00
|
|
|
data-title="${x.title}"
|
|
|
|
data-primary-button-text="${x.primaryButtonText}"
|
|
|
|
data-primary-button-link="${x.primaryButtonLink}"
|
|
|
|
data-variant="${x.variant}">${x.html}</div>
|
|
|
|
`,
|
|
|
|
)
|
|
|
|
.join('\n'),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2020-01-30 13:08:57 -05:00
|
|
|
const findJsHooks = () => document.querySelectorAll('.js-vue-alert');
|
|
|
|
const findAlerts = () => document.querySelectorAll('.gl-alert');
|
2021-04-02 17:09:22 -04:00
|
|
|
const findAlertDismiss = (alert) => alert.querySelector('.gl-dismiss-btn');
|
2020-01-30 13:08:57 -05:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const serializeAlert = (alert) => ({
|
2020-01-30 13:08:57 -05:00
|
|
|
title: alert.querySelector('.gl-alert-title').textContent.trim(),
|
|
|
|
html: alert.querySelector('.gl-alert-body div').innerHTML,
|
2021-04-02 17:09:22 -04:00
|
|
|
dismissible: Boolean(alert.querySelector('.gl-dismiss-btn')),
|
2020-01-30 13:08:57 -05:00
|
|
|
primaryButtonText: alert.querySelector('.gl-alert-action').textContent.trim(),
|
|
|
|
primaryButtonLink: alert.querySelector('.gl-alert-action').href,
|
2021-04-02 17:09:22 -04:00
|
|
|
variant: [...alert.classList]
|
|
|
|
.find((x) => x.match(/gl-alert-(?!not-dismissible)/))
|
|
|
|
.replace('gl-alert-', ''),
|
2020-01-30 13:08:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('starts with only JsHooks', () => {
|
|
|
|
expect(findJsHooks().length).toEqual(alerts.length);
|
|
|
|
expect(findAlerts().length).toEqual(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when mounted', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
initVueAlerts();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('replaces JsHook with GlAlert', () => {
|
|
|
|
expect(findJsHooks().length).toEqual(0);
|
|
|
|
expect(findAlerts().length).toEqual(alerts.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes along props to gl-alert', () => {
|
|
|
|
expect([...findAlerts()].map(serializeAlert)).toEqual(alerts);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when dismissed', () => {
|
2022-01-24 10:11:33 -05:00
|
|
|
beforeEach(async () => {
|
2020-01-30 13:08:57 -05:00
|
|
|
findAlertDismiss(findAlerts()[0]).click();
|
2022-01-24 10:11:33 -05:00
|
|
|
await nextTick();
|
2020-01-30 13:08:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('hides the alert', () => {
|
|
|
|
expect(findAlerts().length).toEqual(alerts.length - 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|