2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2019-12-10 04:07:51 -05:00
|
|
|
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
|
2017-05-05 13:59:41 -04:00
|
|
|
import Autosave from '~/autosave';
|
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
|
|
|
|
|
|
|
describe('Autosave', () => {
|
2019-05-24 14:14:24 -04:00
|
|
|
useLocalStorageSpy();
|
|
|
|
|
2017-05-05 13:59:41 -04:00
|
|
|
let autosave;
|
2018-02-27 19:10:43 -05:00
|
|
|
const field = $('<textarea></textarea>');
|
|
|
|
const key = 'key';
|
2019-11-21 04:06:16 -05:00
|
|
|
const fallbackKey = 'fallbackKey';
|
2020-04-27 11:10:16 -04:00
|
|
|
const lockVersionKey = 'lockVersionKey';
|
|
|
|
const lockVersion = 1;
|
2017-05-05 13:59:41 -04:00
|
|
|
|
|
|
|
describe('class constructor', () => {
|
|
|
|
beforeEach(() => {
|
2021-09-16 14:11:32 -04:00
|
|
|
jest.spyOn(AccessorUtilities, 'canUseLocalStorage').mockReturnValue(true);
|
2019-05-24 14:14:24 -04:00
|
|
|
jest.spyOn(Autosave.prototype, 'restore').mockImplementation(() => {});
|
2018-07-16 08:34:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should set .isLocalStorageAvailable', () => {
|
2018-07-17 19:58:01 -04:00
|
|
|
autosave = new Autosave(field, key);
|
|
|
|
|
2021-09-16 14:11:32 -04:00
|
|
|
expect(AccessorUtilities.canUseLocalStorage).toHaveBeenCalled();
|
2017-05-05 13:59:41 -04:00
|
|
|
expect(autosave.isLocalStorageAvailable).toBe(true);
|
|
|
|
});
|
2019-11-21 04:06:16 -05:00
|
|
|
|
|
|
|
it('should set .isLocalStorageAvailable if fallbackKey is passed', () => {
|
|
|
|
autosave = new Autosave(field, key, fallbackKey);
|
|
|
|
|
2021-09-16 14:11:32 -04:00
|
|
|
expect(AccessorUtilities.canUseLocalStorage).toHaveBeenCalled();
|
2019-11-21 04:06:16 -05:00
|
|
|
expect(autosave.isLocalStorageAvailable).toBe(true);
|
|
|
|
});
|
2020-04-27 11:10:16 -04:00
|
|
|
|
|
|
|
it('should set .isLocalStorageAvailable if lockVersion is passed', () => {
|
|
|
|
autosave = new Autosave(field, key, null, lockVersion);
|
|
|
|
|
2021-09-16 14:11:32 -04:00
|
|
|
expect(AccessorUtilities.canUseLocalStorage).toHaveBeenCalled();
|
2020-04-27 11:10:16 -04:00
|
|
|
expect(autosave.isLocalStorageAvailable).toBe(true);
|
|
|
|
});
|
2017-05-05 13:59:41 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('restore', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave = {
|
|
|
|
field,
|
|
|
|
key,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isLocalStorageAvailable is `false`', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave.isLocalStorageAvailable = false;
|
|
|
|
|
|
|
|
Autosave.prototype.restore.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', () => {
|
2018-02-27 19:10:43 -05:00
|
|
|
Autosave.prototype.restore.call(autosave);
|
|
|
|
|
2017-05-05 13:59:41 -04:00
|
|
|
expect(window.localStorage.getItem).toHaveBeenCalledWith(key);
|
|
|
|
});
|
2018-02-27 19:10:43 -05:00
|
|
|
|
|
|
|
it('triggers jquery event', () => {
|
2019-05-24 14:14:24 -04:00
|
|
|
jest.spyOn(autosave.field, 'trigger').mockImplementation(() => {});
|
2018-02-27 19:10:43 -05:00
|
|
|
|
|
|
|
Autosave.prototype.restore.call(autosave);
|
|
|
|
|
2018-07-31 15:24:16 -04:00
|
|
|
expect(field.trigger).toHaveBeenCalled();
|
2018-02-27 19:10:43 -05:00
|
|
|
});
|
|
|
|
|
2019-08-21 04:01:58 -04:00
|
|
|
it('triggers native event', () => {
|
|
|
|
const fieldElement = autosave.field.get(0);
|
|
|
|
const eventHandler = jest.fn();
|
|
|
|
fieldElement.addEventListener('change', eventHandler);
|
2018-02-27 19:10:43 -05:00
|
|
|
|
|
|
|
Autosave.prototype.restore.call(autosave);
|
2019-08-21 04:01:58 -04:00
|
|
|
|
|
|
|
expect(eventHandler).toHaveBeenCalledTimes(1);
|
|
|
|
fieldElement.removeEventListener('change', eventHandler);
|
2018-02-27 19:10:43 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if field gets deleted from DOM', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave.field = $('.not-a-real-element');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not trigger event', () => {
|
2019-05-24 14:14:24 -04:00
|
|
|
jest.spyOn(field, 'trigger');
|
2018-02-27 19:10:43 -05:00
|
|
|
|
2018-07-31 15:24:16 -04:00
|
|
|
expect(field.trigger).not.toHaveBeenCalled();
|
2018-02-27 19:10:43 -05:00
|
|
|
});
|
2017-05-05 13:59:41 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-27 11:10:16 -04:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-05 13:59:41 -04:00
|
|
|
describe('save', () => {
|
|
|
|
beforeEach(() => {
|
2019-05-24 14:14:24 -04:00
|
|
|
autosave = { reset: jest.fn() };
|
2017-05-05 13:59:41 -04:00
|
|
|
autosave.field = field;
|
2018-02-27 19:10:43 -05:00
|
|
|
field.val('value');
|
2017-05-05 13:59:41 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isLocalStorageAvailable is `false`', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave.isLocalStorageAvailable = false;
|
|
|
|
|
|
|
|
Autosave.prototype.save.call(autosave);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call .setItem', () => {
|
|
|
|
expect(window.localStorage.setItem).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isLocalStorageAvailable is `true`', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave.isLocalStorageAvailable = true;
|
|
|
|
|
|
|
|
Autosave.prototype.save.call(autosave);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .setItem', () => {
|
|
|
|
expect(window.localStorage.setItem).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-27 11:10:16 -04:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-05 13:59:41 -04:00
|
|
|
describe('reset', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave = {
|
|
|
|
key,
|
2020-04-27 11:10:16 -04:00
|
|
|
lockVersionKey,
|
2017-05-05 13:59:41 -04:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isLocalStorageAvailable is `false`', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave.isLocalStorageAvailable = false;
|
|
|
|
|
|
|
|
Autosave.prototype.reset.call(autosave);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call .removeItem', () => {
|
|
|
|
expect(window.localStorage.removeItem).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isLocalStorageAvailable is `true`', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave.isLocalStorageAvailable = true;
|
|
|
|
|
|
|
|
Autosave.prototype.reset.call(autosave);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .removeItem', () => {
|
|
|
|
expect(window.localStorage.removeItem).toHaveBeenCalledWith(key);
|
2020-04-27 11:10:16 -04:00
|
|
|
expect(window.localStorage.removeItem).toHaveBeenCalledWith(lockVersionKey);
|
2017-05-05 13:59:41 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-11-21 04:06:16 -05:00
|
|
|
|
|
|
|
describe('restore with fallbackKey', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave = {
|
|
|
|
field,
|
|
|
|
key,
|
|
|
|
fallbackKey,
|
2020-04-27 11:10:16 -04:00
|
|
|
isLocalStorageAvailable: true,
|
2019-11-21 04:06:16 -05:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .getItem', () => {
|
|
|
|
Autosave.prototype.restore.call(autosave);
|
|
|
|
|
|
|
|
expect(window.localStorage.getItem).toHaveBeenCalledWith(fallbackKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .setItem for key and fallbackKey', () => {
|
|
|
|
Autosave.prototype.save.call(autosave);
|
|
|
|
|
|
|
|
expect(window.localStorage.setItem).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .removeItem for key and fallbackKey', () => {
|
|
|
|
Autosave.prototype.reset.call(autosave);
|
|
|
|
|
2020-04-27 11:10:16 -04:00
|
|
|
expect(window.localStorage.removeItem).toHaveBeenCalledWith(fallbackKey);
|
|
|
|
expect(window.localStorage.removeItem).toHaveBeenCalledWith(key);
|
2019-11-21 04:06:16 -05:00
|
|
|
});
|
|
|
|
});
|
2017-05-05 13:59:41 -04:00
|
|
|
});
|