Created
August 31, 2012 23:40
-
-
Save bunnymatic/3561172 to your computer and use it in GitHub Desktop.
Jasmine Load JSON fixtures
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
/** | |
add ability to load json fixtures into jasmine | |
**/ | |
var readJsonFixtures = function() { | |
return jasmine.getJsonFixtures().proxyCallTo_('read', arguments); | |
}; | |
var preloadJsonFixtures = function() { | |
jasmine.getJsonFixtures().proxyCallTo_('preload', arguments); | |
}; | |
var loadJsonFixtures = function() { | |
return jasmine.getJsonFixtures().proxyCallTo_('load', arguments); | |
}; | |
var setJsonFixtures = function(data) { | |
return jasmine.getJsonFixtures().set(data); | |
}; | |
var getJsonFixture = function(url) { | |
return jasmine.getJsonFixtures().proxyCallTo_('read', arguments)[url]; | |
}; | |
jasmine.getJsonFixtures = function() { | |
return jasmine.currentJsonFixtures_ = jasmine.currentJsonFixtures_ || new jasmine.JSONFixtures(); | |
}; | |
jasmine.JSONFixtures = function() { | |
this.fixturesCache_ = {}; | |
this.fixturesPath = 'spec/javascripts/fixtures/json'; | |
}; | |
jasmine.JSONFixtures.prototype.set = function(data) { | |
this.clearCache(); | |
this.fixturesCache_ = data | |
}; | |
jasmine.JSONFixtures.prototype.preload = function() { | |
this.read.apply(this, arguments); | |
}; | |
jasmine.JSONFixtures.prototype.load = function() { | |
this.read.apply(this, arguments); | |
return this.fixturesCache_ | |
}; | |
jasmine.JSONFixtures.prototype.read = function() { | |
var fixtureUrls = arguments; | |
for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { | |
this.getFixtureData_(fixtureUrls[urlIndex]); | |
} | |
return this.fixturesCache_ | |
}; | |
jasmine.JSONFixtures.prototype.clearCache = function() { | |
this.fixturesCache_ = {}; | |
}; | |
jasmine.JSONFixtures.prototype.getFixtureData_ = function(url) { | |
if (typeof this.fixturesCache_[url] == 'undefined') { | |
this.loadFixtureIntoCache_(url); | |
} | |
return this.fixturesCache_[url]; | |
}; | |
jasmine.JSONFixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) { | |
var self = this; | |
var url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl; | |
jQuery.ajax({ | |
async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded | |
cache: false, | |
dataType: 'json', | |
url: url, | |
success: function(data) { | |
self.fixturesCache_[relativeUrl] = data; | |
}, | |
error: function(jqXHR, status, errorThrown) { | |
throw Error('JSONFixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')'); | |
} | |
}); | |
}; | |
jasmine.JSONFixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) { | |
return this[methodName].apply(this, passedArguments); | |
}; |
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 'jasmine.JSONFixtures', -> | |
describe 'loadJsonFixtures', -> | |
it 'load a single a single fixture', -> | |
@fixture_data = loadJsonFixtures('jasmine_json_test.json') | |
expect(_.keys(@fixture_data)).toEqual(['jasmine_json_test.json']) | |
expect(@fixture_data['jasmine_json_test.json']).toEqual {"a":1,"b":2,"prop1":"val1", "arr1":[1,2,"stuff",{"a":10,"b":20}]} | |
it 'loads 2 fixtures with one call', -> | |
@fixture_data = loadJsonFixtures('jasmine_json_test.json', 'jasmine_json_test2.json') | |
expect(_.keys(@fixture_data)).toInclude('jasmine_json_test.json') | |
expect(_.keys(@fixture_data)).toInclude('jasmine_json_test2.json') | |
it 'loads 2 fixtures with two calls', -> | |
@fixture_data = loadJsonFixtures 'jasmine_json_test.json' | |
@fixture_data = loadJsonFixtures 'jasmine_json_test2.json' | |
expect(_.keys(@fixture_data)).toInclude('jasmine_json_test.json') | |
expect(_.keys(@fixture_data)).toInclude('jasmine_json_test2.json') | |
describe 'getJsonFixture', -> | |
beforeEach -> | |
setJsonFixtures({fixture1: [1,2,3,4,5], fixture2: ['a','b']}) | |
it 'retrieves the fixture data based on the key', -> | |
expect(getJsonFixture('fixture1')).toEqual [1,2,3,4,5] | |
expect(getJsonFixture('fixture2')).toEqual ['a','b'] | |
describe 'setJsonFixtures', -> | |
it 'sets the incoming fixture data to the fixtures', -> | |
setJsonFixtures({fixture1: [1,2,3,4,5], fixture2: ['a','b']}) | |
expect(_.keys(jasmine.getJsonFixtures().fixturesCache_)).toInclude('fixture1') | |
expect(_.keys(jasmine.getJsonFixtures().fixturesCache_)).toInclude('fixture2') | |
it 'clears other fixture data', -> | |
setJsonFixtures({blurp: [1,2,3,4,5], blop: ['a','b']}) | |
expect(_.keys(jasmine.getJsonFixtures().fixturesCache_).length).toEqual(2) | |
setJsonFixtures({fixture1: [1,2,3,4,5], blip: 666, fixture2: ['a','b']}) | |
expect(_.keys(jasmine.getJsonFixtures().fixturesCache_).length).toEqual(3) | |
_.each ['fixture1','fixture2','blip'], (x) -> | |
expect(_.keys(jasmine.getJsonFixtures().fixturesCache_)).toInclude(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nmccready change the line:
https://gist.github.com/bunnymatic/3561172#file-jasmine_json_fixtures-js-L31
into
this.fixturesPath = 'assets/fixtures';
it's assumed that the fixtures are placed into
spec/javascripts/fixtures
with the default config forjasmine-rails
gem.