Commit 978f3d66 authored by Mark Florian's avatar Mark Florian

Merge branch...

Merge branch '34817-issue-description-reverts-to-an-older-version-when-saving-without-prompting-the-conflict' into 'master'

Updated autosave.js to save a lock version

See merge request gitlab-org/gitlab!29349
parents 0a49a02f 8e4823fd
......@@ -3,7 +3,7 @@
import AccessorUtilities from './lib/utils/accessor';
export default class Autosave {
constructor(field, key, fallbackKey) {
constructor(field, key, fallbackKey, lockVersion) {
this.field = field;
this.isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();
......@@ -12,6 +12,8 @@ export default class Autosave {
}
this.key = `autosave/${key}`;
this.fallbackKey = fallbackKey;
this.lockVersionKey = `${this.key}/lockVersion`;
this.lockVersion = lockVersion;
this.field.data('autosave', this);
this.restore();
this.field.on('input', () => this.save());
......@@ -40,6 +42,11 @@ export default class Autosave {
}
}
getSavedLockVersion() {
if (!this.isLocalStorageAvailable) return;
return window.localStorage.getItem(this.lockVersionKey);
}
save() {
if (!this.field.length) return;
......@@ -49,6 +56,9 @@ export default class Autosave {
if (this.fallbackKey) {
window.localStorage.setItem(this.fallbackKey, text);
}
if (this.lockVersion !== undefined) {
window.localStorage.setItem(this.lockVersionKey, this.lockVersion);
}
return window.localStorage.setItem(this.key, text);
}
......@@ -58,6 +68,7 @@ export default class Autosave {
reset() {
if (!this.isLocalStorageAvailable) return;
window.localStorage.removeItem(this.lockVersionKey);
window.localStorage.removeItem(this.fallbackKey);
return window.localStorage.removeItem(this.key);
}
......
......@@ -10,6 +10,8 @@ describe('Autosave', () => {
const field = $('<textarea></textarea>');
const key = 'key';
const fallbackKey = 'fallbackKey';
const lockVersionKey = 'lockVersionKey';
const lockVersion = 1;
describe('class constructor', () => {
beforeEach(() => {
......@@ -30,6 +32,13 @@ describe('Autosave', () => {
expect(AccessorUtilities.isLocalStorageAccessSafe).toHaveBeenCalled();
expect(autosave.isLocalStorageAvailable).toBe(true);
});
it('should set .isLocalStorageAvailable if lockVersion is passed', () => {
autosave = new Autosave(field, key, null, lockVersion);
expect(AccessorUtilities.isLocalStorageAccessSafe).toHaveBeenCalled();
expect(autosave.isLocalStorageAvailable).toBe(true);
});
});
describe('restore', () => {
......@@ -96,6 +105,40 @@ describe('Autosave', () => {
});
});
describe('getSavedLockVersion', () => {
beforeEach(() => {
autosave = {
field,
key,
lockVersionKey,
};
});
describe('if .isLocalStorageAvailable is `false`', () => {
beforeEach(() => {
autosave.isLocalStorageAvailable = false;
Autosave.prototype.getSavedLockVersion.call(autosave);
});
it('should not call .getItem', () => {
expect(window.localStorage.getItem).not.toHaveBeenCalled();
});
});
describe('if .isLocalStorageAvailable is `true`', () => {
beforeEach(() => {
autosave.isLocalStorageAvailable = true;
});
it('should call .getItem', () => {
Autosave.prototype.getSavedLockVersion.call(autosave);
expect(window.localStorage.getItem).toHaveBeenCalledWith(lockVersionKey);
});
});
});
describe('save', () => {
beforeEach(() => {
autosave = { reset: jest.fn() };
......@@ -128,10 +171,51 @@ describe('Autosave', () => {
});
});
describe('save with lockVersion', () => {
beforeEach(() => {
autosave = {
field,
key,
lockVersionKey,
lockVersion,
isLocalStorageAvailable: true,
};
});
describe('lockVersion is valid', () => {
it('should call .setItem', () => {
Autosave.prototype.save.call(autosave);
expect(window.localStorage.setItem).toHaveBeenCalledWith(lockVersionKey, lockVersion);
});
it('should call .setItem when version is 0', () => {
autosave.lockVersion = 0;
Autosave.prototype.save.call(autosave);
expect(window.localStorage.setItem).toHaveBeenCalledWith(
lockVersionKey,
autosave.lockVersion,
);
});
});
describe('lockVersion is invalid', () => {
it('should not call .setItem with lockVersion', () => {
delete autosave.lockVersion;
Autosave.prototype.save.call(autosave);
expect(window.localStorage.setItem).not.toHaveBeenCalledWith(
lockVersionKey,
autosave.lockVersion,
);
});
});
});
describe('reset', () => {
beforeEach(() => {
autosave = {
key,
lockVersionKey,
};
});
......@@ -156,6 +240,7 @@ describe('Autosave', () => {
it('should call .removeItem', () => {
expect(window.localStorage.removeItem).toHaveBeenCalledWith(key);
expect(window.localStorage.removeItem).toHaveBeenCalledWith(lockVersionKey);
});
});
});
......@@ -166,8 +251,8 @@ describe('Autosave', () => {
field,
key,
fallbackKey,
isLocalStorageAvailable: true,
};
autosave.isLocalStorageAvailable = true;
});
it('should call .getItem', () => {
......@@ -185,7 +270,8 @@ describe('Autosave', () => {
it('should call .removeItem for key and fallbackKey', () => {
Autosave.prototype.reset.call(autosave);
expect(window.localStorage.removeItem).toHaveBeenCalledTimes(2);
expect(window.localStorage.removeItem).toHaveBeenCalledWith(fallbackKey);
expect(window.localStorage.removeItem).toHaveBeenCalledWith(key);
});
});
});
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