Skip to content

Commit

Permalink
test: snapshooter (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
miralemd committed Dec 16, 2019
1 parent 826072e commit ea95946
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 16 deletions.
131 changes: 131 additions & 0 deletions apis/snapshooter/src/__tests__/renderer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import renderer from '../renderer';

describe('snapshooter', () => {
let sandbox;
let nucleus;
let nebbie;
before(() => {
sandbox = sinon.createSandbox();
global.window = { location: { search: '?snapshot=abc' } };
});
after(() => {
global.window = undefined;
});

beforeEach(() => {
nucleus = sandbox.stub();
nucleus.config = {
snapshot: {
get: sandbox.stub(),
},
};

nebbie = {
get: sandbox.stub().returns(Promise.resolve()),
};
nucleus.returns(nebbie);

nucleus.config.snapshot.get.returns(
Promise.resolve({
meta: { theme: 'dark', language: 'sv' },
layout: {
cube: 'c',
qInfo: {
qId: 'xyz',
},
},
})
);
});

afterEach(() => {
sandbox.reset();
});

it('should get snapshot with id "abc"', async () => {
await renderer({ nucleus });
expect(nucleus.config.snapshot.get).to.have.been.calledWithExactly('abc');
});

it('should catch snapshot get errors and render the error', async () => {
nucleus.config.snapshot.get.throws(new Error('meh'));
const element = {
setAttribute: sandbox.stub(),
};
try {
await renderer({ nucleus, element });
expect(1).to.equal('a'); // just to make sure this test fails if error is not trown
} catch (e) {
/* */
}
expect(element.setAttribute).to.have.been.calledWithExactly('data-njs-error', 'meh');
expect(element.innerHTML).to.eql('<p>meh</p>');
});

it('should call nucleus with context theme and language', async () => {
await renderer({ nucleus });
expect(nucleus.firstCall.args[1]).to.eql({
context: {
theme: 'dark',
language: 'sv',
},
});
});

it('should mock an app that returns a mocked model', async () => {
await renderer({ nucleus });
const app = nucleus.firstCall.args[0];
const model = await app.getObject('xyz');
expect(model.getLayout).to.be.a('function');
expect(model.on).to.be.a('function');
expect(model.once).to.be.a('function');
});

it('the mocked model should return the snapshot as layout', async () => {
await renderer({ nucleus });
const app = nucleus.firstCall.args[0];
const model = await app.getObject('xyz');
const ly = await model.getLayout();
expect(ly).to.eql({
cube: 'c',
qInfo: {
qId: 'xyz',
},
});
});

it('should reject mocked getObject when id is not matching qId', async () => {
await renderer({ nucleus });
const app = nucleus.firstCall.args[0];
try {
await app.getObject('unknown');
expect(1).to.equal(0); // should never reach this point
} catch (e) {
expect(e.message).to.equal('Could not find an object with id: unknown');
}
});

it('should call nebbie.get()', async () => {
const el = 'el';
await renderer({ nucleus, element: el });
expect(nebbie.get).to.have.been.calledWithExactly(
{
id: 'xyz',
},
{ element: el }
);
});

it('should render error when nebbie.get() throws', async () => {
const el = {
setAttribute: sandbox.stub(),
};
nebbie.get.throws(new Error('aaaaaaah!'));
try {
await renderer({ nucleus, element: el });
} catch (e) {
expect(e.message).to.eql('aaaaaaah!');
}
expect(el.setAttribute).to.have.been.calledWithExactly('data-njs-error', 'aaaaaaah!');
});
});
25 changes: 9 additions & 16 deletions apis/snapshooter/src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
async function renderSnapshot({ nucleus, element }) {
const params = (() => {
const opts = {};
const { pathname } = window.location;
const am = pathname.match(/\/app\/([^/?&]+)/);
if (am) {
opts.app = decodeURIComponent(am[1]);
}
window.location.search
.substring(1)
.split('&')
Expand All @@ -22,9 +17,7 @@ async function renderSnapshot({ nucleus, element }) {

let snapshot = {};
const renderError = e => {
element.innerHTML = `
<p>${e.message}</p>
`;
element.innerHTML = `<p>${e.message}</p>`;
element.setAttribute('data-njs-error', e.message);
};
try {
Expand Down Expand Up @@ -52,7 +45,7 @@ async function renderSnapshot({ nucleus, element }) {
if (id === layout.qInfo.qId) {
return objectModel;
}
return Promise.reject();
return Promise.reject(new Error(`Could not find an object with id: ${id}`));
},
};

Expand All @@ -63,19 +56,19 @@ async function renderSnapshot({ nucleus, element }) {
},
});

nebbie
.get(
try {
await nebbie.get(
{
id: layout.qInfo.qId,
},
{
element,
}
)
.catch(e => {
renderError(e || { message: 'Failed to render supernova' });
throw e;
});
);
} catch (e) {
renderError(e || { message: 'Failed to render supernova' });
throw e;
}
}

export default renderSnapshot;

0 comments on commit ea95946

Please sign in to comment.