1 import { arrayInterpolate } from './interpolate';
3 type TestEntry = { value: number };
4 type TestCluster = { max: number };
6 const shouldInterpolate = (entry: TestEntry, cluster: TestCluster) => entry.value < cluster.max;
7 const fallbackCluster: TestCluster = { max: 0 };
9 describe('Items list cluster interpolation', () => {
10 test('it should not interpolate on empty entries', () => {
11 const result = arrayInterpolate<TestEntry, TestCluster>([], {
12 clusters: [{ max: 100 }],
17 expect(result.interpolation).toStrictEqual([]);
18 expect(result.interpolationIndexes).toEqual([]);
21 test('it should only interpolate first cluster if it is the only match', () => {
22 const entries: TestEntry[] = [{ value: 1 }, { value: 2 }, { value: 3 }];
23 const clusters: TestCluster[] = [{ max: 10 }, { max: 20 }];
25 const { interpolation, interpolationIndexes } = arrayInterpolate<TestEntry, TestCluster>(entries, {
30 expect(interpolation[0]).toEqual({ type: 'interpolation', cluster: clusters[0] });
31 expect(interpolation[1]).toEqual({ type: 'entry', entry: entries[0] });
32 expect(interpolation[2]).toEqual({ type: 'entry', entry: entries[1] });
33 expect(interpolation[3]).toEqual({ type: 'entry', entry: entries[2] });
34 expect(interpolationIndexes).toEqual([0]);
37 test('it should interpolate last cluster if it is the only match', () => {
38 const entries: TestEntry[] = [{ value: 100 }, { value: 101 }, { value: 102 }];
39 const clusters: TestCluster[] = [{ max: 10 }, { max: 20 }, { max: 1000 }];
41 const { interpolation, interpolationIndexes } = arrayInterpolate<TestEntry, TestCluster>(entries, {
46 expect(interpolation[0]).toEqual({ type: 'interpolation', cluster: clusters[2] });
47 expect(interpolation[1]).toEqual({ type: 'entry', entry: entries[0] });
48 expect(interpolation[2]).toEqual({ type: 'entry', entry: entries[1] });
49 expect(interpolation[3]).toEqual({ type: 'entry', entry: entries[2] });
50 expect(interpolationIndexes).toEqual([0]);
53 test('it should interpolate to fallback cluster if there is no match', () => {
54 const entries: TestEntry[] = [{ value: 1 }, { value: 2 }, { value: 3 }];
55 const clusters: TestCluster[] = [{ max: 0 }];
57 const { interpolation, interpolationIndexes } = arrayInterpolate<TestEntry, TestCluster>(entries, {
63 expect(interpolation[0]).toEqual({ type: 'interpolation', cluster: fallbackCluster });
64 expect(interpolation[1]).toEqual({ type: 'entry', entry: entries[0] });
65 expect(interpolation[2]).toEqual({ type: 'entry', entry: entries[1] });
66 expect(interpolation[3]).toEqual({ type: 'entry', entry: entries[2] });
67 expect(interpolationIndexes).toEqual([0]);
70 test('it should interpolate into sub-clusters', () => {
71 const entries: TestEntry[] = [{ value: 1 }, { value: 10 }, { value: 11 }, { value: 20 }, { value: 30 }];
72 const clusters: TestCluster[] = [{ max: 5 }, { max: 15 }, { max: 25 }, { max: 35 }];
74 const { interpolation, interpolationIndexes } = arrayInterpolate<TestEntry, TestCluster>(entries, {
79 expect(interpolation[0]).toEqual({ type: 'interpolation', cluster: clusters[0] });
80 expect(interpolation[1]).toEqual({ type: 'entry', entry: entries[0] });
81 expect(interpolation[2]).toEqual({ type: 'interpolation', cluster: clusters[1] });
82 expect(interpolation[3]).toEqual({ type: 'entry', entry: entries[1] });
83 expect(interpolation[4]).toEqual({ type: 'entry', entry: entries[2] });
84 expect(interpolation[5]).toEqual({ type: 'interpolation', cluster: clusters[2] });
85 expect(interpolation[6]).toEqual({ type: 'entry', entry: entries[3] });
86 expect(interpolation[7]).toEqual({ type: 'interpolation', cluster: clusters[3] });
87 expect(interpolation[8]).toEqual({ type: 'entry', entry: entries[4] });
88 expect(interpolationIndexes).toEqual([0, 2, 5, 7]);