diff options
author | Paul Slaughter <pslaughter@gitlab.com> | 2019-05-24 13:14:24 -0500 |
---|---|---|
committer | Paul Slaughter <pslaughter@gitlab.com> | 2019-06-04 12:14:50 -0500 |
commit | a9850b25c3624fe31d0230ad3f0df00fec6b7d48 (patch) | |
tree | 8f10492f2d6376a2ad2bb903d9f1027780979910 /spec/javascripts | |
parent | 2bf5135af31a2ff133f3682f59323954458cc2c3 (diff) | |
download | gitlab-ce-a9850b25c3624fe31d0230ad3f0df00fec6b7d48.tar.gz |
Jestify autosave spec and add localStorage helperfe-jestify-specs-starting-with-a-1
The helper was needed because `jest.spyOn` would not
work on this special window object property. The only way
we could successfully spy on `localStorage` was with this helper.
Diffstat (limited to 'spec/javascripts')
-rw-r--r-- | spec/javascripts/autosave_spec.js | 154 |
1 files changed, 0 insertions, 154 deletions
diff --git a/spec/javascripts/autosave_spec.js b/spec/javascripts/autosave_spec.js deleted file mode 100644 index dcb1c781591..00000000000 --- a/spec/javascripts/autosave_spec.js +++ /dev/null @@ -1,154 +0,0 @@ -import $ from 'jquery'; -import Autosave from '~/autosave'; -import AccessorUtilities from '~/lib/utils/accessor'; - -describe('Autosave', () => { - let autosave; - const field = $('<textarea></textarea>'); - const key = 'key'; - - describe('class constructor', () => { - beforeEach(() => { - spyOn(AccessorUtilities, 'isLocalStorageAccessSafe').and.returnValue(true); - spyOn(Autosave.prototype, 'restore'); - }); - - it('should set .isLocalStorageAvailable', () => { - autosave = new Autosave(field, key); - - 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', () => { - Autosave.prototype.restore.call(autosave); - - expect(window.localStorage.getItem).toHaveBeenCalledWith(key); - }); - - it('triggers jquery event', () => { - spyOn(autosave.field, 'trigger').and.callThrough(); - - Autosave.prototype.restore.call(autosave); - - expect(field.trigger).toHaveBeenCalled(); - }); - - it('triggers native event', done => { - 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(); - - expect(field.trigger).not.toHaveBeenCalled(); - }); - }); - }); - - describe('save', () => { - beforeEach(() => { - autosave = jasmine.createSpyObj('autosave', ['reset']); - autosave.field = field; - field.val('value'); - - 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); - }); - }); - }); -}); |