Skip to content

Commit

Permalink
fix: ensure loading new type (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
stoffeastrom authored Jul 2, 2020
1 parent 8c95dde commit 2ef51a1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
31 changes: 12 additions & 19 deletions apis/nucleus/src/components/Cell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const initialState = (err) => ({
longRunningQuery: false,
error: err ? { title: err.message } : null,
sn: null,
visualization: null,
});

const contentReducer = (state, action) => {
Expand All @@ -50,6 +51,7 @@ const contentReducer = (state, action) => {
longRunningQuery: false,
error: null,
sn: action.sn,
visualization: action.visualization,
};
}
case 'RENDER': {
Expand Down Expand Up @@ -253,14 +255,9 @@ const getType = async ({ types, name, version }) => {
return SN;
};

const loadType = async ({ dispatch, types, name, version, layout, model, app, selections }) => {
const loadType = async ({ dispatch, types, visualization, version, model, app, selections }) => {
try {
const snType = await getType({ types, name, version });
// Layout might have changed since we requested the new type -> quick return
if (layout.visualization !== name) {
return undefined;
}

const snType = await getType({ types, name: visualization, version });
const sn = snType.create({
model,
app,
Expand Down Expand Up @@ -310,7 +307,7 @@ const Cell = forwardRef(({ halo, model, initialSnOptions, initialError, onMount
};

useEffect(() => {
if (initialError || !appLayout) {
if (initialError || !appLayout || !layout) {
return undefined;
}
const validate = async (sn) => {
Expand All @@ -322,35 +319,31 @@ const Cell = forwardRef(({ halo, model, initialSnOptions, initialError, onMount
}
handleModal({ sn: state.sn, layout, model });
};
const load = async (withLayout, version) => {
const load = async (visualization, version) => {
dispatch({ type: 'LOADING' });
const sn = await loadType({
dispatch,
types,
name: withLayout.visualization,
visualization,
version,
layout,
model,
app,
selections,
});
if (sn) {
dispatch({ type: 'LOADED', sn });
dispatch({ type: 'LOADED', sn, visualization });
onMount();
}
return undefined;
};

if (!layout) {
return undefined;
}

if (state.sn) {
// Validate if it's still the same type
if (state.visualization === layout.visualization && state.sn) {
validate(state.sn);
return undefined;
}

// Load supernova h
// Load supernova
const withVersion = types.getSupportedVersion(layout.visualization, layout.version);
if (!withVersion) {
dispatch({
Expand All @@ -361,7 +354,7 @@ const Cell = forwardRef(({ halo, model, initialSnOptions, initialError, onMount
});
return undefined;
}
load(layout, withVersion);
load(layout.visualization, withVersion);

return () => {};
}, [types, state.sn, model, layout, appLayout, language]);
Expand Down
38 changes: 37 additions & 1 deletion apis/nucleus/src/components/__tests__/cell.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('<Cell />', () => {
Header = () => 'Header';
InstanceContext = React.createContext();
appLayout = { foo: 'app-layout' };
layout = { qSelectionInfo: {} };
layout = { qSelectionInfo: {}, visualization: '' };
layoutState = { validating: true, canCancel: false, canRetry: false };
longrunning = { cancel: sandbox.spy(), retry: sandbox.spy() };
useLayout = sandbox.stub().returns([layout, layoutState, longrunning]);
Expand Down Expand Up @@ -223,6 +223,42 @@ describe('<Cell />', () => {
expect(ftypes).to.have.length(1);
});

it('should render new type', async () => {
const sn = {
generator: {
qae: {
data: {
targets: [],
},
},
},
};
const sn1 = {
generator: {
qae: {
data: {
targets: [],
},
},
},
};
const types = {
get: sandbox.stub().callsFake(({ name }) => {
return {
supernova: async () => ({ create: () => (name === 'sn1' ? sn1 : sn) }),
};
}),
getSupportedVersion: sandbox.stub().returns('1.0.0'),
};
await render({ types });
const renderedSn = renderer.root.findByType(Supernova);
expect(renderedSn.props.sn).to.equal(sn);
sandbox.stub(layout, 'visualization').value('sn1');
await render({ types });
const renderedSn1 = renderer.root.findByType(Supernova);
expect(renderedSn1.props.sn).to.equal(sn1);
});

it('should render requirements', async () => {
const localLayout = { visualization: 'sn', foo: { qDimensionInfo: [], qMeasureInfo: [] } };
const sn = {
Expand Down
4 changes: 2 additions & 2 deletions test/mashup/visualize/life.int.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ describe('object lifecycle', () => {
await waitForTextStatus('.pages', '4001', { timeout: 5000 });
});

it.skip('should render when requirements are fulfilled', async () => {
it('should render other type', async () => {
await page.click('button[data-phase="set-the-other-type"]');
await waitForTextStatus('.njs-viz', 'The other type!');
await waitForTextStatus('.njs-viz', 'The other one!');
});

it('should destroy', async () => {
Expand Down

0 comments on commit 2ef51a1

Please sign in to comment.