summaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/idb-wpt-ported/idbcursor-continue-objectstore.test.ts
blob: e3169195f66f276ae12bc8972c71db6c6dbc1a96 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import test from "ava";
import { BridgeIDBCursor } from "../bridge-idb.js";
import { IDBDatabase } from "../idbtypes.js";
import { createdb } from "./wptsupport.js";

// IDBCursor.continue() - object store - iterate to the next record
test("WPT test idbcursor_continue_objectstore.htm", (t) => {
  return new Promise((resolve, reject) => {
    var db: any;
    let count = 0;
    const records = [{ pKey: "primaryKey_0" }, { pKey: "primaryKey_1" }];

    var open_rq = createdb(t);
    open_rq.onupgradeneeded = function (e: any) {
      db = e.target.result;
      var objStore = db.createObjectStore("test", {
        autoIncrement: true,
        keyPath: "pKey",
      });

      for (var i = 0; i < records.length; i++) objStore.add(records[i]);
    };

    open_rq.onsuccess = function (e: any) {
      var store = db.transaction("test").objectStore("test");

      var cursor_rq = store.openCursor();
      cursor_rq.onsuccess = function (e: any) {
        var cursor = e.target.result;
        if (!cursor) {
          t.deepEqual(count, records.length, "cursor run count");
          resolve();
          return;
        }

        var record = cursor.value;
        t.deepEqual(record.pKey, records[count].pKey, "primary key");

        cursor.continue();
        count++;
      };
    };
  });
});

// IDBCursor.continue() - index - attempt to pass a
// key parameter that is not a valid key
test("WPT test idbcursor_continue_objectstore2.htm", (t) => {
  return new Promise((resolve, reject) => {
    var db: any;
    const records = [{ pKey: "primaryKey_0" }, { pKey: "primaryKey_1" }];

    var open_rq = createdb(t);
    open_rq.onupgradeneeded = function (e: any) {
      db = e.target.result;
      var objStore = db.createObjectStore("test", { keyPath: "pKey" });

      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").openCursor();

      cursor_rq.onsuccess = function (e: any) {
        var cursor = e.target.result;

        t.true(cursor instanceof BridgeIDBCursor, "cursor exists");
        t.throws(
          () => {
            cursor.continue({ foo: "42" });
          },
          { name: "DataError" },
        );

        resolve();
      };
    };
  });
});

// IDBCursor.continue() - object store - attempt to iterate to the
// previous record when the direction is set for the next record
test("WPT test idbcursor_continue_objectstore3.htm", (t) => {
  return new Promise((resolve, reject) => {
    var db: IDBDatabase;
    const records = [{ pKey: "primaryKey_0" }, { pKey: "primaryKey_1" }];

    var open_rq = createdb(t);
    open_rq.onupgradeneeded = function (e: any) {
      db = e.target.result;
      var objStore = db.createObjectStore("test", { keyPath: "pKey" });

      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")
        .openCursor(undefined, "next");

      cursor_rq.onsuccess = function (e: any) {
        var cursor = e.target.result;

        t.true(cursor instanceof BridgeIDBCursor, "cursor exist");
        t.throws(
          () => {
            cursor.continue(records[0].pKey);
          },
          {
            name: "DataError",
          },
        );

        resolve();
      };
    };
  });
});

// IDBCursor.continue() - object store - attempt to iterate to the
// next record when the direction is set for the previous record
test("WPT test idbcursor_continue_objectstore4.htm", (t) => {
  return new Promise((resolve, reject) => {
    var db: any;
    const records = [
      { pKey: "primaryKey_0" },
      { pKey: "primaryKey_1" },
      { pKey: "primaryKey_2" },
    ];

    var open_rq = createdb(t);
    open_rq.onupgradeneeded = function (e: any) {
      db = e.target.result;
      var objStore = db.createObjectStore("test", { keyPath: "pKey" });

      for (var i = 0; i < records.length; i++) objStore.add(records[i]);
    };

    open_rq.onsuccess = function (e: any) {
      var count = 0,
        cursor_rq = db
          .transaction("test")
          .objectStore("test")
          .openCursor(null, "prev");

      cursor_rq.onsuccess = function (e: any) {
        var cursor = e.target.result;

        t.true(cursor != null, "cursor exist");

        switch (count) {
          case 0:
            t.deepEqual(
              cursor.value.pKey,
              records[2].pKey,
              "first cursor pkey",
            );
            cursor.continue(records[1].pKey);
            break;

          case 1:
            t.deepEqual(
              cursor.value.pKey,
              records[1].pKey,
              "second cursor pkey",
            );
            t.throws(
              () => {
                console.log("**** continuing cursor");
                cursor.continue(records[2].pKey);
                console.log("**** this should not happen");
              },
              {
                name: "DataError",
              },
            );
            resolve();
            return;

          default:
            t.fail("Unexpected count value: " + count);
        }

        count++;
      };
    };
  });
});

// IDBCursor.continue() - object store - throw TransactionInactiveError
test("WPT test idbcursor_continue_objectstore5.htm", (t) => {
  return new Promise((resolve, reject) => {
    var db: any;
    const records = [{ pKey: "primaryKey_0" }, { pKey: "primaryKey_1" }];

    var open_rq = createdb(t);
    open_rq.onupgradeneeded = function (e: any) {
      db = e.target.result;
      var objStore = db.createObjectStore("test", { keyPath: "pKey" });

      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").openCursor();

      cursor_rq.onsuccess = function (e: any) {
        var cursor = e.target.result;
        t.true(cursor instanceof BridgeIDBCursor, "cursor exists");

        e.target.transaction.abort();
        t.throws(
          () => {
            cursor.continue();
          },
          {
            name: "TransactionInactiveError",
          },
          "Calling continue() should throw an exception TransactionInactiveError when the transaction is not active.",
        );

        resolve();
        return;
      };
    };
  });
});

// IDBCursor.continue() - object store - throw InvalidStateError caused by object store been deleted
test("WPT test idbcursor_continue_objectstore6.htm", (t) => {
  return new Promise((resolve, reject) => {
    var db: any;
    const records = [{ pKey: "primaryKey_0" }, { pKey: "primaryKey_1" }];

    var open_rq = createdb(t);
    open_rq.onupgradeneeded = function (e: any) {
      db = e.target.result;
      var objStore = db.createObjectStore("test", { keyPath: "pKey" });

      for (var i = 0; i < records.length; i++) objStore.add(records[i]);

      var cursor_rq = objStore.openCursor();

      cursor_rq.onsuccess = function (e: any) {
        var cursor = e.target.result;
        t.true(cursor instanceof BridgeIDBCursor, "cursor exists");

        db.deleteObjectStore("test");
        t.throws(
          () => {
            cursor.continue();
          },
          {
            name: "InvalidStateError",
          },
          "If the cursor's source or effective object store has been deleted, the implementation MUST throw a DOMException of type InvalidStateError",
        );

        resolve();
      };
    };
  });
});