summaryrefslogtreecommitdiff
path: root/spec/javascripts/filtered_search/services/recent_searches_service_spec.js
blob: 31fa478804af9d69c3ddfc8419122a2a7ffc40a2 (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
/* eslint-disable promise/catch-or-return */

import RecentSearchesService from '~/filtered_search/services/recent_searches_service';
import AccessorUtilities from '~/lib/utils/accessor';

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

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

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

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

      fetchItemsPromise
        .then((items) => {
          expect(items).toEqual([]);
          done();
        })
        .catch((err) => {
          done.fail('Shouldn\'t reject with empty localStorage key', err);
        });
    });

    it('should reject when unable to parse', (done) => {
      window.localStorage.setItem(service.localStorageKey, 'fail');
      const fetchItemsPromise = service.fetch();

      fetchItemsPromise
        .catch((error) => {
          expect(error).toEqual(jasmine.any(SyntaxError));
          done();
        });
    });

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

      service.fetch().catch((error) => {
        expect(error).toEqual(jasmine.any(Error));
        done();
      });
    });

    it('should return items from localStorage', (done) => {
      window.localStorage.setItem(service.localStorageKey, '["foo", "bar"]');
      const fetchItemsPromise = service.fetch();

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

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

        spyOn(window.localStorage, 'getItem');

        RecentSearchesService.prototype.fetch();
      });

      it('should not call .getItem', () => {
        expect(window.localStorage.getItem).not.toHaveBeenCalled();
      });
    });
  });

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

    it('should save things in localStorage', () => {
      const items = ['foo', 'bar'];
      service.save(items);
      const newLocalStorageValue = window.localStorage.getItem(service.localStorageKey);
      expect(JSON.parse(newLocalStorageValue)).toEqual(items);
    });
  });

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

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

      beforeEach(() => {
        RecentSearchesService.isAvailable.and.returnValue(true);

        spyOn(JSON, 'stringify').and.returnValue(searchesString);

        RecentSearchesService.prototype.save.call(recentSearchesService);
      });

      it('should call .setItem', () => {
        expect(window.localStorage.setItem).toHaveBeenCalledWith(localStorageKey, searchesString);
      });
    });

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

        RecentSearchesService.prototype.save();
      });

      it('should not call .setItem', () => {
        expect(window.localStorage.setItem).not.toHaveBeenCalled();
      });
    });
  });

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

    beforeEach(() => {
      spyOn(AccessorUtilities, 'isLocalStorageAccessSafe').and.callThrough();

      isAvailable = RecentSearchesService.isAvailable();
    });

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

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