idbcursor-iterating-update.any.js (1707B)
1 // META: global=window,worker 2 // META: title=IndexedDB: Index iteration with cursor updates/deletes 3 // META: script=resources/support.js 4 5 'use strict'; 6 7 const objStoreValues = [ 8 {name: 'foo', id: 1}, 9 {name: 'bar', id: 2}, 10 {name: 'foo', id: 3}, 11 {name: 'bar', id: 4}, 12 ]; 13 14 const objStoreValuesByIndex = [ 15 objStoreValues[1], 16 objStoreValues[3], 17 objStoreValues[0], 18 objStoreValues[2], 19 ]; 20 21 const functionsThatShouldNotAffectIteration = [ 22 (cursor) => cursor.update({}), 23 (cursor) => cursor.delete(), 24 ]; 25 26 functionsThatShouldNotAffectIteration.forEach( 27 (func) => indexeddb_test( 28 (t, db) => { 29 const objStore = db.createObjectStore('items', {autoIncrement: true}); 30 objStore.createIndex('name', 'name', {unique: false}); 31 objStoreValues.forEach((value) => objStore.add(value)); 32 }, 33 (t, db) => { 34 const txn = db.transaction('items', 'readwrite'); 35 const objStore = txn.objectStore('items'); 36 const nameIndex = objStore.index('name'); 37 38 const cursorValues = []; 39 nameIndex.openCursor().onsuccess = (evt) => { 40 const cursor = evt.target.result; 41 if (cursor) { 42 func(cursor); 43 cursorValues.push(cursor.value); 44 cursor.continue(); 45 } else { 46 assert_equals( 47 cursorValues.length, 4, 48 `Cursor should iterate over 4 records`); 49 50 cursorValues.forEach((value, i) => { 51 assert_object_equals(value, objStoreValuesByIndex[i]); 52 }); 53 t.done(); 54 } 55 }; 56 }, 57 `Calling ${func} doesn't affect index iteration`));