mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 02:42:22 +08:00
replace store.js with store.ts, test for store.ts (#9646)
This commit is contained in:

committed by
Torkel Ödegaard

parent
676a113966
commit
95250e4ea3
@ -1,5 +1,3 @@
|
||||
///<reference path="../../../headers/common.d.ts" />
|
||||
|
||||
import store from 'app/core/store';
|
||||
import coreModule from 'app/core/core_module';
|
||||
|
||||
|
44
public/app/core/specs/store.jest.ts
Normal file
44
public/app/core/specs/store.jest.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import store from '../store';
|
||||
|
||||
Object.assign(window, {
|
||||
localStorage: {
|
||||
removeItem(key) {
|
||||
delete window.localStorage[key];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
describe('store', () => {
|
||||
|
||||
it("should store", ()=> {
|
||||
store.set("key1", "123");
|
||||
expect(store.get("key1")).toBe("123");
|
||||
});
|
||||
|
||||
it("get key when undefined", ()=> {
|
||||
expect(store.get("key2")).toBe(undefined);
|
||||
});
|
||||
|
||||
it("check if key exixts", ()=> {
|
||||
store.set("key3", "123");
|
||||
expect(store.exists("key3")).toBe(true);
|
||||
});
|
||||
|
||||
it("get boolean when no key", ()=> {
|
||||
expect(store.getBool("key4", false)).toBe(false);
|
||||
});
|
||||
|
||||
it("get boolean", ()=> {
|
||||
store.set("key5", "true");
|
||||
expect(store.getBool("key5", false)).toBe(true);
|
||||
});
|
||||
|
||||
it("key should be deleted", ()=> {
|
||||
store.set("key6", "123");
|
||||
store.delete("key6");
|
||||
expect(store.exists("key6")).toBe(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
@ -1,26 +0,0 @@
|
||||
define([], function() {
|
||||
'use strict';
|
||||
|
||||
return {
|
||||
get: function(key) {
|
||||
return window.localStorage[key];
|
||||
},
|
||||
set: function(key, value) {
|
||||
window.localStorage[key] = value;
|
||||
},
|
||||
getBool: function(key, def) {
|
||||
if (def !== void 0 && !this.exists(key)) {
|
||||
return def;
|
||||
}
|
||||
return window.localStorage[key] === 'true';
|
||||
},
|
||||
exists: function(key) {
|
||||
return window.localStorage[key] !== void 0;
|
||||
},
|
||||
delete: function(key) {
|
||||
window.localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
});
|
29
public/app/core/store.ts
Normal file
29
public/app/core/store.ts
Normal file
@ -0,0 +1,29 @@
|
||||
export class Store {
|
||||
|
||||
get(key) {
|
||||
return window.localStorage[key];
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
window.localStorage[key] = value;
|
||||
}
|
||||
|
||||
getBool(key, def) {
|
||||
if (def !== void 0 && !this.exists(key)) {
|
||||
return def;
|
||||
}
|
||||
return window.localStorage[key] === 'true';
|
||||
}
|
||||
|
||||
exists(key) {
|
||||
return window.localStorage[key] !== void 0;
|
||||
}
|
||||
|
||||
delete(key) {
|
||||
window.localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const store = new Store();
|
||||
export default store;
|
@ -1,5 +1,3 @@
|
||||
///<reference path="../../headers/common.d.ts" />
|
||||
|
||||
import store from 'app/core/store';
|
||||
import _ from 'lodash';
|
||||
import config from 'app/core/config';
|
||||
|
Reference in New Issue
Block a user