Skip to content

Instantly share code, notes, and snippets.

@tofumatt
Created March 17, 2014 16:32
Show Gist options
  • Save tofumatt/9602838 to your computer and use it in GitHub Desktop.
Save tofumatt/9602838 to your computer and use it in GitHub Desktop.
Old backbone tests
'use strict'
casper.test.begin "Testing Backbone data adapter", (test) ->
casper.start "#{casper.TEST_URL}test.backbone.html", ->
test.info "Testing using global scope (no require.js)"
test.assertEval ->
typeof Backbone.localforage is 'object'
, "localforage storage adapter is attached to Backbone.localforage"
test.assertEval ->
typeof Backbone.localforage.sync is 'function'
, "localforage sync function is attached to Backbone.localforage"
casper.then ->
@evaluate ->
michael = new Model
name: 'Michael Bolton'
job: 'Singer'
Models.add michael
michael.save()
casper.reload()
casper.then ->
@waitForSelector '#ready', ->
test.assertEval ->
results = Models.where({name: 'Michael Bolton'})
results[0].get('job') is "Singer"
, "Backbone adapter should persist data after a reload"
casper.then ->
@waitForSelector '#ready', ->
@evaluate ->
results = Models.where({name: 'Michael Bolton'})
results[0].destroy()
Models.reset()
casper.wait 300
casper.then ->
test.assertEval ->
results = Models.where({name: 'Michael Bolton'})
results.length is 0
, "Backbone adapter should delete data after model is removed"
casper.thenOpen "#{casper.TEST_URL}backbone-example.html", ->
test.info "Test the Backbone example (examples/backbone-example.html)"
@waitForSelector '.content', ->
# Fill the content form and test it saves the content without error.
casper.fill '.content form', {content: 'testing'}, true
casper.reload()
casper.then ->
@waitForSelector '.content .saved-data', ->
test.assertEval ->
$('.saved-data').length is 1
, "Backbone example saves a piece of data between page loads"
test.assertEval ->
$('.saved-data').text() is 'testing'
, "Data saved in Backbone is retrieved properly"
casper.then ->
# See: https://github.com/mozilla/localForage/pull/90
casper.fill '.content form', {content: 'test 2'}, true
casper.fill '.content form', {content: 'test 3'}, true
casper.then ->
test.assertEval ->
$('.saved-data').length is 3
, "Extra data is saved properly"
test.assertEval ->
$('.saved-data').eq(2).text() is 'test 3'
, "Data saved in Backbone is retrieved properly"
casper.then ->
@evaluate ->
localforage.clear()
casper.wait 200
casper.reload()
casper.then ->
@waitForSelector '.content', ->
test.assertEval ->
$('.saved-data').length is 0
, "After running clear() on localforage, no saved-data divs exist"
casper.run ->
test.done()
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Backbone Adapter Test</title>
</head>
<body>
<div class="status"></div>
<script src="/underscore.js"></script>
<script src="/backbone.js"></script>
<script src="/localforage.js"></script>
<script src="/backbone.localforage.js"></script>
<script>
localforage.setDriver('localStorageWrapper');
var Model = Backbone.Model.extend({
sync: Backbone.localforage.sync()
});
var ModelCollection = Backbone.Collection.extend({
model: Model,
sync: Backbone.localforage.sync('Models')
});
var Models = new ModelCollection();
Models.fetch({
success: function() {
document.querySelectorAll('.status')[0].setAttribute('id', 'ready');
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment