Update Canvas usage of Storage from kibana-utils (#55595)

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Corey Robertson 2020-02-14 10:57:06 -05:00 committed by GitHub
parent 52b4fe7404
commit 98564f857d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 40 deletions

View file

@ -10,7 +10,6 @@ import { CanvasStartDeps } from './plugin'; // eslint-disable-line import/order
// @ts-ignore Untyped Kibana Lib
import chrome, { loadingCount } from 'ui/chrome'; // eslint-disable-line import/order
import { absoluteToParsedUrl } from 'ui/url/absolute_to_parsed_url'; // eslint-disable-line import/order
import { Storage } from '../../../../../src/plugins/kibana_utils/public'; // eslint-disable-line import/order
// @ts-ignore Untyped Kibana Lib
import { formatMsg } from '../../../../../src/plugins/kibana_legacy/public'; // eslint-disable-line import/order
@ -31,7 +30,6 @@ const shimStartPlugins: CanvasStartDeps = {
absoluteToParsedUrl,
// ToDo: Copy directly into canvas
formatMsg,
storage: Storage,
// ToDo: Won't be a part of New Platform. Will need to handle internally
trackSubUrlForApp: chrome.trackSubUrlForApp,
},

View file

@ -1,16 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import expect from '@kbn/expect';
import { setClipboardData, getClipboardData } from '../clipboard';
import { elements } from '../../../__tests__/fixtures/workpads';
describe('clipboard', () => {
it('stores and retrieves clipboard data', () => {
setClipboardData(elements);
expect(getClipboardData()).to.eql(JSON.stringify(elements));
});
});

View file

@ -1,17 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { LOCALSTORAGE_CLIPBOARD } from '../../common/lib/constants';
import { getWindow } from './get_window';
let storage = null;
export const initClipboard = function(Storage) {
storage = new Storage(getWindow().localStorage);
};
export const setClipboardData = data => storage.set(LOCALSTORAGE_CLIPBOARD, JSON.stringify(data));
export const getClipboardData = () => storage.get(LOCALSTORAGE_CLIPBOARD);

View file

@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
jest.mock('../../../../../../src/plugins/kibana_utils/public');
import { Storage } from '../../../../../../src/plugins/kibana_utils/public';
import { setClipboardData, getClipboardData } from './clipboard';
import { LOCALSTORAGE_CLIPBOARD } from '../../common/lib/constants';
import { elements } from '../../__tests__/fixtures/workpads';
const set = jest.fn();
const get = jest.fn();
describe('clipboard', () => {
beforeAll(() => {
// @ts-ignore
Storage.mockImplementation(() => ({
set,
get,
}));
});
test('stores data to local storage', () => {
setClipboardData(elements);
expect(set).toBeCalledWith(LOCALSTORAGE_CLIPBOARD, JSON.stringify(elements));
});
test('gets data from local storage', () => {
getClipboardData();
expect(get).toBeCalledWith(LOCALSTORAGE_CLIPBOARD);
});
});

View file

@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Storage } from '../../../../../../src/plugins/kibana_utils/public';
import { LOCALSTORAGE_CLIPBOARD } from '../../common/lib/constants';
import { getWindow } from './get_window';
let storage: Storage;
const getStorage = (): Storage => {
if (!storage) {
storage = new Storage(getWindow().localStorage);
}
return storage;
};
export const setClipboardData = (data: any) => {
getStorage().set(LOCALSTORAGE_CLIPBOARD, JSON.stringify(data));
};
export const getClipboardData = () => getStorage().get(LOCALSTORAGE_CLIPBOARD);

View file

@ -5,8 +5,10 @@
*/
// return window if it exists, otherwise just return an object literal
const windowObj = { location: null };
const windowObj = { location: null, localStorage: {} as Window['localStorage'] };
export const getWindow = (): Window | { location: Location | null } => {
export const getWindow = ():
| Window
| { location: Location | null; localStorage: Window['localStorage'] } => {
return typeof window === 'undefined' ? windowObj : window;
};

View file

@ -8,7 +8,6 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { Chrome } from 'ui/chrome';
import { i18n } from '@kbn/i18n';
import { Storage } from '../../../../../src/plugins/kibana_utils/public';
import { CoreSetup, CoreStart, Plugin } from '../../../../../src/core/public';
import { HomePublicPluginSetup } from '../../../../../src/plugins/home/public';
// @ts-ignore: Untyped Local
@ -43,7 +42,6 @@ export interface CanvasStartDeps {
__LEGACY: {
absoluteToParsedUrl: (url: string, basePath: string) => any;
formatMsg: any;
storage: typeof Storage;
trackSubUrlForApp: Chrome['trackSubUrlForApp'];
};
}
@ -92,7 +90,6 @@ export class CanvasPlugin
loadExpressionTypes();
loadTransitions();
initClipboard(plugins.__LEGACY.storage);
initLoadingIndicator(core.http.addLoadingCountSource);
core.chrome.setBadge(