2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-05-05 13:59:41 -04:00
|
|
|
import Autosave from '~/autosave';
|
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
|
|
|
|
|
|
|
describe('Autosave', () => {
|
|
|
|
let autosave;
|
2018-02-27 19:10:43 -05:00
|
|
|
const field = $('<textarea></textarea>');
|
|
|
|
const key = 'key';
|
2017-05-05 13:59:41 -04:00
|
|
|
|
|
|
|
describe('class constructor', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(AccessorUtilities, 'isLocalStorageAccessSafe').and.returnValue(true);
|
|
|
|
spyOn(Autosave.prototype, 'restore');
|
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);
|
|
|
|
|
2017-05-05 13:59:41 -04:00
|
|
|
expect(AccessorUtilities.isLocalStorageAccessSafe).toHaveBeenCalled();
|
|
|
|
expect(autosave.isLocalStorageAvailable).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('restore', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave = {
|
|
|
|
field,
|
|
|
|
key,
|
|
|
|
};
|
|
|
|
|
|
|
|
spyOn(window.localStorage, 'getItem');
|
|
|
|
});
|
|
|
|
|
|
|
|
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', () => {
|
|
|
|
spyOn(autosave.field, 'trigger').and.callThrough();
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2018-07-31 15:24:16 -04:00
|
|
|
it('triggers native event', done => {
|
2018-02-27 19:10:43 -05:00
|
|
|
autosave.field.get(0).addEventListener('change', () => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
Autosave.prototype.restore.call(autosave);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if field gets deleted from DOM', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave.field = $('.not-a-real-element');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not trigger event', () => {
|
|
|
|
spyOn(field, 'trigger').and.callThrough();
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('save', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave = jasmine.createSpyObj('autosave', ['reset']);
|
|
|
|
autosave.field = field;
|
2018-02-27 19:10:43 -05:00
|
|
|
field.val('value');
|
2017-05-05 13:59:41 -04:00
|
|
|
|
|
|
|
spyOn(window.localStorage, 'setItem');
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('reset', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
autosave = {
|
|
|
|
key,
|
|
|
|
};
|
|
|
|
|
|
|
|
spyOn(window.localStorage, 'removeItem');
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|