Skip to content

Commit

Permalink
refactor: improve naming of instance context (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
miralemd committed Dec 12, 2019
1 parent 0359ab6 commit ce43f0c
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 136 deletions.
2 changes: 1 addition & 1 deletion apis/nucleus/src/__tests__/viz.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('viz', () => {
};
api = create({
model,
context: {},
corona: { public: {} },
});
});
after(() => {
Expand Down
12 changes: 7 additions & 5 deletions apis/nucleus/src/components/Cell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,13 @@ const loadType = async ({ dispatch, types, name, version, layout, model, app })
return undefined;
};

const Cell = forwardRef(({ nebulaContext, model, initialSnContext, initialSnOptions, initialError, onMount }, ref) => {
const Cell = forwardRef(({ corona, model, initialSnContext, initialSnOptions, initialError, onMount }, ref) => {
const {
app,
nebbie: { types },
} = nebulaContext;
public: {
nebbie: { types },
},
} = corona;

const translator = useContext(LocaleContext);
const theme = useTheme();
Expand Down Expand Up @@ -258,11 +260,11 @@ const Cell = forwardRef(({ nebulaContext, model, initialSnContext, initialSnOpti
};
},
async exportImage() {
if (!nebulaContext.snapshot.capture) {
if (!corona.config.snapshot.capture) {
throw new Error('Nebula has not been configured with snapshot.capture');
}
const snapshot = await this.takeSnapshot(); // eslint-disable-line
return nebulaContext.snapshot.capture(snapshot);
return corona.config.snapshot.capture(snapshot);
},
}),
[state.sn, contentRect, layout, theme.name]
Expand Down
20 changes: 12 additions & 8 deletions apis/nucleus/src/components/__tests__/cell.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ describe('<Cell />', () => {
})
),
};
const defaultNebulaContext = {
const defaultCorona = {
app: {},
nebbie: {
types: {
getSupportedVersion: sandbox.stub(),
public: {
nebbie: {
types: {
getSupportedVersion: sandbox.stub(),
},
},
},
};
Expand All @@ -66,10 +68,12 @@ describe('<Cell />', () => {
...defaultModel,
...model,
};
const nebulaContext = {
...defaultNebulaContext,
const corona = {
...defaultCorona,
...app,
...(nebbie.types ? { nebbie } : {}),
public: {
nebbie: nebbie.types ? nebbie : { ...defaultCorona.public.nebbie },
},
};

await act(async () => {
Expand All @@ -78,7 +82,7 @@ describe('<Cell />', () => {
<LocaleContext.Provider value={{ get: s => s, language: () => 'sv' }}>
<Cell
ref={cellRef}
nebulaContext={nebulaContext}
corona={corona}
model={model}
initialSnContext={initialSnContext}
initialSnOptions={initialSnOptions}
Expand Down
6 changes: 3 additions & 3 deletions apis/nucleus/src/components/__tests__/glue.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('glue', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
param = {
nebulaContext: {
corona: {
root: {
add: sandbox.spy(),
remove: sandbox.spy(),
Expand All @@ -34,7 +34,7 @@ describe('glue', () => {
it('should glue outside world with react world', () => {
const [dissolve] = glue(param);
dissolve();
expect(param.nebulaContext.root.add.callCount).to.equal(1);
expect(param.nebulaContext.root.remove.callCount).to.equal(1);
expect(param.corona.root.add.callCount).to.equal(1);
expect(param.corona.root.remove.callCount).to.equal(1);
});
});
14 changes: 3 additions & 11 deletions apis/nucleus/src/components/glue.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@ import React from 'react';
import ReactDOM from 'react-dom';
import Cell from './Cell';

export default function glue({
nebulaContext,
element,
model,
initialSnContext,
initialSnOptions,
onMount,
initialError,
}) {
const { root } = nebulaContext;
export default function glue({ corona, element, initialSnContext, model, initialSnOptions, onMount, initialError }) {
const { root } = corona;
const cellRef = React.createRef();
const portal = ReactDOM.createPortal(
<Cell
ref={cellRef}
nebulaContext={nebulaContext}
corona={corona}
model={model}
initialSnContext={initialSnContext}
initialSnOptions={initialSnOptions}
Expand Down
75 changes: 36 additions & 39 deletions apis/nucleus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,37 +99,10 @@ const mergeConfigs = (base, c) => ({
},
});

function nuked(configuration = {}, prev = {}) {
function nuked(configuration = {}) {
const logger = loggerFn(configuration.log);
const locale = appLocaleFn(configuration.locale);

const config = {
env: {
// env is provided as is to the supernova method
// consider it part of the public API
Promise,
translator: locale.translator,
nucleus, // eslint-disable-line no-use-before-define
},
load: configuration.load,
logger,
};

const types = typesFn({ config, parent: prev.types });

configuration.types.forEach(t =>
types.register(
{
name: t.name,
version: t.version,
},
{
meta: t.meta,
load: t.load,
}
)
);

/**
* Initiates a new `nebbie` instance using the specified `app`.
* @entry
Expand Down Expand Up @@ -160,17 +133,41 @@ function nuked(configuration = {}, prev = {}) {
root,
});

const context = {
nebbie: null,
const publicAPIs = {
env: {
Promise, // TODO - deprecate
translator: locale.translator,
nucleus, // eslint-disable-line no-use-before-define
},
theme: appTheme.externalAPI,
translator: locale.translator,
nebbie: null, // actual value is set further down
};

const corona = {
app,
config,
logger,
types,
root,
theme: appTheme.externalAPI,
snapshot: configuration.snapshot,
logger,
config: configuration,
public: publicAPIs,
nebbie: null,
};

const types = typesFn({ corona });

configuration.types.forEach(t =>
types.register(
{
name: t.name,
version: t.version,
},
{
meta: t.meta,
load: t.load,
}
)
);

let currentThemePromise = appTheme.setTheme(configuration.theme);

let selectionsApi = null;
Expand All @@ -188,7 +185,7 @@ function nuked(configuration = {}, prev = {}) {
*/
get: async (getCfg, vizConfig) => {
await currentThemePromise;
return get(getCfg, vizConfig, context);
return get(getCfg, vizConfig, corona);
},
/**
* @param {CreateObjectConfig} createCfg
Expand All @@ -197,7 +194,7 @@ function nuked(configuration = {}, prev = {}) {
*/
create: async (createCfg, vizConfig) => {
await currentThemePromise;
return create(createCfg, vizConfig, context);
return create(createCfg, vizConfig, corona);
},
theme(themeName) {
currentThemePromise = appTheme.setTheme(themeName);
Expand Down Expand Up @@ -242,10 +239,10 @@ function nuked(configuration = {}, prev = {}) {
}
return selectionsApi;
},
types: context.types,
types,
};

context.nebbie = api;
corona.public.nebbie = api;

return api;
}
Expand Down
38 changes: 20 additions & 18 deletions apis/nucleus/src/object/__tests__/create-object.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('create-object', () => {
let context = {};
let corona = {};
let types;
let sn;
let merged;
Expand All @@ -26,12 +26,14 @@ describe('create-object', () => {
types = {
get: sandbox.stub(),
};
context = {
corona = {
app: {
createSessionObject: sandbox.stub().returns(Promise.resolve(objectModel)),
},
nebbie: {
types,
public: {
nebbie: {
types,
},
},
};

Expand All @@ -51,60 +53,60 @@ describe('create-object', () => {
});

it('should call types.get with name and version', () => {
create({ type: 't', version: 'v', fields: 'f' }, {}, context);
create({ type: 't', version: 'v', fields: 'f' }, {}, corona);
expect(types.get).to.have.been.calledWithExactly({ name: 't', version: 'v' });
});

it('should call initialProperties on returned type', () => {
const t = { initialProperties: sinon.stub() };
t.initialProperties.returns({ then: () => {} });
types.get.returns(t);
create({ type: 't', version: 'v', fields: 'f' }, { properties: 'props' }, context);
create({ type: 't', version: 'v', fields: 'f' }, { properties: 'props' }, corona);
expect(t.initialProperties).to.have.been.calledWithExactly('props');
});

it('should populate fields', async () => {
await create({ type: 't', version: 'v', fields: 'f' }, { properties: 'props' }, context);
expect(populator).to.have.been.calledWithExactly({ sn, properties: merged, fields: 'f' }, context);
await create({ type: 't', version: 'v', fields: 'f' }, { properties: 'props' }, corona);
expect(populator).to.have.been.calledWithExactly({ sn, properties: merged, fields: 'f' }, corona);
});

it('should call properties onChange handler when optional props are provided', async () => {
await create({ type: 't', version: 'v', fields: 'f' }, { properties: 'props' }, context);
await create({ type: 't', version: 'v', fields: 'f' }, { properties: 'props' }, corona);
expect(sn.qae.properties.onChange).to.have.been.calledWithExactly(merged);
});

it('should not call onChange handler when optional props are not provided', async () => {
await create({ type: 't', version: 'v', fields: 'f' }, {}, context);
await create({ type: 't', version: 'v', fields: 'f' }, {}, corona);
expect(sn.qae.properties.onChange.callCount).to.equal(0);
});

it('should create a session object with merged props', async () => {
await create({ type: 't', version: 'v', fields: 'f' }, { properties: 'props' }, context);
expect(context.app.createSessionObject).to.have.been.calledWithExactly(merged);
await create({ type: 't', version: 'v', fields: 'f' }, { properties: 'props' }, corona);
expect(corona.app.createSessionObject).to.have.been.calledWithExactly(merged);
});

it('should create a dummy session object when error is thrown', async () => {
types.get.throws('oops');
await create({ type: 't', version: 'v', fields: 'f' }, { properties: 'props' }, context);
expect(context.app.createSessionObject).to.have.been.calledWithExactly({
await create({ type: 't', version: 'v', fields: 'f' }, { properties: 'props' }, corona);
expect(corona.app.createSessionObject).to.have.been.calledWithExactly({
qInfo: { qType: 't' },
visualization: 't',
});
});

it('should call init', async () => {
const optional = { properties: 'props', x: 'a' };
const ret = await create({ type: 't', version: 'v', fields: 'f' }, optional, context);
const ret = await create({ type: 't', version: 'v', fields: 'f' }, optional, corona);
expect(ret).to.equal('api');
expect(init).to.have.been.calledWithExactly(objectModel, optional, context, undefined);
expect(init).to.have.been.calledWithExactly(objectModel, optional, corona, undefined);
});

it('should catch and pass error', async () => {
const err = new Error('oops');
types.get.throws(err);
const optional = { properties: 'props' };
const ret = await create({ type: 't' }, optional, context);
const ret = await create({ type: 't' }, optional, corona);
expect(ret).to.equal('api');
expect(init).to.have.been.calledWithExactly(objectModel, optional, context, err);
expect(init).to.have.been.calledWithExactly(objectModel, optional, corona, err);
});
});
12 changes: 6 additions & 6 deletions apis/nucleus/src/object/__tests__/initiate.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('initiate api', () => {
const optional = 'optional';
const context = 'context';
const corona = 'corona';
const model = 'model';
let create;
let sandbox;
Expand All @@ -27,23 +27,23 @@ describe('initiate api', () => {

it('should call viz api', async () => {
const initialError = 'err';
const ret = await create(model, optional, context, initialError);
expect(viz).to.have.been.calledWithExactly({ model, context, initialError });
const ret = await create(model, optional, corona, initialError);
expect(viz).to.have.been.calledWithExactly({ model, corona, initialError });
expect(ret).to.equal(api);
});

it('should call mount when element is provided ', async () => {
await create(model, { element: 'el' }, context);
await create(model, { element: 'el' }, corona);
expect(api.mount).to.have.been.calledWithExactly('el');
});

it('should call options when provided ', async () => {
await create(model, { options: 'opts' }, context);
await create(model, { options: 'opts' }, corona);
expect(api.options).to.have.been.calledWithExactly('opts');
});

it('should call context when provided ', async () => {
await create(model, { context: 'ctx' }, context);
await create(model, { context: 'ctx' }, corona);
expect(api.context).to.have.been.calledWithExactly('ctx');
});
});
Loading

0 comments on commit ce43f0c

Please sign in to comment.