Skip to content

Commit

Permalink
add tests for server
Browse files Browse the repository at this point in the history
  • Loading branch information
sometimeskind committed Feb 1, 2018
1 parent 4c87842 commit 07bbe65
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export default class AppManagerServer {
return params;
};

_parseQuery = (query: string | QueryType) => (typeof query === 'object' ? query : qs.parse(query));
_parseQuery = (query: string | QueryType = {}) => (typeof query === 'object' ? query : qs.parse(query));

init = async (path: string, query: string | QueryType, ...getMarkupArgs: Array<mixed>): Promise<any> => {
init = async (path: string, query?: string | QueryType, ...getMarkupArgs: Array<mixed>): Promise<any> => {
const currentAppName = this._getAppNameFromPath(path);

if (!currentAppName) {
Expand All @@ -87,7 +87,7 @@ export default class AppManagerServer {
const fragmentName = appFragments[slotName];
const fragment = this._fragments[fragmentName];

const markup = typeof fragment.getMarkup === 'function' ? await fragment.getMarkup(state, ...getMarkupArgs) : '';
const markup = fragment && typeof fragment.getMarkup === 'function' ? await fragment.getMarkup(state, ...getMarkupArgs) : '';

return { slotName, markup };
});
Expand Down
19 changes: 14 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "app-manager",
"version": "0.19.3",
"version": "0.19.4",
"description": "Script for managing the lifecycles of multiple apps on a single page",
"main": "es5/index.js",
"scripts": {
Expand Down Expand Up @@ -35,10 +35,10 @@
"homepage": "https://github.com/tomruttle/app-manager#readme",
"nyc": {
"check-coverage": true,
"lines": 70,
"statements": 70,
"lines": 80,
"statements": 80,
"functions": 100,
"branches": 64,
"branches": 70,
"include": [
"lib/**"
],
Expand All @@ -59,6 +59,7 @@
"babel-preset-flow": "^6.23.0",
"babel-register": "^6.26.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"env-test": "^1.0.0",
"eslint": "^4.16.0",
"eslint-config-airbnb-base": "^12.1.0",
Expand Down
94 changes: 94 additions & 0 deletions test/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// @flow

import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';

import AppManagerServer from '../lib/server';

chai.use(chaiAsPromised);

describe('Server', () => {
function getConfig() {
return {
apps: {
app_a: {
name: 'app_a',
fragments: ['fragment_a'],
appPath: '/app-a',
},
app_b: {
name: 'app_b',
fragments: ['fragment_b'],
appPath: '/app-b/:param1?',
},
},
fragments: {
fragment_a: {
name: 'fragment_a',
managed: true,
slots: ['app'],
loadScript: () => { throw new Error('Should not be called.'); },
getMarkup: sinon.stub().returns(Promise.resolve('fragment_a')),
},
fragment_b: {
name: 'fragment_b',
managed: true,
slots: ['app'],
loadScript: () => { throw new Error('Should not be called.'); },
getMarkup: sinon.stub().returns(Promise.resolve('fragment_b')),
},
},
slots: {
app: { name: 'app', querySelector: null },
other: { name: 'other', querySelector: null },
},
};
}

describe('init', () => {
it('rejects if path does not match any apps', () => {
const appManagerServer = new AppManagerServer(getConfig());
expect(appManagerServer.init('/app-c')).be.rejectedWith('server.invalid_appPath');
});

it('returns the async return value of getMarkup for the correct app in the correct slot', async () => {
const config = getConfig();
const appManagerServer = new AppManagerServer(config);
const appMarkup = await appManagerServer.init('/app-a');

expect(appMarkup).to.deep.equals({ app: 'fragment_a', other: '' });

const getMarkupStubB = config.fragments.fragment_b.getMarkup;
expect(getMarkupStubB.called).to.be.false;

const getMarkupStubA = config.fragments.fragment_a.getMarkup;
expect(getMarkupStubA.calledOnce).to.be.true;

const args = getMarkupStubA.args[0];
expect(args).to.be.an('array').with.length(1);
expect(args[0].app.name).to.equals('app_a');
});

it('can handle params, query params, and additional arguments', async () => {
const config = getConfig();
const appManagerServer = new AppManagerServer(config);
const appMarkup = await appManagerServer.init('/app-b/test', 'query=success', 'additional data');

expect(appMarkup).to.deep.equals({ app: 'fragment_b', other: '' });

const getMarkupStubA = config.fragments.fragment_a.getMarkup;
expect(getMarkupStubA.called).to.be.false;

const getMarkupStubB = config.fragments.fragment_b.getMarkup;
expect(getMarkupStubB.calledOnce).to.be.true;

const args = getMarkupStubB.args[0];
expect(args).to.be.an('array').with.length(2);
expect(args[0].app.name).to.equals('app_b');
expect(args[0].params).to.deep.equals({ param1: 'test' });
expect(args[0].query).to.deep.equals({ query: 'success' });
expect(args[1]).to.equals('additional data');
});
});
});

0 comments on commit 07bbe65

Please sign in to comment.