From b197f5326cbe807a7b4d22ce187e09708764e3c2 Mon Sep 17 00:00:00 2001 From: Luke Elmers Date: Thu, 6 Aug 2020 14:26:54 -0600 Subject: [PATCH 1/2] Convert kibana_utils storage test to jest. --- .../public/storage/__tests__/storage.js | 124 ------------------ .../public/storage/storage.test.ts | 109 +++++++++++++++ 2 files changed, 109 insertions(+), 124 deletions(-) delete mode 100644 src/plugins/kibana_utils/public/storage/__tests__/storage.js create mode 100644 src/plugins/kibana_utils/public/storage/storage.test.ts diff --git a/src/plugins/kibana_utils/public/storage/__tests__/storage.js b/src/plugins/kibana_utils/public/storage/__tests__/storage.js deleted file mode 100644 index 073ed275b9aac..0000000000000 --- a/src/plugins/kibana_utils/public/storage/__tests__/storage.js +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import sinon from 'sinon'; -import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; -import '..'; - -let storage; -let $window; -const payload = { first: 'john', last: 'smith' }; - -function init() { - ngMock.module('kibana/storage', function ($provide) { - // mock $window.localStorage for storage - $provide.value('$window', { - localStorage: { - getItem: sinon.stub(), - setItem: sinon.spy(), - removeItem: sinon.spy(), - clear: sinon.spy(), - }, - }); - }); - - ngMock.inject(function ($injector) { - storage = $injector.get('localStorage'); - $window = $injector.get('$window'); - }); -} - -describe('StorageService', function () { - beforeEach(function () { - init(); - }); - - describe('expected API', function () { - it('should have expected methods', function () { - expect(storage.get).to.be.a('function'); - expect(storage.set).to.be.a('function'); - expect(storage.remove).to.be.a('function'); - expect(storage.clear).to.be.a('function'); - }); - }); - - describe('call behavior', function () { - it('should call getItem on the store', function () { - storage.get('name'); - - expect($window.localStorage.getItem.callCount).to.equal(1); - }); - - it('should call setItem on the store', function () { - storage.set('name', 'john smith'); - - expect($window.localStorage.setItem.callCount).to.equal(1); - }); - - it('should call removeItem on the store', function () { - storage.remove('name'); - - expect($window.localStorage.removeItem.callCount).to.equal(1); - }); - - it('should call clear on the store', function () { - storage.clear(); - - expect($window.localStorage.clear.callCount).to.equal(1); - }); - }); - - describe('json data', function () { - it('should parse JSON when reading from the store', function () { - const getItem = $window.localStorage.getItem; - getItem.returns(JSON.stringify(payload)); - - const data = storage.get('name'); - expect(data).to.eql(payload); - }); - - it('should write JSON string to the store', function () { - const setItem = $window.localStorage.setItem; - const key = 'name'; - const value = payload; - - storage.set(key, value); - - const call = setItem.getCall(0); - expect(call.args[0]).to.equal(key); - expect(call.args[1]).to.equal(JSON.stringify(value)); - }); - }); - - describe('expected responses', function () { - it('should return null when not exists', function () { - const data = storage.get('notexists'); - expect(data).to.equal(null); - }); - - it('should return null when invalid JSON', function () { - const getItem = $window.localStorage.getItem; - getItem.returns('not: json'); - - const data = storage.get('name'); - expect(data).to.equal(null); - }); - }); -}); diff --git a/src/plugins/kibana_utils/public/storage/storage.test.ts b/src/plugins/kibana_utils/public/storage/storage.test.ts new file mode 100644 index 0000000000000..8c5d3d11a21fe --- /dev/null +++ b/src/plugins/kibana_utils/public/storage/storage.test.ts @@ -0,0 +1,109 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Storage } from './storage'; +import { IStorage, IStorageWrapper } from './types'; + +const payload = { first: 'john', last: 'smith' }; +const createMockStore = (): MockedKeys => { + let store: Record = {}; + return { + getItem: jest.fn().mockImplementation((key) => store[key]), + setItem: jest.fn().mockImplementation((key, value) => (store[key] = value)), + removeItem: jest.fn().mockImplementation((key: string) => delete store[key]), + clear: jest.fn().mockImplementation(() => (store = {})), + }; +}; + +describe('StorageService', () => { + let storage: IStorageWrapper; + let mockStore: MockedKeys; + + beforeEach(() => { + jest.resetAllMocks(); + mockStore = createMockStore(); + storage = new Storage(mockStore); + }); + + describe('expected API', () => { + test('should have expected methods', () => { + expect(typeof storage.get).toBe('function'); + expect(typeof storage.set).toBe('function'); + expect(typeof storage.remove).toBe('function'); + expect(typeof storage.clear).toBe('function'); + }); + }); + + describe('call behavior', () => { + test('should call getItem on the store', () => { + storage.get('name'); + + expect(mockStore.getItem).toHaveBeenCalledTimes(1); + }); + + test('should call setItem on the store', () => { + storage.set('name', 'john smith'); + + expect(mockStore.setItem).toHaveBeenCalledTimes(1); + }); + + test('should call removeItem on the store', () => { + storage.remove('name'); + + expect(mockStore.removeItem).toHaveBeenCalledTimes(1); + }); + + test('should call clear on the store', () => { + storage.clear(); + + expect(mockStore.clear).toHaveBeenCalledTimes(1); + }); + }); + + describe('json data', () => { + test('should parse JSON when reading from the store', () => { + mockStore.getItem = jest.fn().mockImplementationOnce(() => JSON.stringify(payload)); + + const data = storage.get('name'); + expect(data).toEqual(payload); + }); + + test('should write JSON string to the store', () => { + const key = 'name'; + const value = payload; + + storage.set(key, value); + expect(mockStore.setItem).toHaveBeenCalledWith(key, JSON.stringify(value)); + }); + }); + + describe('expected responses', () => { + test('should return null when not exists', () => { + const data = storage.get('notexists'); + expect(data).toBe(null); + }); + + test('should return null when invalid JSON', () => { + mockStore.getItem = jest.fn().mockImplementationOnce(() => 'not: json'); + + const data = storage.get('name'); + expect(data).toBe(null); + }); + }); +}); From 8e29dfaa728798d868a52335260e79bd01776894 Mon Sep 17 00:00:00 2001 From: Luke Elmers Date: Thu, 6 Aug 2020 14:32:20 -0600 Subject: [PATCH 2/2] Move visualization test stubs out of karma test folder. --- .../vis_update_objs/gauge_objs.js => vis_update_state.stub.js} | 0 .../visualizations/public/legacy/vis_update_state.test.js | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename src/plugins/visualizations/public/legacy/{__tests__/vis_update_objs/gauge_objs.js => vis_update_state.stub.js} (100%) diff --git a/src/plugins/visualizations/public/legacy/__tests__/vis_update_objs/gauge_objs.js b/src/plugins/visualizations/public/legacy/vis_update_state.stub.js similarity index 100% rename from src/plugins/visualizations/public/legacy/__tests__/vis_update_objs/gauge_objs.js rename to src/plugins/visualizations/public/legacy/vis_update_state.stub.js diff --git a/src/plugins/visualizations/public/legacy/vis_update_state.test.js b/src/plugins/visualizations/public/legacy/vis_update_state.test.js index 7ddf0cc6e33e1..d0a735fbacdc2 100644 --- a/src/plugins/visualizations/public/legacy/vis_update_state.test.js +++ b/src/plugins/visualizations/public/legacy/vis_update_state.test.js @@ -21,7 +21,7 @@ import _ from 'lodash'; import { updateOldState } from './vis_update_state'; // eslint-disable-next-line camelcase -import { pre_6_1, since_6_1 } from './__tests__/vis_update_objs/gauge_objs'; +import { pre_6_1, since_6_1 } from './vis_update_state.stub'; function watchForChanges(obj) { const originalObject = _.cloneDeep(obj);