Skip to content

Instantly share code, notes, and snippets.

@kpuputti
Created June 30, 2011 11:56
Show Gist options
  • Save kpuputti/1056091 to your computer and use it in GitHub Desktop.
Save kpuputti/1056091 to your computer and use it in GitHub Desktop.
Asynchronous QUnit test with a mocked proxy for a caching json library
asyncTest('Basic fetch with an empty localStorage.', function () {
expect(7);
start();
var proxyMockCallCount = 0;
// Mock the json proxy to avoid networking.
JSONCache._getJSONProxy = function (url, options) {
proxyMockCallCount++;
if (url === 'data.json') {
options.success(testData);
}
};
var timeMockCallCount = 0;
// Mock the timestamp function for a static timestamp.
JSONCache._getTime = function () {
timeMockCallCount++;
return '1234567890123';
};
// Initial conditions.
eq(window.localStorage.length, 0, 'localStorage should be empty.');
JSONCache.getCachedJSON('data.json', {
success: function (data) {
deepEqual(data, {
success: true,
data: [
'först item',
'secönd itém'
],
'Weird väl': 666
}, 'Correct test data should be returned.');
deepEqual(data, JSON.parse(window.localStorage['JSONCache data data.json']),
'localStorage should be populated with the correct data.');
eq(window.localStorage['JSONCache time data.json'], '1234567890123',
'Timestamp should be the one returned by the static mock function.');
eq(proxyMockCallCount, 1, 'Mocked json proxy should be called once.');
eq(timeMockCallCount, 1, 'Mocked timestamp function should be called once.');
eq(window.localStorage.length, 2, 'There should be two items in the localStorage.');
stop();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment