debian-mirror-gitlab/spec/frontend/lib/utils/autosave_spec.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-05-18 00:54:41 +05:30
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);
});
});
});