Skip to content

Commit

Permalink
update IndexedDB test
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Templeton committed May 13, 2018
1 parent c618397 commit e1282cd
Showing 1 changed file with 49 additions and 37 deletions.
86 changes: 49 additions & 37 deletions IndexedDB/bigint_value.htm
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,67 @@
// This support allows them to be used as IndexedDB values.

let i = 0;
function value(value, test) {
var t = async_test(document.title + " - " + (i++));
t.step(function() {
assert_true(test(value),
"condition does not apply to initial value");
});

createdb(t).onupgradeneeded = function(e) {
e.target.result
.createObjectStore("store")
.add(value, 1);
function value(value, test, name) {
async_test(t => {
t.step(function() {
assert_true(test(value),
"condition does not apply to initial value");
});

e.target.onsuccess = t.step_func(function(e) {
createdb(t).onupgradeneeded = t.step_func(e => {
e.target.result
.transaction("store")
.objectStore("store")
.get(1)
.onsuccess = t.step_func(function(e)
{
assert_true(test(e.target.result),
"condition does not apply to deserialized result")
t.done();
.createObjectStore("store")
.add(value, 1);

e.target.onsuccess = t.step_func(function(e) {
e.target.result
.transaction("store")
.objectStore("store")
.get(1)
.onsuccess = t.step_func(function(e)
{
assert_true(test(e.target.result),
"condition does not apply to deserialized result")
t.done();
});
});
});
};
}, "BigInts as values in IndexedDB - " + name);
}

value(1n, x => x === 1n);
value(Object(1n), x => typeof x === 'object' && x instanceof BigInt
&& x.valueOf() === 1n);
value(1n,
x => x === 1n,
"primitive BigInt");
value(Object(1n),
x => typeof x === 'object' &&
x instanceof BigInt &&
x.valueOf() === 1n,
"BigInt object");
value({val: 1n},
x => x.val === 1n,
"primitive BigInt inside object");
value({val: Object(1n)},
x => x.val === 1n &&
x.val instanceof BigInt &&
x.val.valueOf() === 1n,
"BigInt object inside object");

// However, BigInt is not supported as an IndexedDB key; support
// has been proposed in the following PR, but that change has not
// landed at the time this patch was written
// https://github.com/w3c/IndexedDB/pull/231

function invalidKey(key) {
var t = async_test(document.title + " - " + (i++));

createdb(t).onupgradeneeded = function(e) {
let store = e.target.result.createObjectStore("store")
assert_throws('DataError', () => store.add(1, key));
t.done();
};
function invalidKey(key, name) {
async_test(t => {
createdb(t).onupgradeneeded = t.step_func(e => {
let store = e.target.result.createObjectStore("store")
assert_throws('DataError', () => store.add(1, key));
t.done();
});
}, "BigInts as keys in IndexedDB - " + name);
}

invalidKey(1n);
invalidKey(Object(1n)); // Still an error even if the IndexedDB patch lands

invalidKey(1n, "primitive BigInt");
// Still an error even if the IndexedDB patch lands
invalidKey(Object(1n), "BigInt object");
</script>

<div id="log"></div>

0 comments on commit e1282cd

Please sign in to comment.