idbindex_keyPath.any.js (2424B)
1 // META: title=IndexedDB: IDBIndex keyPath attribute 2 // META: script=resources/support.js 3 'use strict'; 4 5 indexeddb_test( 6 (t, db) => { 7 const store = db.createObjectStore('store', {keyPath: ['a', 'b']}); 8 store.createIndex('index', ['a', 'b']); 9 }, 10 (t, db) => { 11 const tx = db.transaction('store', 'readonly'); 12 const store = tx.objectStore('store'); 13 const index = store.index('index'); 14 assert_equals(typeof index.keyPath, 'object', 'keyPath is an object'); 15 assert_true(Array.isArray(index.keyPath), 'keyPath is an array'); 16 17 assert_equals( 18 index.keyPath, index.keyPath, 19 'Same object instance is returned each time keyPath is inspected'); 20 21 const tx2 = db.transaction('store', 'readonly'); 22 const store2 = tx2.objectStore('store'); 23 const index2 = store2.index('index'); 24 25 assert_not_equals( 26 index.keyPath, index2.keyPath, 27 'Different instances are returned from different index instances.'); 28 29 t.done(); 30 }, 31 `IDBIndex's keyPath attribute returns the same object.`); 32 33 indexeddb_test( 34 (t, db) => { 35 const store = db.createObjectStore('store', {autoIncrement: true}); 36 store.createIndex('index', ['a']); 37 38 store.add({a: 1, b: 2, c: 3}) 39 }, 40 (t, db) => { 41 const tx = db.transaction('store', 'readonly'); 42 const store = tx.objectStore('store'); 43 const index = store.index('index'); 44 const cursorReq = index.openCursor(); 45 46 cursorReq.onsuccess = t.step_func_done((e) => { 47 const expectedKeyValue = [1]; 48 const actualKeyValue = e.target.result.key; 49 50 assert_array_equals(actualKeyValue, expectedKeyValue, "An array keypath should yield an array key"); 51 }); 52 }, 53 `IDBIndex's keyPath array with a single value`); 54 55 indexeddb_test( 56 (t, db) => { 57 const store = db.createObjectStore('store', {autoIncrement: true}); 58 store.createIndex('index', ['a', 'b']); 59 60 store.add({a: 1, b: 2, c: 3}) 61 }, 62 (t, db) => { 63 const tx = db.transaction('store', 'readonly'); 64 const store = tx.objectStore('store'); 65 const index = store.index('index'); 66 const cursorReq = index.openCursor(); 67 68 cursorReq.onsuccess = t.step_func_done((e) => { 69 const expectedKeyValue = [1, 2]; 70 const actualKeyValue = e.target.result.key; 71 72 assert_array_equals(actualKeyValue, expectedKeyValue, "An array keypath should yield an array key"); 73 }); 74 }, 75 `IDBIndex's keyPath array with multiple values`);