Index & UniqueIndex: Correctly updates even if the hash has not changed.

This commit is contained in:
Julien Fontanet 2015-05-22 14:46:20 +02:00
parent d55fb36182
commit 47c4516060
4 changed files with 45 additions and 6 deletions

View File

@ -109,9 +109,6 @@ export default class Index {
const prev = keysToHash[key]
const hash = computeHash(value, key)
// Same hash, nothing to do.
if (hash === prev) continue
// Removes item from the previous hash's list if any.
if (prev != null) delete itemsByHash[prev][key]

View File

@ -7,6 +7,7 @@ chai.use(dirtyChai)
import sourceMapSupport from 'source-map-support'
sourceMapSupport.install()
import eventToPromise from 'event-to-promise'
import forEach from 'lodash.foreach'
// -------------------------------------------------------------------
@ -143,6 +144,30 @@ describe('Index', function () {
})
})
it('correctly updates the value even the same object has the same hash', function () {
const item1bis = {
id: item1.id,
group: item1.group,
newProp: true
}
col.update(item1bis)
return eventToPromise(col, 'finish').then(() => {
expect(col.indexes).to.eql({
byGroup: {
foo: {
[item1.id]: item1bis,
[item3.id]: item3
},
bar: {
[item2.id]: item2
}
}
})
})
})
describe('#sweep()', function () {
it('removes empty items lists', function () {
col.remove(item2)

View File

@ -90,9 +90,6 @@ export default class UniqueIndex {
const prev = keysToHash[key]
const hash = computeHash(value, key)
// Same hash, nothing to do.
if (hash === prev) continue
// Removes item from the previous hash's list if any.
if (prev != null) delete itemByHash[prev]

View File

@ -7,6 +7,7 @@ chai.use(dirtyChai)
import sourceMapSupport from 'source-map-support'
sourceMapSupport.install()
import eventToPromise from 'event-to-promise'
import forEach from 'lodash.foreach'
// -------------------------------------------------------------------
@ -117,4 +118,23 @@ describe('UniqueIndex', function () {
})
})
})
it('correctly updates the value even the same object has the same hash', function () {
const item1bis = {
id: item1.id,
key: item1.key,
newProp: true
}
col.update(item1bis)
return eventToPromise(col, 'finish').then(() => {
expect(col.indexes).to.eql({
byKey: {
[item1.key]: item1bis,
[item2.key]: item2
}
})
})
})
})