summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_unique_index_update.js
blob: 736344ae5e38ed9638af24a64f2e82380c952fb4 (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/* exported testGenerator */
var testGenerator = testSteps();

function* testSteps() {
  let request = indexedDB.open(
    this.window ? window.location.pathname : "Splendid Test",
    1
  );
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = grabEventAndContinueHandler;

  let event = yield undefined;

  let db = event.target.result;

  for (let autoIncrement of [false, true]) {
    let objectStore = db.createObjectStore(autoIncrement, {
      keyPath: "id",
      autoIncrement,
    });
    objectStore.createIndex("", "index", { unique: true });

    for (let i = 0; i < 10; i++) {
      objectStore.add({ id: i, index: i });
    }
  }

  event = yield undefined;
  is(event.type, "success", "expect a success event");

  for (let autoIncrement of [false, true]) {
    let objectStore = db
      .transaction(autoIncrement, "readwrite")
      .objectStore(autoIncrement);

    request = objectStore.put({ id: 5, index: 6 });
    request.onsuccess = unexpectedSuccessHandler;
    request.addEventListener("error", new ExpectError("ConstraintError", true));
    event = yield undefined;

    event.preventDefault();

    let keyRange = IDBKeyRange.only(5);

    objectStore.index("").openCursor(keyRange).onsuccess = function (event) {
      let cursor = event.target.result;
      ok(cursor, "Must have a cursor here");

      is(cursor.value.index, 5, "Still have the right index value");

      cursor.value.index = 6;

      request = cursor.update(cursor.value);
      request.onsuccess = unexpectedSuccessHandler;
      request.addEventListener(
        "error",
        new ExpectError("ConstraintError", true)
      );
    };

    yield undefined;
  }

  finishTest();
}