gitlab-org--gitlab-foss/spec/frontend/lib/utils/autosave_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-02-26 09:02:17 -05:00
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);
});
});
});