summaryrefslogtreecommitdiff
path: root/spec/frontend/filtered_search/services/recent_searches_service_spec.js
blob: 426a60df427edc3e36d4f0a26194788778f597d1 (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
155
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import RecentSearchesService from '~/filtered_search/services/recent_searches_service';
import RecentSearchesServiceError from '~/filtered_search/services/recent_searches_service_error';
import AccessorUtilities from '~/lib/utils/accessor';

useLocalStorageSpy();

describe('RecentSearchesService', () => {
  let service;

  beforeEach(() => {
    service = new RecentSearchesService();
    localStorage.removeItem(service.localStorageKey);
  });

  describe('fetch', () => {
    beforeEach(() => {
      jest.spyOn(RecentSearchesService, 'isAvailable').mockReturnValue(true);
    });

    it('should default to empty array', () => {
      const fetchItemsPromise = service.fetch();

      return fetchItemsPromise.then((items) => {
        expect(items).toEqual([]);
      });
    });

    it('should reject when unable to parse', () => {
      jest.spyOn(localStorage, 'getItem').mockReturnValue('fail');
      const fetchItemsPromise = service.fetch();

      return fetchItemsPromise
        .then(() => {
          throw new Error();
        })
        .catch((error) => {
          expect(error).toEqual(expect.any(SyntaxError));
        });
    });

    it('should reject when service is unavailable', () => {
      RecentSearchesService.isAvailable.mockReturnValue(false);

      return service
        .fetch()
        .then(() => {
          throw new Error();
        })
        .catch((error) => {
          expect(error).toEqual(expect.any(Error));
        });
    });

    it('should return items from localStorage', () => {
      jest.spyOn(localStorage, 'getItem').mockReturnValue('["foo", "bar"]');
      const fetchItemsPromise = service.fetch();

      return fetchItemsPromise.then((items) => {
        expect(items).toEqual(['foo', 'bar']);
      });
    });

    describe('if .isAvailable returns `false`', () => {
      beforeEach(() => {
        RecentSearchesService.isAvailable.mockReturnValue(false);

        jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {});
      });

      it('should not call .getItem', () => {
        return RecentSearchesService.prototype
          .fetch()
          .then(() => {
            throw new Error();
          })
          .catch((err) => {
            expect(err).toEqual(new RecentSearchesServiceError());
            expect(localStorage.getItem).not.toHaveBeenCalled();
          });
      });
    });
  });

  describe('setRecentSearches', () => {
    beforeEach(() => {
      jest.spyOn(RecentSearchesService, 'isAvailable').mockReturnValue(true);
    });

    it('should save things in localStorage', () => {
      jest.spyOn(localStorage, 'setItem');
      const items = ['foo', 'bar'];
      service.save(items);

      expect(localStorage.setItem).toHaveBeenCalledWith(expect.any(String), JSON.stringify(items));
    });
  });

  describe('save', () => {
    beforeEach(() => {
      jest.spyOn(localStorage, 'setItem');
      jest.spyOn(RecentSearchesService, 'isAvailable').mockImplementation(() => {});
    });

    describe('if .isAvailable returns `true`', () => {
      const searchesString = 'searchesString';
      const localStorageKey = 'localStorageKey';
      const recentSearchesService = {
        localStorageKey,
      };

      beforeEach(() => {
        RecentSearchesService.isAvailable.mockReturnValue(true);

        jest.spyOn(JSON, 'stringify').mockReturnValue(searchesString);
      });

      it('should call .setItem', () => {
        RecentSearchesService.prototype.save.call(recentSearchesService);

        expect(localStorage.setItem).toHaveBeenCalledWith(localStorageKey, searchesString);
      });
    });

    describe('if .isAvailable returns `false`', () => {
      beforeEach(() => {
        RecentSearchesService.isAvailable.mockReturnValue(false);
      });

      it('should not call .setItem', () => {
        RecentSearchesService.prototype.save();

        expect(localStorage.setItem).not.toHaveBeenCalled();
      });
    });
  });

  describe('isAvailable', () => {
    let isAvailable;

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

      isAvailable = RecentSearchesService.isAvailable();
    });

    it('should call .canUseLocalStorage', () => {
      expect(AccessorUtilities.canUseLocalStorage).toHaveBeenCalled();
    });

    it('should return a boolean', () => {
      expect(typeof isAvailable).toBe('boolean');
    });
  });
});