Skip to content

Commit

Permalink
feat(Aurelia): ability to define root with constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed May 31, 2018
1 parent 2275a51 commit 15fc9dd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/aurelia.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class Aurelia {
* @param applicationHost The DOM object that Aurelia will attach to.
* @return Returns a Promise of the current Aurelia instance.
*/
setRoot(root: string = null, applicationHost: string | Element = null): Promise<Aurelia> {
setRoot(root: string | Function = null, applicationHost: string | Element = null): Promise<Aurelia> {
let instruction = {};

if (this.root && this.root.viewModel && this.root.viewModel.router) {
Expand Down
63 changes: 57 additions & 6 deletions test/aurelia.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Container } from 'aurelia-dependency-injection';
import { Loader } from 'aurelia-loader';
import { DOM, PLATFORM } from 'aurelia-pal';
import { BindingLanguage, ViewResources, ViewSlot, inlineView } from 'aurelia-templating';
import { Aurelia } from '../src/aurelia';
import { FrameworkConfiguration } from '../src/framework-configuration';
import './setup';
import {Aurelia} from '../src/aurelia';
import {Container} from 'aurelia-dependency-injection';
import {Loader} from 'aurelia-loader';
import {BindingLanguage, ViewSlot, ViewResources, CompositionEngine} from 'aurelia-templating';
import {FrameworkConfiguration} from '../src/framework-configuration';
import {DOM, PLATFORM} from 'aurelia-pal';

describe('aurelia', () => {
describe("constructor", () => {
Expand Down Expand Up @@ -204,6 +204,57 @@ describe('aurelia', () => {
});

});

it('should accept view model class as root', (done) => {

const emptyMetadata = Object.freeze({});
const metadataContainerKey = '__metadata__';

Reflect.getOwnMetadata = function(metadataKey, target, targetKey) {
if (target.hasOwnProperty(metadataContainerKey)) {
return (target[metadataContainerKey][targetKey] || emptyMetadata)[metadataKey];
}
};

Reflect.defineMetadata = function(metadataKey, metadataValue, target, targetKey) {
let metadataContainer = target.hasOwnProperty(metadataContainerKey) ? target[metadataContainerKey] : (target[metadataContainerKey] = {});
let targetContainer = metadataContainer[targetKey] || (metadataContainer[targetKey] = {});
targetContainer[metadataKey] = metadataValue;
};

Reflect.metadata = function(metadataKey, metadataValue) {
return function(target, targetKey) {
Reflect.defineMetadata(metadataKey, metadataValue, target, targetKey);
};
};

let documentSpy = spyOn(document, "getElementById").and.returnValue(document.body);

@inlineView('<template>Hello</template>')
class App {}

aurelia = new Aurelia({});
aurelia.use.instance(BindingLanguage, {
inspectTextContent() {
return null;
}
})

aurelia.setRoot(App)
.then(aurelia => {
expect(documentSpy).toHaveBeenCalledWith("applicationHost");
expect(aurelia.root.viewModel.constructor).toBe(App);
})
.catch((ex) => {
expect(ex).toBeFalsy("It should have composed");
})
.then(() => {
Reflect.getOwnMetadata = null;
Reflect.defineMetadata = null;
Reflect.metadata = null;
done();
});
});
});

describe('enhance()', () => {
Expand Down

0 comments on commit 15fc9dd

Please sign in to comment.