Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should save data', async () => {
const ready = new PromiseDelegate();
const restorer = new LayoutRestorer({
connector: new StateDB(),
first: ready.promise,
registry: new CommandRegistry()
});
const currentWidget = new Widget();
// The `fresh` attribute is only here to check against the return value.
const dehydrated: ILabShell.ILayout = {
fresh: false,
mainArea: { currentWidget: null, dock: null, mode: null },
leftArea: {
currentWidget,
collapsed: true,
widgets: [currentWidget]
},
rightArea: { collapsed: true, currentWidget: null, widgets: null }
};
it('should construct a new layout restorer', () => {
const restorer = new LayoutRestorer({
connector: new StateDB(),
first: Promise.resolve(void 0),
registry: new CommandRegistry()
});
expect(restorer).to.be.an.instanceof(LayoutRestorer);
});
});
it('should be a promise available right away', () => {
const restorer = new LayoutRestorer({
connector: new StateDB(),
first: Promise.resolve(void 0),
registry: new CommandRegistry()
});
expect(restorer.restored).to.be.an.instanceof(Promise);
});
it('should resolve when restorer is done', async () => {
const ready = new PromiseDelegate();
const restorer = new LayoutRestorer({
first: ready.promise,
registry: new CommandRegistry(),
state: new StateDB()
});
let promise = restorer.restored;
ready.resolve(void 0);
await promise;
});
});
it('should restore the widgets in a tracker', async () => {
const tracker = new WidgetTracker({ namespace: 'foo-widget' });
const registry = new CommandRegistry();
const state = new StateDB();
const ready = new PromiseDelegate();
const restorer = new LayoutRestorer({
connector: state,
first: ready.promise,
registry
});
let called = false;
const key = `${tracker.namespace}:${tracker.namespace}`;
registry.addCommand(tracker.namespace, {
execute: () => {
called = true;
}
});
await state.save(key, { data: null });
ready.resolve(undefined);
await restorer.restore(tracker, {
name: () => tracker.namespace,
it('should add a widget to be tracked by the restorer', async () => {
const ready = new PromiseDelegate();
const restorer = new LayoutRestorer({
first: ready.promise,
registry: new CommandRegistry(),
state: new StateDB()
});
const currentWidget = new Widget();
const mode: DockPanel.Mode = 'single-document';
const dehydrated: ILabShell.ILayout = {
mainArea: { currentWidget, dock: null, mode },
leftArea: { collapsed: true, currentWidget: null, widgets: null },
rightArea: { collapsed: true, currentWidget: null, widgets: null }
};
restorer.add(currentWidget, 'test-one');
ready.resolve(void 0);
await restorer.restored;
await restorer.save(dehydrated);
const layout = await restorer.fetch();
it('should always return a value', async () => {
const restorer = new LayoutRestorer({
connector: new StateDB(),
first: Promise.resolve(void 0),
registry: new CommandRegistry()
});
const layout = await restorer.fetch();
expect(layout).to.not.equal(null);
});
it('should restore the widgets in a tracker', async () => {
const tracker = new InstanceTracker({
namespace: 'foo-widget'
});
const registry = new CommandRegistry();
const state = new StateDB();
const ready = new PromiseDelegate();
const restorer = new LayoutRestorer({
first: ready.promise,
registry,
state
});
let called = false;
const key = `${tracker.namespace}:${tracker.namespace}`;
registry.addCommand(tracker.namespace, {
execute: () => {
called = true;
}
});
await state.save(key, { data: null });
ready.resolve(undefined);
await restorer.restore(tracker, {
args: () => null,
activate: (app: JupyterLab, state: IStateDB) => {
const first = app.started;
const registry = app.commands;
const restorer = new LayoutRestorer({ first, registry, state });
restorer.fetch().then(saved => {
app.shell.restoreLayout(saved);
app.shell.layoutModified.connect(() => {
restorer.save(app.shell.saveLayout());
});
});
return restorer;
},
autoStart: true,
activate: (app: JupyterLab, state: IStateDB) => {
const first = app.started;
const registry = app.commands;
const restorer = new LayoutRestorer({ first, registry, state });
restorer.fetch().then(saved => {
app.shell.restoreLayout(saved);
app.shell.layoutModified.connect(() => {
restorer.save(app.shell.saveLayout());
});
});
return restorer;
},
autoStart: true,