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

mutation_spec.js « list « store « error_tracking « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e6505e13cd483b8e63244073ef965da74f9970f (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import mutations from '~/error_tracking/store/list/mutations';
import * as types from '~/error_tracking/store/list/mutation_types';
import { useLocalStorageSpy } from 'helpers/local_storage_helper';

const ADD_RECENT_SEARCH = mutations[types.ADD_RECENT_SEARCH];
const CLEAR_RECENT_SEARCHES = mutations[types.CLEAR_RECENT_SEARCHES];
const LOAD_RECENT_SEARCHES = mutations[types.LOAD_RECENT_SEARCHES];

describe('Error tracking mutations', () => {
  describe('SET_ERRORS', () => {
    let state;

    beforeEach(() => {
      state = { errors: [] };
    });

    it('camelizes response', () => {
      const errors = [
        {
          title: 'the title',
          external_url: 'localhost:3456',
          count: 100,
          userCount: 10,
        },
      ];

      mutations[types.SET_ERRORS](state, errors);

      expect(state).toEqual({
        errors: [
          {
            title: 'the title',
            externalUrl: 'localhost:3456',
            count: 100,
            userCount: 10,
          },
        ],
      });
    });
  });

  describe('recent searches', () => {
    useLocalStorageSpy();
    let state;

    beforeEach(() => {
      state = {
        indexPath: '/project/errors.json',
        recentSearches: [],
      };
    });

    describe('ADD_RECENT_SEARCH', () => {
      it('adds search queries to recentSearches and localStorage', () => {
        ADD_RECENT_SEARCH(state, 'my issue');

        expect(state.recentSearches).toEqual(['my issue']);
        expect(localStorage.setItem).toHaveBeenCalledWith(
          'recent-searches/project/errors.json',
          '["my issue"]',
        );
      });

      it('does not add empty searches', () => {
        ADD_RECENT_SEARCH(state, '');

        expect(state.recentSearches).toEqual([]);
        expect(localStorage.setItem).not.toHaveBeenCalled();
      });

      it('adds new queries to start of the list', () => {
        state.recentSearches = ['previous', 'searches'];

        ADD_RECENT_SEARCH(state, 'new search');

        expect(state.recentSearches).toEqual(['new search', 'previous', 'searches']);
      });

      it('limits recentSearches to 5 items', () => {
        state.recentSearches = [1, 2, 3, 4, 5];

        ADD_RECENT_SEARCH(state, 'new search');

        expect(state.recentSearches).toEqual(['new search', 1, 2, 3, 4]);
      });

      it('does not add same search query twice', () => {
        state.recentSearches = ['already', 'searched'];

        ADD_RECENT_SEARCH(state, 'searched');

        expect(state.recentSearches).toEqual(['searched', 'already']);
      });
    });

    describe('CLEAR_RECENT_SEARCHES', () => {
      it('clears recentSearches and localStorage', () => {
        state.recentSearches = ['first', 'second'];

        CLEAR_RECENT_SEARCHES(state);

        expect(state.recentSearches).toEqual([]);
        expect(localStorage.removeItem).toHaveBeenCalledWith('recent-searches/project/errors.json');
      });
    });

    describe('LOAD_RECENT_SEARCHES', () => {
      it('loads recent searches from localStorage', () => {
        jest.spyOn(window.localStorage, 'getItem').mockReturnValue('["first", "second"]');

        LOAD_RECENT_SEARCHES(state);

        expect(state.recentSearches).toEqual(['first', 'second']);
        expect(localStorage.getItem).toHaveBeenCalledWith('recent-searches/project/errors.json');
      });
    });
  });
});