diff options
author | Florian Dold <florian@dold.me> | 2021-02-08 19:59:19 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-02-08 19:59:19 +0100 |
commit | 8c92499d85917693d2f87252419f0eeccd239a2b (patch) | |
tree | 569d0ee1c25d62caf0ac87131ecfa8166d12c62b /packages/idb-bridge/src/idb-wpt-ported/keypath.test.ts | |
parent | 5ff5a686e4f15dea839b18fda9275687557d23a7 (diff) | |
download | wallet-core-8c92499d85917693d2f87252419f0eeccd239a2b.tar.xz |
idb: add first web platform tests, fix issues detected by them
Diffstat (limited to 'packages/idb-bridge/src/idb-wpt-ported/keypath.test.ts')
-rw-r--r-- | packages/idb-bridge/src/idb-wpt-ported/keypath.test.ts | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/packages/idb-bridge/src/idb-wpt-ported/keypath.test.ts b/packages/idb-bridge/src/idb-wpt-ported/keypath.test.ts new file mode 100644 index 000000000..61e416a53 --- /dev/null +++ b/packages/idb-bridge/src/idb-wpt-ported/keypath.test.ts @@ -0,0 +1,191 @@ +import test from "ava"; +import { assert_key_equals, createdb } from "./wptsupport"; + +test("WPT test keypath.htm", async (t) => { + function keypath( + keypath: any, + objects: any[], + expected_keys: any[], + desc?: string, + ) { + return new Promise<void>((resolve, reject) => { + console.log("key path", keypath); + console.log("checking", desc); + let db: any; + const store_name = "store-" + Date.now() + Math.random(); + + var open_rq = createdb(t); + open_rq.onupgradeneeded = function (e) { + db = (e.target as any).result; + var objStore = db.createObjectStore(store_name, { keyPath: keypath }); + + for (var i = 0; i < objects.length; i++) objStore.add(objects[i]); + }; + + open_rq.onsuccess = function (e) { + var actual_keys: any[] = [], + rq = db.transaction(store_name).objectStore(store_name).openCursor(); + + rq.onsuccess = (e: any) => { + var cursor = e.target.result; + + if (cursor) { + actual_keys.push(cursor.key.valueOf()); + cursor.continue(); + } else { + assert_key_equals(actual_keys, expected_keys, "keyorder array"); + resolve(); + } + }; + }; + }); + } + + await keypath("my.key", [{ my: { key: 10 } }], [10]); + + await keypath("my.køi", [{ my: { køi: 5 } }], [5]); + + await keypath("my.key_ya", [{ my: { key_ya: 10 } }], [10]); + + await keypath("public.key$ya", [{ public: { key$ya: 10 } }], [10]); + + await keypath("true.$", [{ true: { $: 10 } }], [10]); + + await keypath("my._", [{ my: { _: 10 } }], [10]); + + await keypath("delete.a7", [{ delete: { a7: 10 } }], [10]); + + await keypath( + "p.p.p.p.p.p.p.p.p.p.p.p.p.p", + [ + { + p: { + p: { + p: { + p: { + p: { + p: { p: { p: { p: { p: { p: { p: { p: { p: 10 } } } } } } } }, + }, + }, + }, + }, + }, + }, + ], + [10], + ); + + await keypath( + "str.length", + [{ str: "pony" }, { str: "my" }, { str: "little" }, { str: "" }], + [0, 2, 4, 6], + ); + + await keypath( + "arr.length", + [ + { arr: [0, 0, 0, 0] }, + { arr: [{}, 0, "hei", "length", Infinity, []] }, + { arr: [10, 10] }, + { arr: [] }, + ], + [0, 2, 4, 6], + ); + + await keypath("length", [[10, 10], "123", { length: 20 }], [2, 3, 20]); + + await keypath( + "", + [["bags"], "bean", 10], + [10, "bean", ["bags"]], + "'' uses value as key", + ); + + await keypath( + [""], + [["bags"], "bean", 10], + [[10], ["bean"], [["bags"]]], + "[''] uses value as [key]", + ); + + await keypath( + ["x", "y"], + [ + { x: 10, y: 20 }, + { y: 1.337, x: 100 }, + ], + [ + [10, 20], + [100, 1.337], + ], + "['x', 'y']", + ); + + await keypath( + [["x"], ["y"]], + [ + { x: 10, y: 20 }, + { y: 1.337, x: 100 }, + ], + [ + [10, 20], + [100, 1.337], + ], + "[['x'], 'y'] (stringifies)", + ); + + await keypath( + [ + "x", + { + toString: function () { + return "y"; + }, + }, + ], + [ + { x: 10, y: 20 }, + { y: 1.337, x: 100 }, + ], + [ + [10, 20], + [100, 1.337], + ], + "['x', {toString->'y'}] (stringifies)", + ); + + await keypath( + ["name", "type"], + [ + { name: "orange", type: "fruit" }, + { name: "orange", type: ["telecom", "french"] }, + ], + [ + ["orange", "fruit"], + ["orange", ["telecom", "french"]], + ], + ); + + await keypath( + ["name", "type.name"], + [ + { name: "orange", type: { name: "fruit" } }, + { name: "orange", type: { name: "telecom" } }, + ], + [ + ["orange", "fruit"], + ["orange", "telecom"], + ], + ); + + const loop_array: any[] = []; + loop_array.push(loop_array); + await keypath( + loop_array, + ["a", 1, ["k"]], + [[1], ["a"], [["k"]]], + "array loop -> stringify becomes ['']", + ); + + t.pass(); +}); |