Skip to content

Commit

Permalink
feat(enhance): use document.body as fallback
Browse files Browse the repository at this point in the history
When enhancing a page, if no applicationHost is specified (either string
for the id of the element, or an element itself) enhance will use the
body as the host.
  • Loading branch information
plwalters committed Mar 9, 2016
1 parent 435bf14 commit e1cbfcc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/aurelia.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class Aurelia {
* @return Returns a Promise for the current Aurelia instance.
*/
enhance(bindingContext: Object = {}, applicationHost: string | Element = null): Promise<Aurelia> {
this._configureHost(applicationHost);
this._configureHost(applicationHost || DOM.querySelectorAll('body')[0]);

return new Promise(resolve => {
let engine = this.container.get(TemplatingEngine);
Expand Down Expand Up @@ -142,7 +142,6 @@ export class Aurelia {
if (this.hostConfigured) {
return;
}

applicationHost = applicationHost || this.host;

if (!applicationHost || typeof applicationHost === 'string') {
Expand Down
73 changes: 72 additions & 1 deletion test/aurelia.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 {PLATFORM} from 'aurelia-pal';
import {DOM, PLATFORM} from 'aurelia-pal';

describe('aurelia', () => {
describe("constructor", () => {
Expand Down Expand Up @@ -205,4 +205,75 @@ describe('aurelia', () => {

});
});

describe('enhance()', () => {
let aurelia, mockContainer, mockLoader, mockResources, mockPlugin, mockViewEngine, mockTemplatingEngine;
let rootStub = {
attached() {}
};

beforeEach(() => {
mockLoader = jasmine.createSpy('loader');

mockViewEngine = jasmine.createSpyObj("viewEngine", ['importViewResources', 'enhance']);
mockViewEngine.importViewResources.and.returnValue(new Promise((resolve, error) => {
resolve();
}));

mockViewEngine.enhance.and.returnValue(rootStub);

mockContainer = jasmine.createSpyObj('container', ['registerInstance', 'hasResolver', 'get', 'makeGlobal']);
mockContainer.hasResolver.and.returnValue(true);
mockContainer.get.and.returnValue(mockViewEngine);

mockResources = jasmine.createSpy('viewResources');

mockPlugin = jasmine.createSpyObj('plugin', ['apply']);
mockPlugin.apply.and.returnValue(new Promise((resolve, error) => {
resolve();
}));

aurelia = new Aurelia(mockLoader, mockContainer, mockResources);
aurelia.use = mockPlugin;
});

describe('when passing in no arguments', () => {
let result;

it('configures body as host', () => {
let documentSpy = spyOn(document, "querySelectorAll").and.returnValue([document.body]);
spyOn(aurelia, '_configureHost');
result = aurelia.enhance();
expect(aurelia._configureHost).toHaveBeenCalledWith(document.body);
});
});

describe('when passing in bindingContext and string for Id', () => {
let result;

it('configures body as host', () => {
let elId = 'Testing';
let fakeElement = DOM.createElement('div');
fakeElement.setAttribute('id', elId);
let documentSpy = spyOn(document, "getElementById").and.returnValue(fakeElement);
result = aurelia.enhance({}, elId);
expect(aurelia.host).toBe(fakeElement);
});
});

describe('when passing in bindingContext and an element', () => {
let result;

it('configures body as host', () => {
let elId = 'Testing';
let fakeElement = DOM.createElement('div');
fakeElement.setAttribute('id', fakeElement);
result = aurelia.enhance({}, fakeElement);
expect(aurelia.host).toBe(fakeElement);
});
});
});



});

0 comments on commit e1cbfcc

Please sign in to comment.