summaryrefslogtreecommitdiff
path: root/spec/frontend/error_tracking/store/list/mutation_spec.js
blob: a326a6c55c0234c2abea9e415a669197eef221d6 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import mutations from '~/error_tracking/store/list/mutations';
import * as types from '~/error_tracking/store/list/mutation_types';

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];
const REMOVE_IGNORED_RESOLVED_ERRORS = mutations[types.REMOVE_IGNORED_RESOLVED_ERRORS];
const SET_STATUS_FILTER = mutations[types.SET_STATUS_FILTER];

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');
      });
    });

    describe('REMOVE_IGNORED_RESOLVED_ERRORS', () => {
      it('removes ignored or resolved errors from list', () => {
        state.errors = [
          {
            id: 1,
            status: 'unresolved',
          },
          {
            id: 2,
            status: 'ignored',
          },
          {
            id: 3,
            status: 'unresolved',
          },
        ];
        const ignoredError = state.errors[2].id;

        REMOVE_IGNORED_RESOLVED_ERRORS(state, ignoredError);

        expect(state.errors).not.toContain(ignoredError);
      });
    });

    describe('SET_STATUS_FILTER', () => {
      it('sets the filter to ignored, resolved or unresolved', () => {
        state.statusFilter = 'unresolved';

        SET_STATUS_FILTER(state, 'ignored');

        expect(state.statusFilter).toBe('ignored');
      });
    });
  });
});