diff options
author | Florian Dold <florian@dold.me> | 2021-02-17 11:45:28 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-02-17 11:45:28 +0100 |
commit | 69b62c62a0e417985f6e104edd9b8a7fd75a0f81 (patch) | |
tree | 853f36b492be78c1dfe57746de4c9d476e243841 /packages/idb-bridge/src/idb-wpt-ported | |
parent | 47bddb2a1b5c603f6440d4745a2ac33cdc85d9ac (diff) | |
download | wallet-core-69b62c62a0e417985f6e104edd9b8a7fd75a0f81.tar.xz |
idb: fix renaming, make renaming tests pass
Diffstat (limited to 'packages/idb-bridge/src/idb-wpt-ported')
-rw-r--r-- | packages/idb-bridge/src/idb-wpt-ported/idbfactory-open.test.ts | 61 | ||||
-rw-r--r-- | packages/idb-bridge/src/idb-wpt-ported/idbobjectstore-rename-store.test.ts | 308 |
2 files changed, 211 insertions, 158 deletions
diff --git a/packages/idb-bridge/src/idb-wpt-ported/idbfactory-open.test.ts b/packages/idb-bridge/src/idb-wpt-ported/idbfactory-open.test.ts index 102b54719..4ba7caa6f 100644 --- a/packages/idb-bridge/src/idb-wpt-ported/idbfactory-open.test.ts +++ b/packages/idb-bridge/src/idb-wpt-ported/idbfactory-open.test.ts @@ -1,4 +1,6 @@ import test from "ava"; +import { BridgeIDBVersionChangeEvent } from "../bridge-idb"; +import FakeEvent from "../util/FakeEvent"; import { createdb, format_value, idbFactory } from "./wptsupport"; // IDBFactory.open() - request has no source @@ -466,3 +468,62 @@ test("WPT idbfactory-open11.htm", async (t) => { }); t.pass(); }); + +// IDBFactory.open() - upgradeneeded gets VersionChangeEvent +test("WPT idbfactory-open12.htm", async (t) => { + const indexedDB = idbFactory; + + var db: any; + var open_rq = createdb(t, undefined, 9); + + await new Promise<void>((resolve, reject) => { + open_rq.onupgradeneeded = function (e: any) { + db = e.target.result; + + t.true( + e instanceof BridgeIDBVersionChangeEvent, + "e instanceof IDBVersionChangeEvent", + ); + t.deepEqual(e.oldVersion, 0, "oldVersion"); + t.deepEqual(e.newVersion, 9, "newVersion"); + t.deepEqual(e.type, "upgradeneeded", "event type"); + + t.deepEqual(db.version, 9, "db.version"); + }; + open_rq.onsuccess = function (e) { + t.true(e instanceof FakeEvent, "e instanceof Event"); + t.false( + e instanceof BridgeIDBVersionChangeEvent, + "e not instanceof IDBVersionChangeEvent", + ); + t.deepEqual(e.type, "success", "event type"); + resolve(); + }; + }); + + await new Promise<void>((resolve, reject) => { + /** + * Second test + */ + db.onversionchange = function () { + db.close(); + }; + + var open_rq2 = createdb(t, db.name, 10); + open_rq2.onupgradeneeded = function (e: any) { + var db2 = e.target.result; + t.true( + e instanceof BridgeIDBVersionChangeEvent, + "e instanceof IDBVersionChangeEvent", + ); + t.deepEqual(e.oldVersion, 9, "oldVersion"); + t.deepEqual(e.newVersion, 10, "newVersion"); + t.deepEqual(e.type, "upgradeneeded", "event type"); + + t.deepEqual(db2.version, 10, "new db.version"); + + resolve(); + }; + }); + t.pass(); +}); diff --git a/packages/idb-bridge/src/idb-wpt-ported/idbobjectstore-rename-store.test.ts b/packages/idb-bridge/src/idb-wpt-ported/idbobjectstore-rename-store.test.ts index 0f872fa51..40f4dda98 100644 --- a/packages/idb-bridge/src/idb-wpt-ported/idbobjectstore-rename-store.test.ts +++ b/packages/idb-bridge/src/idb-wpt-ported/idbobjectstore-rename-store.test.ts @@ -1,13 +1,10 @@ import test, { ExecutionContext } from "ava"; -import { BridgeIDBRequest } from ".."; -import { EventTarget, IDBDatabase } from "../idbtypes"; import { checkStoreContents, checkStoreGenerator, checkStoreIndexes, createBooksStore, createDatabase, - createdb, createNotBooksStore, migrateDatabase, } from "./wptsupport"; @@ -15,183 +12,175 @@ import { // IndexedDB: object store renaming support // IndexedDB object store rename in new transaction test("WPT idbobjectstore-rename-store.html (subtest 1)", async (t) => { - await new Promise<void>((resolve, reject) => { - let bookStore: any = null; - let bookStore2: any = null; - let renamedBookStore: any = null; - let renamedBookStore2: any = null; - - return createDatabase(t, (database, transaction) => { - bookStore = createBooksStore(t, database); + let bookStore: any = null; + let bookStore2: any = null; + let renamedBookStore: any = null; + let renamedBookStore2: any = null; + await createDatabase(t, (database, transaction) => { + bookStore = createBooksStore(t, database); + }) + .then((database) => { + t.deepEqual( + database.objectStoreNames as any, + ["books"], + 'Test setup should have created a "books" object store', + ); + const transaction = database.transaction("books", "readonly"); + bookStore2 = transaction.objectStore("books"); + return checkStoreContents( + t, + bookStore2, + "The store should have the expected contents before any renaming", + ).then(() => database.close()); }) - .then((database) => { + .then(() => + migrateDatabase(t, 2, (database, transaction) => { + renamedBookStore = transaction.objectStore("books"); + renamedBookStore.name = "renamed_books"; + t.deepEqual( - database.objectStoreNames as any, - ["books"], - 'Test setup should have created a "books" object store', + renamedBookStore.name, + "renamed_books", + "IDBObjectStore name should change immediately after a rename", ); - const transaction = database.transaction("books", "readonly"); - bookStore2 = transaction.objectStore("books"); - return checkStoreContents( - t, - bookStore2, - "The store should have the expected contents before any renaming", - ).then(() => database.close()); - }) - .then(() => - migrateDatabase(t, 2, (database, transaction) => { - renamedBookStore = transaction.objectStore("books"); - renamedBookStore.name = "renamed_books"; - - t.deepEqual( - renamedBookStore.name, - "renamed_books", - "IDBObjectStore name should change immediately after a rename", - ); - t.deepEqual( - database.objectStoreNames as any, - ["renamed_books"], - "IDBDatabase.objectStoreNames should immediately reflect the " + - "rename", - ); - t.deepEqual( - transaction.objectStoreNames as any, - ["renamed_books"], - "IDBTransaction.objectStoreNames should immediately reflect the " + - "rename", - ); - t.deepEqual( - transaction.objectStore("renamed_books"), - renamedBookStore, - "IDBTransaction.objectStore should return the renamed object " + - "store when queried using the new name immediately after the " + - "rename", - ); - t.throws( - () => transaction.objectStore("books"), - { name: "NotFoundError" }, - "IDBTransaction.objectStore should throw when queried using the " + - "renamed object store's old name immediately after the rename", - ); - }), - ) - .then((database) => { t.deepEqual( database.objectStoreNames as any, ["renamed_books"], - "IDBDatabase.objectStoreNames should still reflect the rename " + - "after the versionchange transaction commits", - ); - const transaction = database.transaction("renamed_books", "readonly"); - renamedBookStore2 = transaction.objectStore("renamed_books"); - return checkStoreContents( - t, - renamedBookStore2, - "Renaming an object store should not change its records", - ).then(() => database.close()); - }) - .then(() => { - t.deepEqual( - bookStore.name, - "books", - "IDBObjectStore obtained before the rename transaction should " + - "not reflect the rename", + "IDBDatabase.objectStoreNames should immediately reflect the " + + "rename", ); t.deepEqual( - bookStore2.name, - "books", - "IDBObjectStore obtained before the rename transaction should " + - "not reflect the rename", + transaction.objectStoreNames as any, + ["renamed_books"], + "IDBTransaction.objectStoreNames should immediately reflect the " + + "rename", ); t.deepEqual( - renamedBookStore.name, - "renamed_books", - "IDBObjectStore used in the rename transaction should keep " + - "reflecting the new name after the transaction is committed", + transaction.objectStore("renamed_books"), + renamedBookStore, + "IDBTransaction.objectStore should return the renamed object " + + "store when queried using the new name immediately after the " + + "rename", ); - t.deepEqual( - renamedBookStore2.name, - "renamed_books", - "IDBObjectStore obtained after the rename transaction should " + - "reflect the new name", + t.throws( + () => transaction.objectStore("books"), + { name: "NotFoundError" }, + "IDBTransaction.objectStore should throw when queried using the " + + "renamed object store's old name immediately after the rename", ); - }); - }); - t.pass(); + }), + ) + .then((database) => { + t.deepEqual( + database.objectStoreNames as any, + ["renamed_books"], + "IDBDatabase.objectStoreNames should still reflect the rename " + + "after the versionchange transaction commits", + ); + const transaction = database.transaction("renamed_books", "readonly"); + renamedBookStore2 = transaction.objectStore("renamed_books"); + return checkStoreContents( + t, + renamedBookStore2, + "Renaming an object store should not change its records", + ).then(() => database.close()); + }) + .then(() => { + t.deepEqual( + bookStore.name, + "books", + "IDBObjectStore obtained before the rename transaction should " + + "not reflect the rename", + ); + t.deepEqual( + bookStore2.name, + "books", + "IDBObjectStore obtained before the rename transaction should " + + "not reflect the rename", + ); + t.deepEqual( + renamedBookStore.name, + "renamed_books", + "IDBObjectStore used in the rename transaction should keep " + + "reflecting the new name after the transaction is committed", + ); + t.deepEqual( + renamedBookStore2.name, + "renamed_books", + "IDBObjectStore obtained after the rename transaction should " + + "reflect the new name", + ); + }); }); // IndexedDB: object store renaming support // IndexedDB object store rename in the transaction where it is created test("WPT idbobjectstore-rename-store.html (subtest 2)", async (t) => { - await new Promise<void>((resolve, reject) => { - let renamedBookStore: any = null, - renamedBookStore2: any = null; - return createDatabase(t, (database, transaction) => { - renamedBookStore = createBooksStore(t, database); - renamedBookStore.name = "renamed_books"; + let renamedBookStore: any = null, + renamedBookStore2: any = null; + await createDatabase(t, (database, transaction) => { + renamedBookStore = createBooksStore(t, database); + renamedBookStore.name = "renamed_books"; - t.deepEqual( - renamedBookStore.name, - "renamed_books", - "IDBObjectStore name should change immediately after a rename", - ); + t.deepEqual( + renamedBookStore.name, + "renamed_books", + "IDBObjectStore name should change immediately after a rename", + ); + t.deepEqual( + database.objectStoreNames as any, + ["renamed_books"], + "IDBDatabase.objectStoreNames should immediately reflect the " + "rename", + ); + t.deepEqual( + transaction.objectStoreNames as any, + ["renamed_books"], + "IDBTransaction.objectStoreNames should immediately reflect the " + + "rename", + ); + t.deepEqual( + transaction.objectStore("renamed_books"), + renamedBookStore, + "IDBTransaction.objectStore should return the renamed object " + + "store when queried using the new name immediately after the " + + "rename", + ); + t.throws( + () => transaction.objectStore("books"), + { name: "NotFoundError" }, + "IDBTransaction.objectStore should throw when queried using the " + + "renamed object store's old name immediately after the rename", + ); + }) + .then((database) => { t.deepEqual( database.objectStoreNames as any, ["renamed_books"], - "IDBDatabase.objectStoreNames should immediately reflect the " + - "rename", + "IDBDatabase.objectStoreNames should still reflect the rename " + + "after the versionchange transaction commits", ); + const transaction = database.transaction("renamed_books", "readonly"); + renamedBookStore2 = transaction.objectStore("renamed_books"); + return checkStoreContents( + t, + renamedBookStore2, + "Renaming an object store should not change its records", + ).then(() => database.close()); + }) + .then(() => { t.deepEqual( - transaction.objectStoreNames as any, - ["renamed_books"], - "IDBTransaction.objectStoreNames should immediately reflect the " + - "rename", + renamedBookStore.name, + "renamed_books", + "IDBObjectStore used in the rename transaction should keep " + + "reflecting the new name after the transaction is committed", ); t.deepEqual( - transaction.objectStore("renamed_books"), - renamedBookStore, - "IDBTransaction.objectStore should return the renamed object " + - "store when queried using the new name immediately after the " + - "rename", - ); - t.throws( - () => transaction.objectStore("books"), - { name: "NotFoundError" }, - "IDBTransaction.objectStore should throw when queried using the " + - "renamed object store's old name immediately after the rename", + renamedBookStore2.name, + "renamed_books", + "IDBObjectStore obtained after the rename transaction should " + + "reflect the new name", ); - }) - .then((database) => { - t.deepEqual( - database.objectStoreNames as any, - ["renamed_books"], - "IDBDatabase.objectStoreNames should still reflect the rename " + - "after the versionchange transaction commits", - ); - const transaction = database.transaction("renamed_books", "readonly"); - renamedBookStore2 = transaction.objectStore("renamed_books"); - return checkStoreContents( - t, - renamedBookStore2, - "Renaming an object store should not change its records", - ).then(() => database.close()); - }) - .then(() => { - t.deepEqual( - renamedBookStore.name, - "renamed_books", - "IDBObjectStore used in the rename transaction should keep " + - "reflecting the new name after the transaction is committed", - ); - t.deepEqual( - renamedBookStore2.name, - "renamed_books", - "IDBObjectStore obtained after the rename transaction should " + - "reflect the new name", - ); - }); - }); - t.pass(); + }); }); // Renames the 'books' store to 'renamed_books'. @@ -333,13 +322,13 @@ test("WPT idbobjectstore-rename-store.html (IndexedDB object store swapping via "IDBDatabase.objectStoreNames should immediately reflect the swap", ); - t.deepEqual( + t.is( transaction.objectStore("books"), notBookStore, 'IDBTransaction.objectStore should return the original "books" ' + 'store when queried with "not_books" after the swap', ); - t.deepEqual( + t.is( transaction.objectStore("not_books"), bookStore, "IDBTransaction.objectStore should return the original " + @@ -452,9 +441,12 @@ test("WPT idbobjectstore-rename-store.html (IndexedDB object store rename string t.pass(); }); -function rename_test_macro(t: ExecutionContext, escapedName: string) { +function rename_test_macro( + t: ExecutionContext, + escapedName: string, +): Promise<void> { const name = JSON.parse('"' + escapedName + '"'); - createDatabase(t, (database, transaction) => { + return createDatabase(t, (database, transaction) => { createBooksStore(t, database); }) .then((database) => { |