Permalink
Browse files

Fixes #362 - Regression from not having default fetchTemplate tests. …

…Added DOM and Node tests to cover this.
  • Loading branch information...
1 parent ecabab5 commit dc82c05e09648a429efc874278c5464a1d811c5a @tbranyen committed Aug 2, 2013
Showing with 57 additions and 13 deletions.
  1. +1 −1 Gruntfile.coffee
  2. +5 −8 example/03.Using_in_Node/index.js
  3. +4 −3 node/index.js
  4. +13 −0 test/dom.js
  5. +5 −1 test/index.html
  6. +28 −0 test/node.js
  7. +1 −0 test/templates/test.html
View
2 Gruntfile.coffee
@@ -72,7 +72,7 @@ module.exports = ->
coberturaReport: "test/report"
linesThresholdPct: 85
- files: ["test/index.html"]
+ files: ["test/index.html", "!test/node.js"]
# Run QUnit tests for Node.js environments.
nodequnit:
View
13 example/03.Using_in_Node/index.js
@@ -1,28 +1,25 @@
-// If you install via NPM per the best practices, you would do:
+// If you installed LayoutManager globally, you would do:
// var LayoutManager = require("backbone.layoutmanager");
// Since we want this to run without having to install LayoutManager...
var LayoutManager = require("../../node");
-// Setting this option augments `Backbone.View` to work like `Layout`.
-LayoutManager.configure({ manage: true });
-
// Create a header View and notice that simply providing a path to a template
// will automatically load it.
-var Header = Backbone.View.extend({
- template: "header.html"
+var Header = Backbone.Layout.extend({
+ template: "header"
});
var layout = new Backbone.Layout({
- template: "test.html",
+ template: "test",
views: {
"header": new Header({ el: false })
}
});
// Render the layout and echo out the contents.
-layout.render().then(function() {
+layout.render().promise().then(function() {
// Should echo out: <header><h1>This is my header!</h1></header>
console.log(this.$el.html());
});
View
7 node/index.js
@@ -1,4 +1,5 @@
var fs = require("fs");
+var path = require("path");
// Common dependencies to get LayoutManager running.
var Backbone = require("backbone");
@@ -37,16 +38,16 @@ require("../backbone.layoutmanager");
Backbone.Layout.configure({
// Sensible default for Node.js is to load templates from the filesystem.
// This is similar to how we default to script tags in browser-land.
- fetch: function(path) {
+ fetchTemplate: function(template) {
// Automatically add the `.html` extension.
- path = path + ".html";
+ template = template + ".html";
// Put this fetch into `async` mode to work better in the Node environment.
var done = this.async();
// By default read in the file from the filesystem relative to the code
// being executed.
- fs.readFile(path, function(err, contents) {
+ fs.readFile(path.join(__dirname, template), function(err, contents) {
// Ensure the contents are a String.
contents = String(contents);
View
13 test/dom.js
@@ -3,6 +3,8 @@
QUnit.module("dom", {
setup: function() {
+ this.LM = Backbone.Layout.extend();
+
this.SubView = Backbone.Layout.extend({
template: _.template(testUtil.templates.testSub),
fetchTemplate: _.identity,
@@ -14,6 +16,17 @@ QUnit.module("dom", {
}
});
+asyncTest("test default fetchTemplate implementation", 1, function() {
+ // Create a new layout with a sample template.
+ var layout = new this.LM({ template: "#test" });
+
+ // Render and check.
+ layout.render().promise().then(function() {
+ equal(this.$el.html().trim(), "Sample template.", "Correct render output.");
+ start();
+ });
+});
+
asyncTest("use layout without a template property", function() {
expect(1);
View
6 test/index.html
@@ -20,7 +20,11 @@
<script src="dom.js"></script>
<script type="text/template" id="dom-template">
- This template lives in the <b>DOM</b>
+ This template lives in the <b>DOM</b>
+ </script>
+
+ <script type="text/template" id="test">
+ Sample template.
</script>
</head>
<body>
View
28 test/node.js
@@ -0,0 +1,28 @@
+(function() {
+"use strict";
+
+/*
+ * Test Module: Configure
+ * Ensures that configuration settings have correct defaults, initialization,
+ * overriding, and functionality.
+ *
+ */
+QUnit.module("configure", {
+ setup: function() {
+ this.LM = require("../");
+ }
+});
+
+// Ensure the correct defaults are set for all Layout and View options.
+asyncTest("can render a basic template", 1, function() {
+ // Create a new layout with a sample template.
+ var layout = new this.LM({ template: "../test/templates/test" });
+
+ // Render and check.
+ layout.render().promise().then(function() {
+ equal(this.$el.html().trim(), "Sample template.", "Correct render output.");
+ start();
+ });
+});
+
+})();
View
1 test/templates/test.html
@@ -0,0 +1 @@
+Sample template.

0 comments on commit dc82c05

Please sign in to comment.