Skip to content

Commit

Permalink
feat: allow 'root' to be specified when creating a component constructor
Browse files Browse the repository at this point in the history
- if specified, component will be mounted after init
- add tests
  • Loading branch information
kbrsh committed Aug 30, 2017
1 parent 7966be9 commit 878b4ba
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,7 @@
if(componentOptions === undefined) {
this.insert = [];
} else {
var root = componentOptions.root;
var props = componentOptions.props;
this.insert = componentOptions.insert;

Expand All @@ -2052,6 +2053,10 @@
this$1.data[prop] = props[prop];
}
}

if(root !== undefined) {
this.mount(root);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/global/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Moon.component = function(name, options) {
if(componentOptions === undefined) {
this.insert = [];
} else {
const root = componentOptions.root;
const props = componentOptions.props;
this.insert = componentOptions.insert;

Expand All @@ -93,6 +94,10 @@ Moon.component = function(name, options) {
this.data[prop] = props[prop];
}
}

if(root !== undefined) {
this.mount(root);
}
}
}

Expand Down
24 changes: 23 additions & 1 deletion test/core/component/component.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
describe("Component", function() {
var componentConstructor = Moon.component("const", {template: "<div>Hello Moon!</div>"});

it("should create a constructor", function() {
var componentConstructor = Moon.component("const", {template: "<div></div>"});
expect(new componentConstructor()).to.be.an.instanceof(Moon);
});

it("should create a constructor that can mount to an element", function() {
var root = createTestElement("componentConstructorMount", "");
new componentConstructor({
root: root
});

return wait(function() {
expect(root.firstChild.textContent).to.equal("Hello Moon!");
});
});

it("should create a constructor that can mount to an element manually", function() {
var root = createTestElement("componentConstructorMountManual", "");
var instance = new componentConstructor();
instance.mount(root);

return wait(function() {
expect(root.firstChild.textContent).to.equal("Hello Moon!");
});
});

it("should render HTML", function() {
var component = createTestElement("component", "<component></component>");

Expand Down

0 comments on commit 878b4ba

Please sign in to comment.