2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2017-08-17 22:00:37 +05:30
|
|
|
import Autosave from '~/autosave';
|
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
2019-09-04 21:01:54 +05:30
|
|
|
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe('Autosave', () => {
|
2019-09-04 21:01:54 +05:30
|
|
|
useLocalStorageSpy();
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
let autosave;
|
2018-03-27 19:54:05 +05:30
|
|
|
const field = $('<textarea></textarea>');
|
|
|
|
const key = 'key';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe('class constructor', () => {
|
|
|
|
beforeEach(() => {
|
2019-09-04 21:01:54 +05:30
|
|
|
jest.spyOn(AccessorUtilities, 'isLocalStorageAccessSafe').mockReturnValue(true);
|
|
|
|
jest.spyOn(Autosave.prototype, 'restore').mockImplementation(() => {});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should set .isLocalStorageAvailable', () => {
|
2018-03-27 19:54:05 +05:30
|
|
|
autosave = new Autosave(field, key);
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(AccessorUtilities.isLocalStorageAccessSafe).toHaveBeenCalled();
|
|
|
|
expect(autosave.isLocalStorageAvailable).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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-03-27 19:54:05 +05:30
|
|
|
Autosave.prototype.restore.call(autosave);
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(window.localStorage.getItem).toHaveBeenCalledWith(key);
|
|
|
|
});
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
it('triggers jquery event', () => {
|
2019-09-04 21:01:54 +05:30
|
|
|
jest.spyOn(autosave.field, 'trigger').mockImplementation(() => {});
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
Autosave.prototype.restore.call(autosave);
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(field.trigger).toHaveBeenCalled();
|
2018-03-27 19:54:05 +05:30
|
|
|
});
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it('triggers native event', done => {
|
2018-03-27 19:54:05 +05:30
|
|
|
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', () => {
|
2019-09-04 21:01:54 +05:30
|
|
|
jest.spyOn(field, 'trigger');
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(field.trigger).not.toHaveBeenCalled();
|
2018-03-27 19:54:05 +05:30
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('save', () => {
|
|
|
|
beforeEach(() => {
|
2019-09-04 21:01:54 +05:30
|
|
|
autosave = { reset: jest.fn() };
|
2017-08-17 22:00:37 +05:30
|
|
|
autosave.field = field;
|
2018-03-27 19:54:05 +05:30
|
|
|
field.val('value');
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|