Welcome to mirror list, hosted at ThFree Co, Russian Federation.

show_alert_from_local_storage_spec.js « local_storage_alert « runner « ci « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03908891cfdb513e6923f22284755194f7a58f62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import AccessorUtilities from '~/lib/utils/accessor';
import { showAlertFromLocalStorage } from '~/ci/runner/local_storage_alert/show_alert_from_local_storage';
import { LOCAL_STORAGE_ALERT_KEY } from '~/ci/runner/local_storage_alert/constants';
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import { createAlert } from '~/flash';

jest.mock('~/flash');

describe('showAlertFromLocalStorage', () => {
  useLocalStorageSpy();

  beforeEach(() => {
    jest.spyOn(AccessorUtilities, 'canUseLocalStorage').mockReturnValue(true);
  });

  it('retrieves message from local storage and displays it', async () => {
    const mockAlert = { message: 'Message!' };

    localStorage.getItem.mockReturnValueOnce(JSON.stringify(mockAlert));

    await showAlertFromLocalStorage();

    expect(createAlert).toHaveBeenCalledTimes(1);
    expect(createAlert).toHaveBeenCalledWith(mockAlert);

    expect(localStorage.removeItem).toHaveBeenCalledTimes(1);
    expect(localStorage.removeItem).toHaveBeenCalledWith(LOCAL_STORAGE_ALERT_KEY);
  });

  it.each(['not a json string', null])('does not fail when stored message is %o', async (item) => {
    localStorage.getItem.mockReturnValueOnce(item);

    await showAlertFromLocalStorage();

    expect(createAlert).not.toHaveBeenCalled();

    expect(localStorage.removeItem).toHaveBeenCalledTimes(1);
    expect(localStorage.removeItem).toHaveBeenCalledWith(LOCAL_STORAGE_ALERT_KEY);
  });
});