Skip to content

Commit

Permalink
fix: children one layer deep cannot resolve their parent controller (#…
Browse files Browse the repository at this point in the history
…662)

* fix: children one layer deep cannot resolve their parent controller/serializer

* test: add unit tests for create-parent-builder util
  • Loading branch information
zacharygolba authored Jan 24, 2017
1 parent c8f0254 commit d9c538e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
71 changes: 71 additions & 0 deletions src/packages/loader/builder/test/create-parent-builder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// @flow
import { posix } from 'path';

import { expect } from 'chai';
import { describe, it, beforeEach } from 'mocha';

import Controller from '../../../controller';
import Serializer from '../../../serializer';
import { FreezeableMap } from '../../../freezeable';
import createParentBuilder from '../utils/create-parent-builder';

describe('module "loader/builder"', () => {
describe('util createParentBuilder()', () => {
let subject;

class ApplicationController extends Controller {}
class ApiApplicationController extends Controller {}
class ApiV1ApplicationController extends Controller {}

beforeEach(() => {
subject = createParentBuilder((key, target, parent) => {
const namespace = posix.dirname(key).replace('.', '');

// $FlowIgnore
const serializer = new Serializer({
namespace,
model: null,
parent: null
});

return Reflect.construct(target, [{
parent,
namespace,
serializer,
model: null
}]);
});
});

it('correctly builds parent objects', () => {
subject(new FreezeableMap([
['root', new FreezeableMap([
['application', ApplicationController]
])],
['api', new FreezeableMap([
['application', ApiApplicationController]
])],
['api/v1', new FreezeableMap([
['application', ApiV1ApplicationController]
])]
])).forEach(({ key, parent }) => {
switch (key) {
case 'root':
expect(parent).to.be.an.instanceOf(ApplicationController);
break;

case 'api':
expect(parent).to.be.an.instanceOf(ApiApplicationController);
break;

case 'api/v1':
expect(parent).to.be.an.instanceOf(ApiV1ApplicationController);
break;

default:
throw new Error(`Unexpected key "${key}".`);
}
});
});
});
});
12 changes: 9 additions & 3 deletions src/packages/loader/builder/utils/create-parent-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ export default function createParentBuilder<T>(
let grandparent = null;

if (key !== 'root') {
grandparent = result.find(namespace => (
namespace.key === posix.dirname(key)
));
grandparent = result.find(namespace => {
const dirname = posix.dirname(key);

if (namespace.key === 'root') {
return dirname === '.';
}

return dirname === namespace.key;
});

if (grandparent) {
grandparent = grandparent.parent;
Expand Down

0 comments on commit d9c538e

Please sign in to comment.