Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add more tests for MultiIndexer
Browse files Browse the repository at this point in the history
  • Loading branch information
sz-piotr committed Mar 21, 2024
1 parent 4c05d46 commit efaf191
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
119 changes: 119 additions & 0 deletions packages/uif/src/indexers/multi/MultiIndexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@ import { IMultiIndexer, MultiIndexer } from './MultiIndexer'
import { Configuration, SavedConfiguration } from './types'

describe(MultiIndexer.name, () => {
describe(MultiIndexer.prototype.initialize.name, () => {
it('calls multiInitialize and saves configurations', async () => {
const testIndexer = new TestMultiIndexer(
[actual('a', 100, 400), actual('b', 200, 500)],
[saved('a', 100, 300), saved('b', 200, 300), saved('c', 100, 300)],
)

const newHeight = await testIndexer.initialize()
expect(newHeight).toEqual(300)

expect(testIndexer.removeData).toHaveBeenOnlyCalledWith([
removal('c', 100, 300),
])
expect(testIndexer.saveConfigurations).toHaveBeenOnlyCalledWith([
saved('a', 100, 300),
saved('b', 200, 300),
])
})

it('skips calling removeData if there is nothing to remove', async () => {
const testIndexer = new TestMultiIndexer(
[actual('a', 100, 400), actual('b', 200, 500)],
[saved('a', 100, 400), saved('b', 200, 500)],
)

const newHeight = await testIndexer.initialize()
expect(newHeight).toEqual(Infinity)

expect(testIndexer.removeData).not.toHaveBeenCalled()
expect(testIndexer.saveConfigurations).not.toHaveBeenCalled()
})
})

describe(MultiIndexer.prototype.update.name, () => {
it('calls multiUpdate with an early matching configuration', async () => {
const testIndexer = new TestMultiIndexer(
Expand Down Expand Up @@ -125,6 +158,84 @@ describe(MultiIndexer.name, () => {
expect(testIndexer.saveConfigurations).not.toHaveBeenCalled()
})
})

describe('multiUpdate', () => {
it('returns the currentHeight', async () => {
const testIndexer = new TestMultiIndexer(
[actual('a', 100, 300), actual('b', 100, 400)],
[saved('a', 100, 200), saved('b', 100, 200)],
)
await testIndexer.initialize()

testIndexer.multiUpdate.resolvesTo(200)

const newHeight = await testIndexer.update(200, 500)
expect(newHeight).toEqual(200)
expect(testIndexer.saveConfigurations).not.toHaveBeenCalled()
})

it('returns the targetHeight', async () => {
const testIndexer = new TestMultiIndexer(
[actual('a', 100, 300), actual('b', 100, 400)],
[saved('a', 100, 200), saved('b', 100, 200)],
)
await testIndexer.initialize()

testIndexer.multiUpdate.resolvesTo(300)

const newHeight = await testIndexer.update(200, 300)
expect(newHeight).toEqual(300)
expect(testIndexer.saveConfigurations).toHaveBeenOnlyCalledWith([
saved('a', 100, 300),
saved('b', 100, 300),
])
})

it('returns something in between', async () => {
const testIndexer = new TestMultiIndexer(
[actual('a', 100, 300), actual('b', 100, 400)],
[saved('a', 100, 200), saved('b', 100, 200)],
)
await testIndexer.initialize()

testIndexer.multiUpdate.resolvesTo(250)

const newHeight = await testIndexer.update(200, 300)
expect(newHeight).toEqual(250)
expect(testIndexer.saveConfigurations).toHaveBeenOnlyCalledWith([
saved('a', 100, 250),
saved('b', 100, 250),
])
})

it('cannot return less than currentHeight', async () => {
const testIndexer = new TestMultiIndexer(
[actual('a', 100, 300), actual('b', 100, 400)],
[saved('a', 100, 200), saved('b', 100, 200)],
)
await testIndexer.initialize()

testIndexer.multiUpdate.resolvesTo(150)

await expect(testIndexer.update(200, 300)).toBeRejectedWith(
/returned height must be between currentHeight and targetHeight/,
)
})

it('cannot return more than targetHeight', async () => {
const testIndexer = new TestMultiIndexer(
[actual('a', 100, 300), actual('b', 100, 400)],
[saved('a', 100, 200), saved('b', 100, 200)],
)
await testIndexer.initialize()

testIndexer.multiUpdate.resolvesTo(350)

await expect(testIndexer.update(200, 300)).toBeRejectedWith(
/returned height must be between currentHeight and targetHeight/,
)
})
})
})

class TestMultiIndexer
Expand Down Expand Up @@ -159,3 +270,11 @@ function actual(id: string, minHeight: number, maxHeight: number | null) {
function saved(id: string, minHeight: number, currentHeight: number) {
return { id, minHeight, currentHeight }
}

function removal(
id: string,
fromHeightInclusive: number,
toHeightInclusive: number,
) {
return { id, fromHeightInclusive, toHeightInclusive }
}
4 changes: 2 additions & 2 deletions packages/uif/src/indexers/multi/MultiIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export interface IMultiIndexer<T> {
* in each configuration. It is possible for multiple ranges to share a
* configuration id!
*
* This method is only called during the initialization of the indexer, after
* `multiInitialize` returns.
* This method can only be called during the initialization of the indexer,
* after `multiInitialize` returns.
*/
removeData: (configurations: RemovalConfiguration[]) => Promise<void>

Expand Down

0 comments on commit efaf191

Please sign in to comment.