Skip to content

Instantly share code, notes, and snippets.

View bjartwolf's full-sized avatar

Bjørn Einar Bjartnes bjartwolf

View GitHub Profile
@bjartwolf
bjartwolf / buster-101-test.js
Created August 19, 2012 15:21
buster 101 test
require("buster");
var my = require("../lib/my.js");
buster.testCase("A module", {
"states the obvious": function () {
assert.equals(my.double(5), "10", "Should double the number");
},
"we need a funny failing test also": function () {
assert.same(my.double(5), "10", "Should double the number");
}
});
@bjartwolf
bjartwolf / buster-101-src.js
Created August 19, 2012 15:24
buster 101 src
exports.double = function (tall) {
return tall * doubler;
};
@bjartwolf
bjartwolf / buser-201-test.js
Created August 19, 2012 16:52
buster 201 test
buster.testCase("The ViewModel", {
"should update the fullname when firstname changes": function () {
var vm = new AppViewModel();
vm.firstName("Arne");
assert.equals(vm.fullName(), "Arne Bertington");
},
"should capitalize the last name when capitalizeLastName is called": function () {
var vm = new AppViewModel();
vm.firstName("Arne");
vm.capitalizeLastName();
@bjartwolf
bjartwolf / buster-201-src.js
Created August 19, 2012 16:53
buster 201 src
function AppViewModel() {
var self = this;
self.firstName = ko.observable("Bert");
self.lastName = ko.observable("Bertington");
self.fullName = ko.computed(function () {
return self.firstName() + " " + self.lastName();
}, self);
self.capitalizeLastName = function () {
@bjartwolf
bjartwolf / buster-301-tests.js
Created August 19, 2012 21:43
buster 301 tests
buster.testCase("The ViewModel", {
"setUp": function () {
this.vm = new ReservationsViewModel();
},
"total sum should be zero when initialized": function () {
assert.same(this.vm.totalSurcharge(), 0);
},
"total number of seats should be two": function () {
assert.same(this.vm.seats().length, 2);
},
@bjartwolf
bjartwolf / buster-301-src.js
Created August 19, 2012 21:44
buster 301 src
function SeatReservation(name, initialMeal) {
var self = this;
self.name = name;
self.meal = ko.observable(initialMeal);
self.formattedPrice = ko.computed(function () {
var price = self.meal().price;
return price ? "$" + price.toFixed(2) : "None";
});
}
@bjartwolf
bjartwolf / refactor underscore.js
Created August 20, 2012 08:39
refactor underscore
// Overall viewmodel for this screen, along with initial state
function ReservationsViewModel() {
var self = this;
// Non-editable catalog data - would come from the server
self.availableMeals = [
{ mealName: "Standard (sandwich)", price: 0 },
{ mealName: "Premium (lobster)", price: 34.95 },
{ mealName: "Ultimate (whole zebra)", price: 290 }
];
@bjartwolf
bjartwolf / load and save.js
Created August 21, 2012 08:02
loading and saving data
"use strict";
buster.testCase("The ViewModel", {
"setUp": function () {
// Spying on existing method
this.spy(MYAPP.services, 'load');
this.vm = new TaskListViewModel();
},
"load should be called on initialization": function () {
assert.calledOnce(MYAPP.services.load);
},
@bjartwolf
bjartwolf / 601viewmodel.js
Created August 21, 2012 14:35
601 viewmodel
"use strict";
function Task(data) {
this.title = ko.observable(data.title);
this.isDone = ko.observable(data.isDone);
}
function TaskListViewModel() {
// Data
var self = this;
@bjartwolf
bjartwolf / 601-test.js
Created August 21, 2012 14:36
601 tests
"use strict";
buster.testCase("The ViewModel", {
"setUp": function () {
// Spying on existing method
this.spy(MYAPP.services, 'load');
this.vm = new TaskListViewModel();
},
"load should be called on initialization": function () {
assert.calledOnce(MYAPP.services.load);
},