Commit c75da766 authored by Winnie Hellmann's avatar Winnie Hellmann

Add autosave utilities

parent e9d1393e
export const clearDraft = autosaveKey => {
try {
window.localStorage.removeItem(`autosave/${autosaveKey}`);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
};
export const getDraft = autosaveKey => {
try {
return window.localStorage.getItem(`autosave/${autosaveKey}`);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
return null;
}
};
export const updateDraft = (autosaveKey, text) => {
try {
window.localStorage.setItem(`autosave/${autosaveKey}`, text);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
};
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);
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment