-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #942 from dicksonlaw583/local_fake_jsonblob
Added fake local JSONBlob endpoint.
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* Local fake JsonBlob endpoint */ | ||
|
||
/* global Storage, Mirador */ | ||
|
||
(function($){ | ||
function generateUUID() { | ||
var d = new Date().getTime(); | ||
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | ||
var r = (d + Math.random()*16)%16 | 0; | ||
d = Math.floor(d/16); | ||
return (c==='x' ? r : (r&0x3|0x8)).toString(16); | ||
}); | ||
return uuid; | ||
} | ||
|
||
$.LocalJSONBlobAPI = function(opts) { | ||
this.options = { | ||
storage: window.localStorage | ||
}; | ||
jQuery.extend(this.options, opts); | ||
}; | ||
|
||
$.LocalJSONBlobAPI.prototype = { | ||
readSync: function(blobId) { | ||
return JSON.parse(this.options.storage.getItem(blobId)); | ||
}, | ||
save: function(blob) { | ||
var deferred = jQuery.Deferred(), | ||
uuid = generateUUID(); | ||
this.options.storage.setItem("BM-" + uuid, JSON.stringify(blob)); | ||
deferred.resolve("BM-" + uuid); | ||
return deferred.promise(); | ||
} | ||
}; | ||
}(Mirador)); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
describe('LocalJsonBlobApi', function () { | ||
BogusStorage = function() { | ||
this.data = {}; | ||
}; | ||
BogusStorage.prototype = { | ||
setItem: function(k, v) { | ||
this.data[k] = v; | ||
}, | ||
getItem: function(k) { | ||
return this.data[k]; | ||
} | ||
}; | ||
|
||
|
||
var myWindow = { | ||
"bogusStorage": new BogusStorage() | ||
}; | ||
|
||
var dummyBlobs = { | ||
a: { | ||
"waahoo": 5 | ||
}, | ||
b: { | ||
"woohah": "super foobar" | ||
} | ||
}; | ||
|
||
var localBlob; | ||
beforeEach(function() { | ||
localBlob = new Mirador.LocalJSONBlobAPI({ | ||
storage: myWindow.bogusStorage | ||
}); | ||
}); | ||
|
||
it('readSync', function () { | ||
myWindow.bogusStorage.setItem('ABC', JSON.stringify(dummyBlobs.a)); | ||
expect(localBlob.readSync('ABC')).toEqual(dummyBlobs.a); | ||
}); | ||
|
||
it('save', function () { | ||
var promise = null, | ||
savedKey = ""; | ||
(promise = localBlob.save(dummyBlobs.b)).done(function(k) { | ||
savedKey = k; | ||
}); | ||
expect(typeof promise).toBe('object'); | ||
expect(savedKey).not.toEqual(""); | ||
expect(myWindow.bogusStorage.getItem(savedKey)).toEqual(JSON.stringify(dummyBlobs.b)); | ||
}); | ||
}); |