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

100 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-11-25 23:54:43 +05:30
import { clearDraft, getDraft, updateDraft, getLockVersion } from '~/lib/utils/autosave';
2019-07-07 11:18:12 +05:30
describe('autosave utils', () => {
const autosaveKey = 'dummy-autosave-key';
const text = 'some dummy text';
2022-11-25 23:54:43 +05:30
const lockVersion = '2';
const normalizedAutosaveKey = `autosave/${autosaveKey}`;
const lockVersionKey = `autosave/${autosaveKey}/lockVersion`;
2019-07-07 11:18:12 +05:30
describe('clearDraft', () => {
beforeEach(() => {
2022-11-25 23:54:43 +05:30
localStorage.setItem(normalizedAutosaveKey, text);
localStorage.setItem(lockVersionKey, lockVersion);
2019-07-07 11:18:12 +05:30
});
afterEach(() => {
2022-11-25 23:54:43 +05:30
localStorage.removeItem(normalizedAutosaveKey);
2019-07-07 11:18:12 +05:30
});
it('removes the draft from localStorage', () => {
clearDraft(autosaveKey);
2022-11-25 23:54:43 +05:30
expect(localStorage.getItem(normalizedAutosaveKey)).toBe(null);
});
it('removes the lockVersion from localStorage', () => {
clearDraft(autosaveKey);
expect(localStorage.getItem(lockVersionKey)).toBe(null);
2019-07-07 11:18:12 +05:30
});
});
describe('getDraft', () => {
beforeEach(() => {
2022-11-25 23:54:43 +05:30
localStorage.setItem(normalizedAutosaveKey, text);
2019-07-07 11:18:12 +05:30
});
afterEach(() => {
2022-11-25 23:54:43 +05:30
localStorage.removeItem(normalizedAutosaveKey);
2019-07-07 11:18:12 +05:30
});
it('returns the draft from localStorage', () => {
const result = getDraft(autosaveKey);
expect(result).toBe(text);
});
it('returns null if no entry exists in localStorage', () => {
2022-11-25 23:54:43 +05:30
localStorage.removeItem(normalizedAutosaveKey);
2019-07-07 11:18:12 +05:30
const result = getDraft(autosaveKey);
expect(result).toBe(null);
});
});
describe('updateDraft', () => {
beforeEach(() => {
2022-11-25 23:54:43 +05:30
localStorage.setItem(normalizedAutosaveKey, text);
2019-07-07 11:18:12 +05:30
});
afterEach(() => {
2022-11-25 23:54:43 +05:30
localStorage.removeItem(normalizedAutosaveKey);
2019-07-07 11:18:12 +05:30
});
2022-11-25 23:54:43 +05:30
it('updates the stored draft', () => {
2019-07-07 11:18:12 +05:30
const newText = 'new text';
updateDraft(autosaveKey, newText);
2022-11-25 23:54:43 +05:30
expect(localStorage.getItem(normalizedAutosaveKey)).toBe(newText);
});
describe('when lockVersion is provided', () => {
it('updates the stored lockVersion', () => {
const newText = 'new text';
const newLockVersion = '2';
updateDraft(autosaveKey, newText, lockVersion);
expect(localStorage.getItem(lockVersionKey)).toBe(newLockVersion);
});
});
});
describe('getLockVersion', () => {
beforeEach(() => {
localStorage.setItem(lockVersionKey, lockVersion);
});
afterEach(() => {
localStorage.removeItem(lockVersionKey);
});
it('returns the lockVersion from localStorage', () => {
expect(getLockVersion(autosaveKey)).toBe(lockVersion);
2019-07-07 11:18:12 +05:30
});
});
});