From 5ff3b44550d4f5ab9a20d85dbf4387d455ebb862 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 10 Feb 2022 19:52:45 +0100 Subject: idb-bridge: update tests for ava 4.x --- .../idb-wpt-ported/idbcursor-update-index.test.ts | 594 +++++++++++---------- 1 file changed, 305 insertions(+), 289 deletions(-) (limited to 'packages/idb-bridge/src/idb-wpt-ported/idbcursor-update-index.test.ts') diff --git a/packages/idb-bridge/src/idb-wpt-ported/idbcursor-update-index.test.ts b/packages/idb-bridge/src/idb-wpt-ported/idbcursor-update-index.test.ts index dcbee2b16..81a7cd753 100644 --- a/packages/idb-bridge/src/idb-wpt-ported/idbcursor-update-index.test.ts +++ b/packages/idb-bridge/src/idb-wpt-ported/idbcursor-update-index.test.ts @@ -1,340 +1,356 @@ import test from "ava"; -import { BridgeIDBCursor, BridgeIDBKeyRange } from ".."; +import { BridgeIDBCursor, BridgeIDBKeyRange } from "../bridge-idb.js"; import { createDatabase, createdb, promiseForRequest, promiseForTransaction, -} from "./wptsupport"; +} from "./wptsupport.js"; // IDBCursor.update() - index - modify a record in the object store -test.cb("WPT test idbcursor_update_index.htm", (t) => { - var db: any, - records = [ - { pKey: "primaryKey_0", iKey: "indexKey_0" }, - { pKey: "primaryKey_1", iKey: "indexKey_1" }, - ]; +test("WPT test idbcursor_update_index.htm", (t) => { + return new Promise((resolve, reject) => { + var db: any, + records = [ + { pKey: "primaryKey_0", iKey: "indexKey_0" }, + { pKey: "primaryKey_1", iKey: "indexKey_1" }, + ]; - var open_rq = createdb(t); - open_rq.onupgradeneeded = function (e: any) { - db = e.target.result; + var open_rq = createdb(t); + open_rq.onupgradeneeded = function (e: any) { + db = e.target.result; - var objStore = db.createObjectStore("test", { keyPath: "pKey" }); - objStore.createIndex("index", "iKey"); + var objStore = db.createObjectStore("test", { keyPath: "pKey" }); + objStore.createIndex("index", "iKey"); - for (var i = 0; i < records.length; i++) objStore.add(records[i]); + for (var i = 0; i < records.length; i++) objStore.add(records[i]); - // XXX: Gecko doesn't like this - //e.target.transaction.oncomplete = t.step_func(CursorUpdateRecord); - }; + // XXX: Gecko doesn't like this + //e.target.transaction.oncomplete = t.step_func(CursorUpdateRecord); + }; - open_rq.onsuccess = CursorUpdateRecord; + open_rq.onsuccess = CursorUpdateRecord; - function CursorUpdateRecord(e: any) { - var txn = db.transaction("test", "readwrite"), - cursor_rq = txn.objectStore("test").index("index").openCursor(); - cursor_rq.onsuccess = function (e: any) { - var cursor = e.target.result; + function CursorUpdateRecord(e: any) { + var txn = db.transaction("test", "readwrite"), + cursor_rq = txn.objectStore("test").index("index").openCursor(); + cursor_rq.onsuccess = function (e: any) { + var cursor = e.target.result; - cursor.value.iKey += "_updated"; - cursor.update(cursor.value); - }; + cursor.value.iKey += "_updated"; + cursor.update(cursor.value); + }; - txn.oncomplete = VerifyRecordWasUpdated; - } + txn.oncomplete = VerifyRecordWasUpdated; + } - function VerifyRecordWasUpdated(e: any) { - var cursor_rq = db.transaction("test").objectStore("test").openCursor(); + function VerifyRecordWasUpdated(e: any) { + var cursor_rq = db.transaction("test").objectStore("test").openCursor(); - cursor_rq.onsuccess = function (e: any) { - var cursor = e.target.result; + cursor_rq.onsuccess = function (e: any) { + var cursor = e.target.result; - t.deepEqual(cursor.value.iKey, records[0].iKey + "_updated"); - t.end(); - }; - } + t.deepEqual(cursor.value.iKey, records[0].iKey + "_updated"); + resolve(); + }; + } + }); }); // IDBCursor.update() - index - attempt to modify a record in a read-only transaction -test.cb("WPT test idbcursor_update_index2.htm", (t) => { - var db: any, - records = [ - { pKey: "primaryKey_0", iKey: "indexKey_0" }, - { pKey: "primaryKey_1", iKey: "indexKey_1" }, - ]; - - var open_rq = createdb(t); - open_rq.onupgradeneeded = function (e: any) { - db = e.target.result; - - var objStore = db.createObjectStore("test", { keyPath: "pKey" }); - objStore.createIndex("index", "iKey"); - - for (var i = 0; i < records.length; i++) objStore.add(records[i]); - }; - - open_rq.onsuccess = function (e: any) { - var cursor_rq = db - .transaction("test") - .objectStore("test") - .index("index") - .openCursor(); - - cursor_rq.onsuccess = function (e: any) { - var cursor = e.target.result; - t.throws( - function () { - cursor.update(cursor.value); - }, - { name: "ReadOnlyError" }, - ); - t.end(); +test("WPT test idbcursor_update_index2.htm", (t) => { + return new Promise((resolve, reject) => { + var db: any, + records = [ + { pKey: "primaryKey_0", iKey: "indexKey_0" }, + { pKey: "primaryKey_1", iKey: "indexKey_1" }, + ]; + + var open_rq = createdb(t); + open_rq.onupgradeneeded = function (e: any) { + db = e.target.result; + + var objStore = db.createObjectStore("test", { keyPath: "pKey" }); + objStore.createIndex("index", "iKey"); + + for (var i = 0; i < records.length; i++) objStore.add(records[i]); }; - }; -}); -//IDBCursor.update() - index - attempt to modify a record in an inactive transaction -test.cb("WPT test idbcursor_update_index3.htm", (t) => { - var db: any, - records = [ - { pKey: "primaryKey_0", iKey: "indexKey_0" }, - { pKey: "primaryKey_1", iKey: "indexKey_1" }, - ]; - - var open_rq = createdb(t); - open_rq.onupgradeneeded = function (e: any) { - db = e.target.result; - var objStore = db.createObjectStore("test", { keyPath: "pKey" }); - var index = objStore.createIndex("index", "iKey"); - - for (var i = 0; i < records.length; i++) objStore.add(records[i]); - - var cursor_rq = index.openCursor(); - - const window: any = {}; - - cursor_rq.onsuccess = function (e: any) { - var cursor = e.target.result; - t.true(cursor instanceof BridgeIDBCursor, "cursor exist"); - window.cursor = cursor; - window.record = cursor.value; + open_rq.onsuccess = function (e: any) { + var cursor_rq = db + .transaction("test") + .objectStore("test") + .index("index") + .openCursor(); + + cursor_rq.onsuccess = function (e: any) { + var cursor = e.target.result; + t.throws( + function () { + cursor.update(cursor.value); + }, + { name: "ReadOnlyError" }, + ); + resolve(); + }; }; + }); +}); - e.target.transaction.oncomplete = function (e: any) { - t.throws( - function () { - window.cursor.update(window.record); - }, - { name: "TransactionInactiveError" }, - ); - t.end(); +//IDBCursor.update() - index - attempt to modify a record in an inactive transaction +test("WPT test idbcursor_update_index3.htm", (t) => { + return new Promise((resolve, reject) => { + var db: any, + records = [ + { pKey: "primaryKey_0", iKey: "indexKey_0" }, + { pKey: "primaryKey_1", iKey: "indexKey_1" }, + ]; + + var open_rq = createdb(t); + open_rq.onupgradeneeded = function (e: any) { + db = e.target.result; + var objStore = db.createObjectStore("test", { keyPath: "pKey" }); + var index = objStore.createIndex("index", "iKey"); + + for (var i = 0; i < records.length; i++) objStore.add(records[i]); + + var cursor_rq = index.openCursor(); + + const window: any = {}; + + cursor_rq.onsuccess = function (e: any) { + var cursor = e.target.result; + t.true(cursor instanceof BridgeIDBCursor, "cursor exist"); + window.cursor = cursor; + window.record = cursor.value; + }; + + e.target.transaction.oncomplete = function (e: any) { + t.throws( + function () { + window.cursor.update(window.record); + }, + { name: "TransactionInactiveError" }, + ); + resolve(); + }; }; - }; + }); }); // IDBCursor.update() - index - attempt to modify a record when object store been deleted -test.cb("WPT test idbcursor_update_index4.htm", (t) => { - var db: any, - records = [ - { pKey: "primaryKey_0", iKey: "indexKey_0" }, - { pKey: "primaryKey_1", iKey: "indexKey_1" }, - ]; - - var open_rq = createdb(t); - open_rq.onupgradeneeded = function (event: any) { - db = event.target.result; - var objStore = db.createObjectStore("store", { keyPath: "pKey" }); - objStore.createIndex("index", "iKey"); - for (var i = 0; i < records.length; i++) { - objStore.add(records[i]); - } - var rq = objStore.index("index").openCursor(); - rq.onsuccess = function (event: any) { - var cursor = event.target.result; - t.true(cursor instanceof BridgeIDBCursor); - - db.deleteObjectStore("store"); - cursor.value.iKey += "_updated"; - t.throws( - function () { - cursor.update(cursor.value); - }, - { name: "InvalidStateError" }, - "If the cursor's source or effective object store has been deleted, the implementation MUST throw a DOMException of type InvalidStateError", - ); - - t.end(); +test("WPT test idbcursor_update_index4.htm", (t) => { + return new Promise((resolve, reject) => { + var db: any, + records = [ + { pKey: "primaryKey_0", iKey: "indexKey_0" }, + { pKey: "primaryKey_1", iKey: "indexKey_1" }, + ]; + + var open_rq = createdb(t); + open_rq.onupgradeneeded = function (event: any) { + db = event.target.result; + var objStore = db.createObjectStore("store", { keyPath: "pKey" }); + objStore.createIndex("index", "iKey"); + for (var i = 0; i < records.length; i++) { + objStore.add(records[i]); + } + var rq = objStore.index("index").openCursor(); + rq.onsuccess = function (event: any) { + var cursor = event.target.result; + t.true(cursor instanceof BridgeIDBCursor); + + db.deleteObjectStore("store"); + cursor.value.iKey += "_updated"; + t.throws( + function () { + cursor.update(cursor.value); + }, + { name: "InvalidStateError" }, + "If the cursor's source or effective object store has been deleted, the implementation MUST throw a DOMException of type InvalidStateError", + ); + + resolve(); + }; }; - }; + }); }); // IDBCursor.update() - index - throw DataCloneError -test.cb("WPT test idbcursor_update_index5.htm", (t) => { - var db: any, - records = [ - { pKey: "primaryKey_0", iKey: "indexKey_0" }, - { pKey: "primaryKey_1", iKey: "indexKey_1" }, - ]; - - var open_rq = createdb(t); - open_rq.onupgradeneeded = function (e: any) { - db = e.target.result; - - var objStore = db.createObjectStore("test", { keyPath: "pKey" }); - objStore.createIndex("index", "iKey"); - - for (var i = 0; i < records.length; i++) objStore.add(records[i]); - }; - - open_rq.onsuccess = function (e: any) { - var cursor_rq = db - .transaction("test", "readwrite") - .objectStore("test") - .index("index") - .openCursor(); - - cursor_rq.onsuccess = function (e: any) { - var cursor = e.target.result; - t.true(cursor instanceof BridgeIDBCursor); - - var record = cursor.value; - // Original test uses different uncloneable value - record.data = { foo: () => {} }; - t.throws( - function () { - cursor.update(record); - }, - { name: "DataCloneError" }, - ); - t.end(); +test("WPT test idbcursor_update_index5.htm", (t) => { + return new Promise((resolve, reject) => { + var db: any, + records = [ + { pKey: "primaryKey_0", iKey: "indexKey_0" }, + { pKey: "primaryKey_1", iKey: "indexKey_1" }, + ]; + + var open_rq = createdb(t); + open_rq.onupgradeneeded = function (e: any) { + db = e.target.result; + + var objStore = db.createObjectStore("test", { keyPath: "pKey" }); + objStore.createIndex("index", "iKey"); + + for (var i = 0; i < records.length; i++) objStore.add(records[i]); + }; + + open_rq.onsuccess = function (e: any) { + var cursor_rq = db + .transaction("test", "readwrite") + .objectStore("test") + .index("index") + .openCursor(); + + cursor_rq.onsuccess = function (e: any) { + var cursor = e.target.result; + t.true(cursor instanceof BridgeIDBCursor); + + var record = cursor.value; + // Original test uses different uncloneable value + record.data = { foo: () => {} }; + t.throws( + function () { + cursor.update(record); + }, + { name: "DataCloneError" }, + ); + resolve(); + }; }; - }; + }); }); // IDBCursor.update() - index - no argument -test.cb("WPT test idbcursor_update_index6.htm", (t) => { - var db: any, - records = [ - { pKey: "primaryKey_0", iKey: "indexKey_0" }, - { pKey: "primaryKey_1", iKey: "indexKey_1" }, - ]; - - var open_rq = createdb(t); - open_rq.onupgradeneeded = function (e: any) { - db = e.target.result; - - var objStore = db.createObjectStore("test", { keyPath: "pKey" }); - objStore.createIndex("index", "iKey"); - - for (var i = 0; i < records.length; i++) objStore.add(records[i]); - }; - - open_rq.onsuccess = function (e: any) { - var cursor_rq = db - .transaction("test") - .objectStore("test") - .index("index") - .openCursor(); - - cursor_rq.onsuccess = function (e: any) { - var cursor = e.target.result; - t.true(cursor instanceof BridgeIDBCursor); - - t.throws( - function () { - cursor.update(); - }, - { - instanceOf: TypeError, - }, - ); - t.end(); +test("WPT test idbcursor_update_index6.htm", (t) => { + return new Promise((resolve, reject) => { + var db: any, + records = [ + { pKey: "primaryKey_0", iKey: "indexKey_0" }, + { pKey: "primaryKey_1", iKey: "indexKey_1" }, + ]; + + var open_rq = createdb(t); + open_rq.onupgradeneeded = function (e: any) { + db = e.target.result; + + var objStore = db.createObjectStore("test", { keyPath: "pKey" }); + objStore.createIndex("index", "iKey"); + + for (var i = 0; i < records.length; i++) objStore.add(records[i]); + }; + + open_rq.onsuccess = function (e: any) { + var cursor_rq = db + .transaction("test") + .objectStore("test") + .index("index") + .openCursor(); + + cursor_rq.onsuccess = function (e: any) { + var cursor = e.target.result; + t.true(cursor instanceof BridgeIDBCursor); + + t.throws( + function () { + cursor.update(); + }, + { + instanceOf: TypeError, + }, + ); + resolve(); + }; }; - }; + }); }); // IDBCursor.update() - index - throw DataError -test.cb("WPT test idbcursor_update_index7.htm", (t) => { - var db: any, - records = [ - { pKey: "primaryKey_0", iKey: "indexKey_0" }, - { pKey: "primaryKey_1", iKey: "indexKey_1" }, - ]; - - var open_rq = createdb(t); - open_rq.onupgradeneeded = function (e: any) { - db = e.target.result; - - var objStore = db.createObjectStore("test", { keyPath: "pKey" }); - objStore.createIndex("index", "iKey"); - - for (var i = 0; i < records.length; i++) objStore.add(records[i]); - }; - - open_rq.onsuccess = function (e: any) { - var cursor_rq = db - .transaction("test", "readwrite") - .objectStore("test") - .index("index") - .openCursor(); - - cursor_rq.onsuccess = function (e: any) { - var cursor = e.target.result; - t.true(cursor instanceof BridgeIDBCursor); - - t.throws( - function () { - cursor.update(null); - }, - { name: "DataError" }, - ); - t.end(); +test("WPT test idbcursor_update_index7.htm", (t) => { + return new Promise((resolve, reject) => { + var db: any, + records = [ + { pKey: "primaryKey_0", iKey: "indexKey_0" }, + { pKey: "primaryKey_1", iKey: "indexKey_1" }, + ]; + + var open_rq = createdb(t); + open_rq.onupgradeneeded = function (e: any) { + db = e.target.result; + + var objStore = db.createObjectStore("test", { keyPath: "pKey" }); + objStore.createIndex("index", "iKey"); + + for (var i = 0; i < records.length; i++) objStore.add(records[i]); + }; + + open_rq.onsuccess = function (e: any) { + var cursor_rq = db + .transaction("test", "readwrite") + .objectStore("test") + .index("index") + .openCursor(); + + cursor_rq.onsuccess = function (e: any) { + var cursor = e.target.result; + t.true(cursor instanceof BridgeIDBCursor); + + t.throws( + function () { + cursor.update(null); + }, + { name: "DataError" }, + ); + resolve(); + }; }; - }; + }); }); // IDBCursor.update() - index - throw InvalidStateError when the cursor is being iterated -test.cb("WPT test idbcursor_update_index8.htm", (t) => { - var db: any, - records = [ - { pKey: "primaryKey_0", iKey: "indexKey_0" }, - { pKey: "primaryKey_1", iKey: "indexKey_1" }, - ]; - - var open_rq = createdb(t); - open_rq.onupgradeneeded = function (e: any) { - db = e.target.result; - - var objStore = db.createObjectStore("store", { keyPath: "pKey" }); - objStore.createIndex("index", "iKey"); - - for (var i = 0; i < records.length; i++) objStore.add(records[i]); - }; - - open_rq.onsuccess = function (e: any) { - var cursor_rq = db - .transaction("store", "readwrite") - .objectStore("store") - .index("index") - .openCursor(); - - cursor_rq.onsuccess = function (e: any) { - var cursor = e.target.result; - t.true(cursor instanceof BridgeIDBCursor, "cursor exists"); +test("WPT test idbcursor_update_index8.htm", (t) => { + return new Promise((resolve, reject) => { + var db: any, + records = [ + { pKey: "primaryKey_0", iKey: "indexKey_0" }, + { pKey: "primaryKey_1", iKey: "indexKey_1" }, + ]; + + var open_rq = createdb(t); + open_rq.onupgradeneeded = function (e: any) { + db = e.target.result; + + var objStore = db.createObjectStore("store", { keyPath: "pKey" }); + objStore.createIndex("index", "iKey"); + + for (var i = 0; i < records.length; i++) objStore.add(records[i]); + }; - cursor.continue(); - t.throws( - function () { - cursor.update({ pKey: "primaryKey_0", iKey: "indexKey_0_updated" }); - }, - { - name: "InvalidStateError", - }, - ); - - t.end(); + open_rq.onsuccess = function (e: any) { + var cursor_rq = db + .transaction("store", "readwrite") + .objectStore("store") + .index("index") + .openCursor(); + + cursor_rq.onsuccess = function (e: any) { + var cursor = e.target.result; + t.true(cursor instanceof BridgeIDBCursor, "cursor exists"); + + cursor.continue(); + t.throws( + function () { + cursor.update({ pKey: "primaryKey_0", iKey: "indexKey_0_updated" }); + }, + { + name: "InvalidStateError", + }, + ); + + resolve(); + }; }; - }; + }); }); // Index cursor - indexed values updated during iteration -- cgit v1.2.3