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

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

function* testSteps() {
  const dbName = this.window
    ? window.location.pathname
    : "test_transaction_error";
  const dbVersion = 1;
  const objectStoreName = "foo";
  const data = {};
  const dataKey = 1;
  const expectedError = "ConstraintError";

  let request = indexedDB.open(dbName, dbVersion);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = unexpectedSuccessHandler;

  let event = yield undefined;

  info("Creating database");

  let db = event.target.result;
  let objectStore = db.createObjectStore(objectStoreName);
  objectStore.add(data, dataKey);

  request.onupgradeneeded = unexpectedSuccessHandler;
  request.onsuccess = grabEventAndContinueHandler;

  event = yield undefined;

  db = event.target.result;

  try {
    db.transaction(objectStoreName, "versionchange");
    ok(false, "TypeError shall be thrown if transaction mode is wrong.");
  } catch (e) {
    ok(e instanceof TypeError, "got a database exception");
    is(e.name, "TypeError", "correct error");
  }

  let transaction = db.transaction(objectStoreName, "readwrite");
  transaction.onerror = grabEventAndContinueHandler;
  transaction.oncomplete = grabEventAndContinueHandler;

  objectStore = transaction.objectStore(objectStoreName);

  info("Adding duplicate entry with preventDefault()");

  request = objectStore.add(data, dataKey);
  request.onsuccess = unexpectedSuccessHandler;
  request.onerror = grabEventAndContinueHandler;
  event = yield undefined;

  is(event.type, "error", "Got an error event");
  is(event.target, request, "Error event targeted request");
  is(event.currentTarget, request, "Got request error first");
  is(
    event.currentTarget.error.name,
    expectedError,
    "Request has correct error"
  );
  event.preventDefault();

  event = yield undefined;

  is(event.type, "error", "Got an error event");
  is(event.target, request, "Error event targeted request");
  is(event.currentTarget, transaction, "Got transaction error second");
  is(event.currentTarget.error, null, "Transaction has null error");

  event = yield undefined;

  is(event.type, "complete", "Got a complete event");
  is(event.target, transaction, "Complete event targeted transaction");
  is(
    event.currentTarget,
    transaction,
    "Complete event only targeted transaction"
  );
  is(event.currentTarget.error, null, "Transaction has null error");

  // Try again without preventDefault().

  transaction = db.transaction(objectStoreName, "readwrite");
  transaction.onerror = grabEventAndContinueHandler;
  transaction.onabort = grabEventAndContinueHandler;

  objectStore = transaction.objectStore(objectStoreName);

  info("Adding duplicate entry without preventDefault()");

  request = objectStore.add(data, dataKey);
  request.onsuccess = unexpectedSuccessHandler;
  request.onerror = grabEventAndContinueHandler;
  event = yield undefined;

  is(event.type, "error", "Got an error event");
  is(event.target, request, "Error event targeted request");
  is(event.currentTarget, request, "Got request error first");
  is(
    event.currentTarget.error.name,
    expectedError,
    "Request has correct error"
  );

  event = yield undefined;

  is(event.type, "error", "Got an error event");
  is(event.target, request, "Error event targeted request");
  is(event.currentTarget, transaction, "Got transaction error second");
  is(event.currentTarget.error, null, "Transaction has null error");

  event = yield undefined;

  is(event.type, "abort", "Got an abort event");
  is(event.target, transaction, "Abort event targeted transaction");
  is(event.currentTarget, transaction, "Abort event only targeted transaction");
  is(
    event.currentTarget.error.name,
    expectedError,
    "Transaction has correct error"
  );

  finishTest();
}