Skip to content

Commit

Permalink
[ui/uiApp] add tests to verify #getModules() sorts modules
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Jan 9, 2018
1 parent b7ce7c3 commit fd24ae5
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/ui/ui_apps/__tests__/ui_app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import sinon from 'sinon';
import expect from 'expect.js';
import Chance from 'chance';

import { UiApp } from '../ui_app';
import { UiNavLink } from '../../ui_nav_links';

const chance = new Chance();

function createStubUiAppSpec(extraParams) {
return {
id: 'uiapp-test',
Expand Down Expand Up @@ -51,7 +54,7 @@ function createUiApp(spec = createStubUiAppSpec(), kbnServer = createStubKbnServ
return new UiApp(kbnServer, spec);
}

describe('UiApp', () => {
describe('ui apps / UiApp', () => {
describe('constructor', () => {
it('throws an exception if an ID is not given', () => {
const spec = {}; // should have id property
Expand Down Expand Up @@ -354,4 +357,45 @@ describe('UiApp', () => {
});
});
});

describe('#getModules', () => {
it('returns empty array by default', () => {
const app = createUiApp({ id: 'foo' });
expect(app.getModules()).to.eql([]);
});

it('returns main module if not using appExtensions', () => {
const app = createUiApp({ id: 'foo', main: 'bar' });
expect(app.getModules()).to.eql(['bar']);
});

it('returns appExtensions for used types only, in alphabetical order, starting with main module', () => {
const kbnServer = createStubKbnServer();
kbnServer.uiExports.appExtensions = {
abc: chance.shuffle([
'a',
'b',
'c',
]),
def: chance.shuffle([
'd',
'e',
'f',
])
};

const appExtensionType = chance.shuffle(Object.keys(kbnServer.uiExports.appExtensions))[0];
const appSpec = {
id: 'foo',
main: 'bar',
uses: [appExtensionType],
};

const app = createUiApp(appSpec, kbnServer);
expect(app.getModules()).to.eql([
'bar',
...appExtensionType.split(''),
]);
});
});
});
30 changes: 30 additions & 0 deletions src/ui/ui_bundles/__tests__/app_entry_template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sinon from 'sinon';
import expect from 'expect.js';

import { appEntryTemplate } from '../app_entry_template';

function createMockBundle() {
return {
getContext: sinon.stub().returns(''),
getRequires: sinon.stub().returns([])
};
}

describe('ui bundles / appEntryTemplate', () => {
it('embeds bundle.getContext() result', () => {
const bundle = createMockBundle();
bundle.getContext.returns('foo bar baz');
expect(appEntryTemplate(bundle)).to.contain('foo bar baz');
});

it('joins requires into list', () => {
const bundle = createMockBundle();
const requires = [
'foo',
'bar',
'baz'
];
bundle.getRequires.returns(requires);
expect(appEntryTemplate(bundle)).to.contain(requires.join('\n'));
});
});
53 changes: 53 additions & 0 deletions src/ui/ui_bundles/__tests__/ui_bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import expect from 'expect.js';

import { UiBundle } from '../ui_bundle';

describe('ui bundles / UiBundle', () => {
describe('#getRequires', () => {
it('returns modules option as a list of require calls', () => {
const bundle = new UiBundle({
modules: [
'a',
'b',
'c'
]
});

expect(bundle.getRequires()).to.eql([
`require('a');`,
`require('b');`,
`require('c');`,
]);
});

it('does not sort modules', () => {
const bundle = new UiBundle({
modules: [
'c',
'a',
'b'
]
});

expect(bundle.getRequires()).to.eql([
`require('c');`,
`require('a');`,
`require('b');`,
]);
});

it('converts \\ to /', () => {
const bundle = new UiBundle({
modules: [
'a\\b\\c',
'd/e/f',
]
});

expect(bundle.getRequires()).to.eql([
`require('a/b/c');`,
`require('d/e/f');`,
]);
});
});
});

0 comments on commit fd24ae5

Please sign in to comment.