summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/autosave_spec.js
blob: 12e97f6cdec9271e93a7abf865a4f763a8d379be (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
import { clearDraft, getDraft, updateDraft } from '~/lib/utils/autosave';

describe('autosave utils', () => {
  const autosaveKey = 'dummy-autosave-key';
  const text = 'some dummy text';

  describe('clearDraft', () => {
    beforeEach(() => {
      localStorage.setItem(`autosave/${autosaveKey}`, text);
    });

    afterEach(() => {
      localStorage.removeItem(`autosave/${autosaveKey}`);
    });

    it('removes the draft from localStorage', () => {
      clearDraft(autosaveKey);

      expect(localStorage.getItem(`autosave/${autosaveKey}`)).toBe(null);
    });
  });

  describe('getDraft', () => {
    beforeEach(() => {
      localStorage.setItem(`autosave/${autosaveKey}`, text);
    });

    afterEach(() => {
      localStorage.removeItem(`autosave/${autosaveKey}`);
    });

    it('returns the draft from localStorage', () => {
      const result = getDraft(autosaveKey);

      expect(result).toBe(text);
    });

    it('returns null if no entry exists in localStorage', () => {
      localStorage.removeItem(`autosave/${autosaveKey}`);

      const result = getDraft(autosaveKey);

      expect(result).toBe(null);
    });
  });

  describe('updateDraft', () => {
    beforeEach(() => {
      localStorage.setItem(`autosave/${autosaveKey}`, text);
    });

    afterEach(() => {
      localStorage.removeItem(`autosave/${autosaveKey}`);
    });

    it('removes the draft from localStorage', () => {
      const newText = 'new text';

      updateDraft(autosaveKey, newText);

      expect(localStorage.getItem(`autosave/${autosaveKey}`)).toBe(newText);
    });
  });
});