Browse Source

Fix storage tests

pull/4830/head
Artur Arseniev 3 years ago
parent
commit
979ebcd3c7
  1. 5
      src/editor/config/config.ts
  2. 5
      src/editor/model/Editor.ts
  3. 2
      src/storage_manager/index.ts
  4. 77
      test/specs/storage_manager/index.ts

5
src/editor/config/config.ts

@ -12,7 +12,9 @@ import { PanelsConfig } from '../../panels/config/config';
import { ParserConfig } from '../../parser/config/config';
import { RichTextEditorConfig } from '../../rich_text_editor/config/config';
import { SelectorManagerConfig } from '../../selector_manager/config/config';
import { StorageManagerConfig } from '../../storage_manager/config/config';
import { UndoManagerConfig } from '../../undo_manager/config';
import { Plugin } from '../../plugin_manager';
type AnyObject = Record<string, any>;
@ -313,11 +315,10 @@ export interface EditorConfig {
*/
canvas?: CanvasConfig;
// TODO
/**
* Configurations for Storage Manager.
*/
storageManager?: AnyObject | boolean;
storageManager?: StorageManagerConfig | boolean;
/**
* Configurations for Rich Text Editor.

5
src/editor/model/Editor.ts

@ -17,6 +17,7 @@ import Component from '../../dom_components/model/Component';
import BlockManager from '../../block_manager';
import SelectorManager from '../../selector_manager';
import ParserModule from '../../parser';
import StorageManager from '../../storage_manager';
//@ts-ignore
Backbone.$ = $;
@ -132,6 +133,10 @@ export default class EditorModel extends Model {
return this.get('SelectorManager');
}
get StorageManager(): StorageManager {
return this.get('StorageManager');
}
get Parser(): ParserModule {
return this.get('Parser');
}

2
src/storage_manager/index.ts

@ -233,7 +233,7 @@ export default class StorageManager extends Module<
const st = this.getCurrentStorage();
const opts = { ...this.getCurrentOptions(), ...options };
const recoveryStorage = this.getRecoveryStorage();
let result: ProjectData = {};
let result: ProjectData | undefined;
if (recoveryStorage) {
const recoveryData = await this.__exec(recoveryStorage, this.getCurrentOptions(STORAGE_LOCAL));

77
test/specs/storage_manager/index.js → test/specs/storage_manager/index.ts

@ -1,15 +1,18 @@
import StorageManager from 'storage_manager';
import EditorModel from '../../../src/editor/model/Editor';
import StorageManager from '../../../src/storage_manager';
describe('Storage Manager', () => {
describe('Main', () => {
var obj;
let obj: StorageManager;
let em: EditorModel;
beforeEach(() => {
obj = new StorageManager().init();
em = new EditorModel();
obj = em.StorageManager;
});
afterEach(() => {
obj = null;
em.destroy();
});
test('Object exists', () => {
@ -21,7 +24,7 @@ describe('Storage Manager', () => {
});
test('Change autosave', () => {
obj.setAutosave(0);
obj.setAutosave(false);
expect(obj.isAutosave()).toEqual(false);
});
@ -35,8 +38,14 @@ describe('Storage Manager', () => {
});
test('Add and get new storage', () => {
obj.add('test', 'gen');
expect(obj.get('test')).toEqual('gen');
expect(obj.get('test')).toBe(undefined);
obj.add('test', {
async load() {
return {};
},
async store() {},
});
expect(obj.get('test')).toBeDefined();
});
test('LocalStorage is set as default', () => {
@ -61,44 +70,46 @@ describe('Storage Manager', () => {
});
describe('With custom storage', () => {
var storeValue;
var storageId = 'testStorage';
var storage = {
store(data) {
storeValue = data;
},
load(keys) {
return storeValue;
},
};
let storeValue = {};
const storageId = 'testStorage';
beforeEach(() => {
storeValue = [];
obj = new StorageManager().init({
type: storageId,
em = new EditorModel({
storageManager: {
type: storageId,
},
});
obj = em.StorageManager;
obj.add(storageId, {
async store(data) {
storeValue = data;
},
async load() {
return storeValue;
},
});
obj.add(storageId, storage);
});
afterEach(() => {
obj = null;
em.destroy();
});
test('Store and load data', () => {
var data = {
test('Check custom storage is enabled', () => {
expect(obj.getCurrent()).toEqual(storageId);
expect(obj.getCurrentStorage()).toBeDefined();
});
test('Store and load data', async () => {
const data = {
item: 'testData',
item2: 'testData2',
};
var data2 = {};
var id = obj.getConfig().id;
data2[id + 'item'] = 'testData';
data2[id + 'item2'] = 'testData2';
obj.store(data);
obj.load(['item', 'item2'], res => {
expect(storeValue).toEqual(data2);
expect(res).toEqual(data);
});
await obj.store(data);
expect(storeValue).toEqual(data);
const res = await obj.load();
expect(res).toEqual(data);
});
});
});
Loading…
Cancel
Save