Created
July 19, 2012 20:27
-
-
Save mxriverlynn/3146544 to your computer and use it in GitHub Desktop.
making Jasmine's async specs beautiful, with promises and an AsyncSpec helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('live tile', function () { | |
describe('when updating the tile', function () { | |
var async = new AsyncSpec(); | |
async.beforeEach(function (complete) { | |
Shared.getImages() | |
.then(Tiles.buildThumbails) | |
.then(async.storage.store('fileNames')) | |
.then(complete); | |
}); | |
async.it('should write the thumbnails to the tile thumbnails folder', function (storage) { | |
storage['fileNames'].forEach(function (file) { | |
var thumbnailExists = Shared.thumbnailFileExists(file); | |
async.await(thumbnailExists).then(function (fileExists) { | |
expect(fileExists).toBe(true); | |
}); | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Built on WinJS/WinRT/Win8 but easily adapted to any JS runtime | |
// -------------------------------------------------------------- | |
(function (global) { | |
var Storage = WinJS.Class.define(null, { | |
store: function (fieldName, cb) { | |
var that = this; | |
return function (data) { | |
if (cb) { data = cb(data); } | |
that[fieldName] = data; | |
return new WinJS.Promise(function (complete) { | |
complete(data); | |
}); | |
} | |
} | |
}); | |
var AsyncSpec = WinJS.Class.define(function () { | |
this.storage = new Storage(); | |
}, { | |
it: function (description, block) { | |
var that = this; | |
var storage = this.storage; | |
global.it(description, function () { | |
waitsFor(function () { | |
return that.done; | |
}); | |
runs(function () { | |
block(storage); | |
}); | |
}); | |
}, | |
beforeEach: function (block) { | |
this.done = false; | |
var that = this; | |
global.beforeEach(function () { | |
block(that.complete.bind(that)); | |
}); | |
}, | |
complete: function () { | |
this.done = true; | |
}, | |
await: function (waitingPromise) { | |
var done = false; | |
var count = 0; | |
var promise = new WinJS.Promise(function (complete) { | |
global.runs(function () { | |
waitingPromise.then(function () { | |
complete.apply(null, arguments); | |
done = true; | |
}); | |
}); | |
global.waitsFor(function () { | |
return done; | |
}); | |
}); | |
return promise; | |
} | |
}); | |
global.AsyncSpec = AsyncSpec; | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment