mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
Typescript: stub browser typings (#26914)
* typings for stub browser * Fix default import * Address feedback * update jest snapshot
This commit is contained in:
parent
f522bbafe8
commit
686676c38d
10 changed files with 100 additions and 81 deletions
|
@ -32,6 +32,7 @@ export default {
|
|||
'<rootDir>/src/utils',
|
||||
'<rootDir>/src/setup_node_env',
|
||||
'<rootDir>/packages',
|
||||
'<rootDir>/src/test_utils',
|
||||
],
|
||||
collectCoverageFrom: [
|
||||
'packages/kbn-ui-framework/src/components/**/*.js',
|
||||
|
|
|
@ -17,54 +17,52 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
import expect from 'expect.js';
|
||||
|
||||
import StubBrowserStorage from '../stub_browser_storage';
|
||||
import { StubBrowserStorage } from './stub_browser_storage';
|
||||
|
||||
describe('StubBrowserStorage', () => {
|
||||
describe('#getItem() / #setItem()', () => {
|
||||
it('stores items as strings', () => {
|
||||
const store = new StubBrowserStorage();
|
||||
store.setItem(1, 1);
|
||||
expect(store.getItem(1)).to.be('1');
|
||||
store.setItem('1', '1');
|
||||
expect(store.getItem('1')).toBe('1');
|
||||
});
|
||||
|
||||
it('stores keys as strings', () => {
|
||||
const store = new StubBrowserStorage();
|
||||
store.setItem(1, 1);
|
||||
expect(store.key(0)).to.be('1');
|
||||
store.setItem('1', '1');
|
||||
expect(store.key(0)).toBe('1');
|
||||
});
|
||||
|
||||
it('returns null for missing keys', () => {
|
||||
const store = new StubBrowserStorage();
|
||||
expect(store.getItem('unknown key')).to.be(null);
|
||||
expect(store.getItem('unknown key')).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#length', () => {
|
||||
it('reports the number of items stored', () => {
|
||||
const store = new StubBrowserStorage();
|
||||
store.setItem(1, 1);
|
||||
store.setItem(2, 2);
|
||||
store.setItem(3, 3);
|
||||
store.setItem(4, 4);
|
||||
expect(store).to.have.length(4);
|
||||
store.setItem('1', '1');
|
||||
store.setItem('2', '2');
|
||||
store.setItem('3', '3');
|
||||
store.setItem('4', '4');
|
||||
expect(store).toHaveLength(4);
|
||||
});
|
||||
|
||||
it('does not trip on items getting reset', () => {
|
||||
const store = new StubBrowserStorage();
|
||||
store.setItem(1, 1);
|
||||
store.setItem(1, 2);
|
||||
expect(store).to.have.length(1);
|
||||
store.setItem('1', '1');
|
||||
store.setItem('1', '2');
|
||||
expect(store).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#key()', () => {
|
||||
it('returns the key as a specific index', () => {
|
||||
const store = new StubBrowserStorage();
|
||||
store.setItem(1, 2);
|
||||
expect(store.key(0)).to.be('1');
|
||||
expect(store.key(1)).to.be(undefined);
|
||||
store.setItem('1', '2');
|
||||
expect(store.key(0)).toBe('1');
|
||||
expect(store.key(1)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -75,7 +73,9 @@ describe('StubBrowserStorage', () => {
|
|||
store.setItem('abc', 'def'); // store size is 6, key.length + val.length
|
||||
expect(() => {
|
||||
store.setItem('ghi', 'jkl');
|
||||
}).throwError(/quota/);
|
||||
}).toThrowErrorMatchingInlineSnapshot(
|
||||
`"something about quota exceeded, browsers are not consistent here"`
|
||||
);
|
||||
});
|
||||
|
||||
it('allows defining the limit as infinity', () => {
|
||||
|
@ -90,7 +90,7 @@ describe('StubBrowserStorage', () => {
|
|||
store.setItem('key', 'val');
|
||||
expect(() => {
|
||||
store.setStubbedSizeLimit(5);
|
||||
}).throwError(Error);
|
||||
}).toThrowError(Error);
|
||||
});
|
||||
|
||||
it('respects removed items', () => {
|
||||
|
@ -106,15 +106,15 @@ describe('StubBrowserStorage', () => {
|
|||
it('returns the size limit', () => {
|
||||
const store = new StubBrowserStorage();
|
||||
store.setStubbedSizeLimit(10);
|
||||
expect(store.getStubbedSizeLimit()).to.equal(10);
|
||||
expect(store.getStubbedSizeLimit()).toBe(10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getStubbedSize', () => {
|
||||
it('returns the size', () => {
|
||||
const store = new StubBrowserStorage();
|
||||
store.setItem(1, 1);
|
||||
expect(store.getStubbedSize()).to.equal(2);
|
||||
store.setItem('1', '1');
|
||||
expect(store.getStubbedSize()).toBe(2);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -17,112 +17,106 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
export default class StubBrowserStorage {
|
||||
constructor() {
|
||||
this._keys = [];
|
||||
this._values = [];
|
||||
this._size = 0;
|
||||
this._sizeLimit = 5000000; // 5mb, minimum browser storage size
|
||||
}
|
||||
export class StubBrowserStorage {
|
||||
private readonly keys: string[] = [];
|
||||
private readonly values: string[] = [];
|
||||
private size = 0;
|
||||
private sizeLimit = 5000000; // 5mb, minimum browser storage size;
|
||||
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
// Browser-specific methods.
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
|
||||
get length() {
|
||||
return this._keys.length;
|
||||
return this.keys.length;
|
||||
}
|
||||
|
||||
key(i) {
|
||||
return this._keys[i];
|
||||
public key(i: number) {
|
||||
return this.keys[i];
|
||||
}
|
||||
|
||||
getItem(key) {
|
||||
public getItem(key: string) {
|
||||
key = String(key);
|
||||
|
||||
const i = this._keys.indexOf(key);
|
||||
if (i === -1) return null;
|
||||
return this._values[i];
|
||||
const i = this.keys.indexOf(key);
|
||||
if (i === -1) {
|
||||
return null;
|
||||
}
|
||||
return this.values[i];
|
||||
}
|
||||
|
||||
setItem(key, value) {
|
||||
public setItem(key: string, value: string) {
|
||||
key = String(key);
|
||||
value = String(value);
|
||||
const sizeOfAddition = this._getSizeOfAddition(key, value);
|
||||
this._updateSize(sizeOfAddition);
|
||||
const sizeOfAddition = this.getSizeOfAddition(key, value);
|
||||
this.updateSize(sizeOfAddition);
|
||||
|
||||
const i = this._keys.indexOf(key);
|
||||
const i = this.keys.indexOf(key);
|
||||
if (i === -1) {
|
||||
this._keys.push(key);
|
||||
this._values.push(value);
|
||||
this.keys.push(key);
|
||||
this.values.push(value);
|
||||
} else {
|
||||
this._values[i] = value;
|
||||
this.values[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
removeItem(key) {
|
||||
public removeItem(key: string) {
|
||||
key = String(key);
|
||||
const sizeOfRemoval = this._getSizeOfRemoval(key);
|
||||
this._updateSize(sizeOfRemoval);
|
||||
const sizeOfRemoval = this.getSizeOfRemoval(key);
|
||||
this.updateSize(sizeOfRemoval);
|
||||
|
||||
const i = this._keys.indexOf(key);
|
||||
if (i === -1) return;
|
||||
this._keys.splice(i, 1);
|
||||
this._values.splice(i, 1);
|
||||
const i = this.keys.indexOf(key);
|
||||
if (i === -1) {
|
||||
return;
|
||||
}
|
||||
this.keys.splice(i, 1);
|
||||
this.values.splice(i, 1);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
// Test-specific methods.
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
|
||||
getStubbedKeys() {
|
||||
return this._keys.slice();
|
||||
}
|
||||
|
||||
getStubbedValues() {
|
||||
return this._values.slice();
|
||||
}
|
||||
|
||||
setStubbedSizeLimit(sizeLimit) {
|
||||
public setStubbedSizeLimit(sizeLimit: number) {
|
||||
// We can't reconcile a size limit with the "stored" items, if the stored items size exceeds it.
|
||||
if (sizeLimit < this._size) {
|
||||
if (sizeLimit < this.size) {
|
||||
throw new Error(`You can't set a size limit smaller than the current size.`);
|
||||
}
|
||||
|
||||
this._sizeLimit = sizeLimit;
|
||||
this.sizeLimit = sizeLimit;
|
||||
}
|
||||
|
||||
getStubbedSizeLimit() {
|
||||
return this._sizeLimit;
|
||||
public getStubbedSizeLimit() {
|
||||
return this.sizeLimit;
|
||||
}
|
||||
|
||||
getStubbedSize() {
|
||||
return this._size;
|
||||
public getStubbedSize() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
_getSizeOfAddition(key, value) {
|
||||
const i = this._keys.indexOf(key);
|
||||
private getSizeOfAddition(key: string, value: string) {
|
||||
const i = this.keys.indexOf(key);
|
||||
if (i === -1) {
|
||||
return key.length + value.length;
|
||||
}
|
||||
// Return difference of what's been stored, and what *will* be stored.
|
||||
return value.length - this._values[i].length;
|
||||
return value.length - this.values[i].length;
|
||||
}
|
||||
|
||||
_getSizeOfRemoval(key) {
|
||||
const i = this._keys.indexOf(key);
|
||||
private getSizeOfRemoval(key: string) {
|
||||
const i = this.keys.indexOf(key);
|
||||
if (i === -1) {
|
||||
return 0;
|
||||
}
|
||||
// Return negative value.
|
||||
return -(key.length + this._values[i].length);
|
||||
return -(key.length + this.values[i].length);
|
||||
}
|
||||
|
||||
_updateSize(delta) {
|
||||
if (this._size + delta > this._sizeLimit) {
|
||||
private updateSize(delta: number) {
|
||||
if (this.size + delta > this.sizeLimit) {
|
||||
throw new Error('something about quota exceeded, browsers are not consistent here');
|
||||
}
|
||||
|
||||
this._size += delta;
|
||||
this.size += delta;
|
||||
}
|
||||
}
|
|
@ -20,7 +20,7 @@
|
|||
import expect from 'expect.js';
|
||||
|
||||
import { initChromeNavApi } from '../nav';
|
||||
import StubBrowserStorage from 'test_utils/stub_browser_storage';
|
||||
import { StubBrowserStorage } from 'test_utils/stub_browser_storage';
|
||||
import { KibanaParsedUrl } from '../../../url/kibana_parsed_url';
|
||||
|
||||
const basePath = '/someBasePath';
|
||||
|
|
23
src/ui/public/crypto/sha256.d.ts
vendored
Normal file
23
src/ui/public/crypto/sha256.d.ts
vendored
Normal file
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
export class Sha256 {
|
||||
public update(json: string | Buffer, encoding?: string): Sha256;
|
||||
public digest(encoding: string): string;
|
||||
}
|
|
@ -22,7 +22,7 @@ import expect from 'expect.js';
|
|||
import Chance from 'chance';
|
||||
|
||||
import { Storage } from '../../storage';
|
||||
import StubBrowserStorage from 'test_utils/stub_browser_storage';
|
||||
import { StubBrowserStorage } from 'test_utils/stub_browser_storage';
|
||||
import StubIndexPatternProvider from 'test_utils/stub_index_pattern';
|
||||
import { IsUserAwareOfUnsupportedTimePatternProvider } from '../unsupported_time_patterns';
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ import {
|
|||
isStateHash,
|
||||
} from '../state_storage';
|
||||
import { HashedItemStore } from '../state_storage/hashed_item_store';
|
||||
import StubBrowserStorage from 'test_utils/stub_browser_storage';
|
||||
import { StubBrowserStorage } from 'test_utils/stub_browser_storage';
|
||||
import { EventsProvider } from '../../events';
|
||||
|
||||
describe('State Management', () => {
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
import expect from 'expect.js';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import StubBrowserStorage from 'test_utils/stub_browser_storage';
|
||||
import { StubBrowserStorage } from 'test_utils/stub_browser_storage';
|
||||
import { HashedItemStore } from '../hashed_item_store';
|
||||
|
||||
describe('hashedItemStore', () => {
|
||||
|
|
|
@ -51,7 +51,8 @@
|
|||
"include": [
|
||||
"kibana.d.ts",
|
||||
"src/**/*",
|
||||
"typings/**/*"
|
||||
"typings/**/*",
|
||||
"test_utils/**/*"
|
||||
],
|
||||
"exclude": [
|
||||
"src/**/__fixtures__/**/*",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue