summaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/idb-wpt-ported/cursor-overloads.test.ts
blob: 795d515ed4e9c19c3cf4493d470e6cc857880581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import test from "ava";
import { BridgeIDBKeyRange } from "../bridge-idb.js";
import { IDBRequest } from "../idbtypes.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";

test.before("test DB initialization", initTestIndexedDB);

const IDBKeyRange = BridgeIDBKeyRange;

// Validate the overloads of IDBObjectStore.openCursor(),
// IDBIndex.openCursor() and IDBIndex.openKeyCursor()
test("WPT test cursor-overloads.htm", (t) => {
  return new Promise((resolve, reject) => {
    var db: any, store: any, index: any;

    var request = createdb(t);
    request.onupgradeneeded = function (e: any) {
      db = request.result;
      store = db.createObjectStore("store");
      index = store.createIndex("index", "value");
      store.put({ value: 0 }, 0);
      const trans = request.transaction!;
      trans.oncomplete = verifyOverloads;
    };

    async function verifyOverloads() {
      const trans = db.transaction("store");
      store = trans.objectStore("store");
      index = store.index("index");

      await checkCursorDirection(store.openCursor(), "next");
      await checkCursorDirection(store.openCursor(0), "next");
      await checkCursorDirection(store.openCursor(0, "next"), "next");
      await checkCursorDirection(
        store.openCursor(0, "nextunique"),
        "nextunique",
      );
      await checkCursorDirection(store.openCursor(0, "prev"), "prev");
      await checkCursorDirection(
        store.openCursor(0, "prevunique"),
        "prevunique",
      );

      await checkCursorDirection(store.openCursor(IDBKeyRange.only(0)), "next");
      await checkCursorDirection(
        store.openCursor(BridgeIDBKeyRange.only(0), "next"),
        "next",
      );
      await checkCursorDirection(
        store.openCursor(IDBKeyRange.only(0), "nextunique"),
        "nextunique",
      );
      await checkCursorDirection(
        store.openCursor(IDBKeyRange.only(0), "prev"),
        "prev",
      );
      await checkCursorDirection(
        store.openCursor(IDBKeyRange.only(0), "prevunique"),
        "prevunique",
      );

      await checkCursorDirection(index.openCursor(), "next");
      await checkCursorDirection(index.openCursor(0), "next");
      await checkCursorDirection(index.openCursor(0, "next"), "next");
      await checkCursorDirection(
        index.openCursor(0, "nextunique"),
        "nextunique",
      );
      await checkCursorDirection(index.openCursor(0, "prev"), "prev");
      await checkCursorDirection(
        index.openCursor(0, "prevunique"),
        "prevunique",
      );

      await checkCursorDirection(index.openCursor(IDBKeyRange.only(0)), "next");
      await checkCursorDirection(
        index.openCursor(IDBKeyRange.only(0), "next"),
        "next",
      );
      await checkCursorDirection(
        index.openCursor(IDBKeyRange.only(0), "nextunique"),
        "nextunique",
      );
      await checkCursorDirection(
        index.openCursor(IDBKeyRange.only(0), "prev"),
        "prev",
      );
      await checkCursorDirection(
        index.openCursor(IDBKeyRange.only(0), "prevunique"),
        "prevunique",
      );

      await checkCursorDirection(index.openKeyCursor(), "next");
      await checkCursorDirection(index.openKeyCursor(0), "next");
      await checkCursorDirection(index.openKeyCursor(0, "next"), "next");
      await checkCursorDirection(
        index.openKeyCursor(0, "nextunique"),
        "nextunique",
      );
      await checkCursorDirection(index.openKeyCursor(0, "prev"), "prev");
      await checkCursorDirection(
        index.openKeyCursor(0, "prevunique"),
        "prevunique",
      );

      await checkCursorDirection(
        index.openKeyCursor(IDBKeyRange.only(0)),
        "next",
      );
      await checkCursorDirection(
        index.openKeyCursor(IDBKeyRange.only(0), "next"),
        "next",
      );
      await checkCursorDirection(
        index.openKeyCursor(IDBKeyRange.only(0), "nextunique"),
        "nextunique",
      );
      await checkCursorDirection(
        index.openKeyCursor(IDBKeyRange.only(0), "prev"),
        "prev",
      );
      await checkCursorDirection(
        index.openKeyCursor(IDBKeyRange.only(0), "prevunique"),
        "prevunique",
      );

      resolve();
    }
  });

  function checkCursorDirection(
    request: IDBRequest,
    direction: string,
  ): Promise<void> {
    return new Promise<void>((resolve, reject) => {
      request.onsuccess = function (event: any) {
        t.notDeepEqual(
          event.target.result,
          null,
          "Check the result is not null",
        );
        t.deepEqual(
          event.target.result.direction,
          direction,
          "Check the result direction",
        );
        resolve();
      };
    });
  }
});