summaryrefslogtreecommitdiff
path: root/spec/frontend/flash_spec.js
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/flash_spec.js')
-rw-r--r--spec/frontend/flash_spec.js204
1 files changed, 1 insertions, 203 deletions
diff --git a/spec/frontend/flash_spec.js b/spec/frontend/flash_spec.js
index ade36cd1637..2f0a52a9884 100644
--- a/spec/frontend/flash_spec.js
+++ b/spec/frontend/flash_spec.js
@@ -1,9 +1,8 @@
import * as Sentry from '@sentry/browser';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
-import createFlash, {
+import {
hideFlash,
addDismissFlashClickListener,
- FLASH_TYPES,
FLASH_CLOSED_EVENT,
createAlert,
VARIANT_WARNING,
@@ -340,207 +339,6 @@ describe('Flash', () => {
});
});
- describe('createFlash', () => {
- const message = 'test';
- const fadeTransition = false;
- const addBodyClass = true;
- const defaultParams = {
- message,
- actionConfig: null,
- fadeTransition,
- addBodyClass,
- };
-
- describe('no flash-container', () => {
- it('does not add to the DOM', () => {
- const flashEl = createFlash({ message });
-
- expect(flashEl).toBeNull();
-
- expect(document.querySelector('.flash-alert')).toBeNull();
- });
- });
-
- describe('with flash-container', () => {
- beforeEach(() => {
- setHTMLFixture(
- '<div class="content-wrapper js-content-wrapper"><div class="flash-container"></div></div>',
- );
- });
-
- afterEach(() => {
- resetHTMLFixture();
- });
-
- it('adds flash alert element into the document by default', () => {
- createFlash({ ...defaultParams });
-
- expect(document.querySelector('.flash-container .flash-alert')).not.toBeNull();
- expect(document.body.className).toContain('flash-shown');
- });
-
- it('adds flash of a warning type', () => {
- createFlash({ ...defaultParams, type: FLASH_TYPES.WARNING });
-
- expect(document.querySelector('.flash-container .flash-warning')).not.toBeNull();
- expect(document.body.className).toContain('flash-shown');
- });
-
- it('escapes text', () => {
- createFlash({ ...defaultParams, message: '<script>alert("a")</script>' });
-
- const html = document.querySelector('.flash-text').innerHTML;
-
- expect(html).toContain('&lt;script&gt;alert("a")&lt;/script&gt;');
- expect(html).not.toContain('<script>alert("a")</script>');
- });
-
- it('adds flash into specified parent', () => {
- createFlash({ ...defaultParams, parent: document.querySelector('.content-wrapper') });
-
- expect(document.querySelector('.content-wrapper .flash-alert')).not.toBeNull();
- expect(document.querySelector('.content-wrapper').innerText.trim()).toEqual(message);
- });
-
- it('adds container classes when inside content-wrapper', () => {
- createFlash(defaultParams);
-
- expect(document.querySelector('.flash-text').className).toBe('flash-text');
- expect(document.querySelector('.content-wrapper').innerText.trim()).toEqual(message);
- });
-
- it('does not add container when outside of content-wrapper', () => {
- document.querySelector('.content-wrapper').className = 'js-content-wrapper';
- createFlash(defaultParams);
-
- expect(document.querySelector('.flash-text').className.trim()).toContain('flash-text');
- });
-
- it('removes element after clicking', () => {
- createFlash({ ...defaultParams });
-
- document.querySelector('.flash-alert .js-close-icon').click();
-
- expect(document.querySelector('.flash-alert')).toBeNull();
-
- expect(document.body.className).not.toContain('flash-shown');
- });
-
- it('does not capture error using Sentry', () => {
- createFlash({ ...defaultParams, captureError: false, error: new Error('Error!') });
-
- expect(Sentry.captureException).not.toHaveBeenCalled();
- });
-
- it('captures error using Sentry', () => {
- createFlash({ ...defaultParams, captureError: true, error: new Error('Error!') });
-
- expect(Sentry.captureException).toHaveBeenCalledWith(expect.any(Error));
- expect(Sentry.captureException).toHaveBeenCalledWith(
- expect.objectContaining({
- message: 'Error!',
- }),
- );
- });
-
- describe('with actionConfig', () => {
- const findFlashAction = () => document.querySelector('.flash-container .flash-action');
-
- it('adds action link', () => {
- createFlash({
- ...defaultParams,
- actionConfig: {
- title: 'test',
- },
- });
-
- expect(findFlashAction()).not.toBeNull();
- });
-
- it('creates link with href', () => {
- createFlash({
- ...defaultParams,
- actionConfig: {
- href: 'testing',
- title: 'test',
- },
- });
-
- const action = findFlashAction();
-
- expect(action.href).toBe(`${window.location}testing`);
- expect(action.textContent.trim()).toBe('test');
- });
-
- it('uses hash as href when no href is present', () => {
- createFlash({
- ...defaultParams,
- actionConfig: {
- title: 'test',
- },
- });
-
- expect(findFlashAction().href).toBe(`${window.location}#`);
- });
-
- it('adds role when no href is present', () => {
- createFlash({
- ...defaultParams,
- actionConfig: {
- title: 'test',
- },
- });
-
- expect(findFlashAction().getAttribute('role')).toBe('button');
- });
-
- it('escapes the title text', () => {
- createFlash({
- ...defaultParams,
- actionConfig: {
- title: '<script>alert("a")</script>',
- },
- });
-
- const html = findFlashAction().innerHTML;
-
- expect(html).toContain('&lt;script&gt;alert("a")&lt;/script&gt;');
- expect(html).not.toContain('<script>alert("a")</script>');
- });
-
- it('calls actionConfig clickHandler on click', () => {
- const clickHandler = jest.fn();
-
- createFlash({
- ...defaultParams,
- actionConfig: {
- title: 'test',
- clickHandler,
- },
- });
-
- findFlashAction().click();
-
- expect(clickHandler).toHaveBeenCalled();
- });
- });
-
- describe('additional behavior', () => {
- describe('close', () => {
- it('clicks the close icon', () => {
- const flash = createFlash({ ...defaultParams });
- const close = document.querySelector('.flash-alert .js-close-icon');
-
- jest.spyOn(close, 'click');
- flash.close();
-
- expect(close.click.mock.calls.length).toBe(1);
- });
- });
- });
- });
- });
-
describe('addDismissFlashClickListener', () => {
let el;