Skip to content

Commit de72ee3

Browse files
committed
issue-171 limited ds refactoring (tests)
1 parent ced0c87 commit de72ee3

17 files changed

+55
-60
lines changed

tests/adapter.append-prepend.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Direction } from '../src/component/interfaces';
2-
import { makeTest, TestBedConfig, OperationConfig } from './scaffolding/runner';
2+
import { makeTest, OperationConfig } from './scaffolding/runner';
33
import { Misc } from './miscellaneous/misc';
44
import { Item } from './miscellaneous/items';
55

@@ -80,7 +80,7 @@ describe('Adapter Append-Prepend Spec', () => {
8080
[Operation.append]: [...Array(total).keys()].map((i: number) => ({
8181
id: max - total + i + 1,
8282
text: 'item #' + (max - total + i + 1)
83-
})),
83+
})),
8484
[Operation.prepend]: [...Array(total).keys()].map((i: number) => ({
8585
id: min + i,
8686
text: 'item #' + (min + i)

tests/adapter.check.spec.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { makeTest, TestBedConfig } from './scaffolding/runner';
22
import { Misc } from './miscellaneous/misc';
3-
import { IndexedItem } from './miscellaneous/items';
43

54
const MIN_INDEX = -99;
65
const MAX_INDEX = 100;
@@ -98,12 +97,11 @@ const updateDOMElement = (misc: Misc, index: number, size: number) => {
9897
};
9998

10099
const updateDOM = (misc: Misc, { min, max, size, initialSize }: any) => {
101-
const { datasource } = misc.fixture.componentInstance;
102100
for (let i = min; i <= max; i++) {
103101
updateDOMElement(misc, i, size);
104102
// persist new sizes on the datasource level
105-
(datasource as any).setProcessGet((result: IndexedItem[]) =>
106-
result.forEach(({ data }) => data.size = data.id >= min && data.id <= max ? size : initialSize)
103+
misc.setItemProcessor(({ data }) =>
104+
data.size = data.id >= min && data.id <= max ? size : initialSize
107105
);
108106
}
109107
};
@@ -129,9 +127,7 @@ const shouldCheck = (config: TestBedConfig) => (misc: Misc) => (done: Function)
129127
const { min, max, size } = config.custom;
130128
const changedCount = (max - min + 1);
131129
let firstVisibleIndex = NaN;
132-
(misc.datasource as any).setProcessGet((result: IndexedItem[]) =>
133-
result.forEach(({ data }) => data.size = initialSize)
134-
);
130+
misc.setItemProcessor(({ data }) => data.size = initialSize);
135131
spyOn(misc.workflow, 'finalize').and.callFake(() => {
136132
const cycle = state.cycle.count;
137133
const { firstVisible } = adapter; // need to have a pre-call

tests/adapter.insert.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe('Adapter Insert Spec', () => {
143143
return;
144144
}
145145
// insert items to the original datasource
146-
(misc.datasource as any).setProcessGet((
146+
misc.setDatasourceProcessor((
147147
result: IndexedItem[], _index: number, _count: number, _min: number, _max: number
148148
) =>
149149
insertItems(result, _index, _count, _min, _max, index + (before ? 0 : 1), amount, decrease)

tests/adapter.reload.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ const doReload = ({ custom }: TestBedConfig, { adapter }: Misc) => {
146146
};
147147

148148
const doReloadOnFirstDatasourceGetCall = (config: TestBedConfig, misc: Misc) => {
149-
const { datasource } = misc.fixture.componentInstance;
150149
let reloaded = false;
151-
(datasource as any).setProcessGet(() => {
150+
misc.setDatasourceProcessor(() => {
152151
if (!reloaded) {
153152
reloaded = true;
154153
doReload(config, misc);

tests/adapter.remove.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const doRemove = async (config: TestBedConfig, misc: Misc, byId = false) => {
121121
const indexList = remove || [...removeBwd, ...removeFwd];
122122
const indexListInterrupted = config.custom.interrupted;
123123
// remove item from the original datasource
124-
(misc.datasource as any).setProcessGet((result: IndexedItem[]) =>
124+
misc.setDatasourceProcessor((result: IndexedItem[]) =>
125125
[removeBwd, removeFwd, remove].forEach(list =>
126126
list && removeItems(result, list, -99, 100, increase)
127127
)

tests/bug.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ describe('Bug Spec', () => {
109109
itemSize: 20,
110110
inverse: true
111111
},
112-
templateSettings: { viewportHeight: 200, dynamicSize: 'size' }
112+
templateSettings: { viewportHeight: 300, dynamicSize: 'size' }
113113
},
114114
it: (misc: Misc) => async (done: Function) => {
115+
misc.setItemProcessor(({ $index, data }) => $index === 4 && (data.size = 120));
115116
await misc.relaxNext();
116117
const { paddings } = misc.scroller.viewport;
117118
expect(paddings.backward.size).toEqual(0);
@@ -152,7 +153,7 @@ describe('Bug Spec', () => {
152153
expect(misc.adapter.firstVisible.$index).toEqual(startIndex);
153154

154155
// remove item from the original datasource
155-
(misc.datasource as any).setProcessGet((result: IndexedItem[]) =>
156+
misc.setDatasourceProcessor((result: IndexedItem[]) =>
156157
removeItems(result, Array.from({ length: MAX - MIN + 1 }).map((j, i) => MIN + i), -99, 100)
157158
);
158159
await misc.adapter.remove({

tests/dynamic-height-scroll.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Stat } from './miscellaneous/stat';
44
import { appendItems, generateItems, IndexedItem } from './miscellaneous/items';
55

66
const configFetch: TestBedConfig = {
7-
datasourceName: 'limited-1-20-dynamic-size-special',
7+
datasourceName: 'limited-1-20-dynamic-size-processor',
88
datasourceSettings: {
99
startIndex: 1, padding: 0.5, bufferSize: 5, minIndex: 1, maxIndex: 20, itemSize: 20, adapter: true
1010
},
@@ -25,6 +25,7 @@ const configAppend: TestBedConfig = {
2525

2626
const testConfigFetch = (config: TestBedConfig, misc: Misc, done: Function) => {
2727
const { scroller, shared } = misc;
28+
misc.setItemProcessor(({ $index, data }) => $index === 1 && (data.size = 200));
2829
let initialFetchCount = 0;
2930
spyOn(misc.workflow, 'finalize').and.callFake(() => {
3031
const cycle = scroller.state.cycle.count;
@@ -48,7 +49,7 @@ const testConfigAppend = async (config: TestBedConfig, misc: Misc, done: Functio
4849
const { MAX, AMOUNT } = config.custom;
4950
await misc.relaxNext();
5051
// append items to the original datasource
51-
(misc.datasource as any).setProcessGet((
52+
misc.setDatasourceProcessor((
5253
result: IndexedItem[], _index: number, _count: number, _min: number, _max: number
5354
) =>
5455
appendItems(result, _index, _count, _min, _max, AMOUNT, true)

tests/dynamic-size.spec.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
getDynamicSizeData,
99
getDynamicSumSize
1010
} from './miscellaneous/dynamicSize';
11-
import { IndexedItem } from './miscellaneous/items';
1211

1312
const configList: TestBedConfig[] = [{
1413
datasourceName: 'limited--50-99-dynamic-size',
@@ -219,14 +218,16 @@ describe('Zero Size Spec', () => {
219218
makeTest({
220219
config: {
221220
...config,
222-
datasourceName: 'limited-1-100-zero-size-started-from-6'
221+
datasourceName: 'limited-1-100-processor'
223222
},
224223
title: 'should stop the Workflow after the second loop',
225-
it: (misc: Misc) => (done: Function) =>
224+
it: (misc: Misc) => (done: Function) => {
225+
misc.setItemProcessor(({ $index, data }) => data.size = $index >= 6 ? 0 : 20);
226226
spyOn(misc.workflow, 'finalize').and.callFake(() => {
227227
expect(misc.innerLoopCount).toEqual(2);
228228
done();
229-
})
229+
});
230+
}
230231
})
231232
);
232233

@@ -240,12 +241,10 @@ describe('Zero Size Spec', () => {
240241
title: 'should continue the Workflow after re-size and check',
241242
it: (misc: Misc) => (done: Function) =>
242243
spyOn(misc.workflow, 'finalize').and.callFake(() => {
243-
const { scroller: { viewport }, adapter, datasource } = misc;
244+
const { scroller: { viewport }, adapter } = misc;
244245
if (misc.workflow.cyclesDone === 1) {
245246
expect(viewport.getScrollableSize()).toEqual(viewport.paddings.forward.size);
246-
(datasource as any).setProcessGet((result: IndexedItem[]) =>
247-
result.forEach(({ data }) => data.size = 20)
248-
);
247+
misc.setItemProcessor(({ data }) => data.size = 20);
249248
adapter.fix({
250249
updater: ({ element, data }) => {
251250
data.size = 20;

tests/initial-load.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { makeTest, TestBedConfig } from './scaffolding/runner';
22
import { Misc } from './miscellaneous/misc';
3-
import { ItemsCounter, ItemsDirCounter, testItemsCounter } from './miscellaneous/itemsCounter';
3+
import { ItemsCounter, testItemsCounter } from './miscellaneous/itemsCounter';
44

55
const fixedItemSizeConfigList: TestBedConfig[] = [{
66
datasourceSettings: { startIndex: 1, padding: 2, itemSize: 15 },

tests/miscellaneous/dynamicSize.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const INITIAL_ITEM_SIZE = 20;
22
const MIN_ITEM_SIZE = 1;
33
const MAX_ITEM_SIZE = 100;
44

5+
export type DynamicSizeArg = boolean | number;
6+
57
export interface DynamicSizeData {
68
size: number;
79
average: number;

tests/miscellaneous/items.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDynamicSizeByIndex } from './dynamicSize';
1+
import { DynamicSizeArg, getDynamicSizeByIndex } from './dynamicSize';
22
import { getMin, getMax } from './common';
33

44
export interface Item {
@@ -12,9 +12,7 @@ export interface IndexedItem {
1212
data: Item;
1313
}
1414

15-
type DynamicSize = boolean | number;
16-
17-
const generateItemWithId = (id: number, index: number, dynamicSize?: DynamicSize, suffix = ''): Item => ({
15+
const generateItemWithId = (id: number, index: number, dynamicSize?: DynamicSizeArg, suffix = ''): Item => ({
1816
id,
1917
text: 'item #' + index + suffix,
2018
...(
@@ -28,7 +26,7 @@ const generateItemWithId = (id: number, index: number, dynamicSize?: DynamicSize
2826
)
2927
});
3028

31-
export const generateItem = (index: number, dynamicSize: DynamicSize = false, suffix = ''): Item =>
29+
export const generateItem = (index: number, dynamicSize: DynamicSizeArg = false, suffix = ''): Item =>
3230
generateItemWithId(index, index, dynamicSize, suffix);
3331

3432
export const generateItems = (length: number, lastIndex: number): Item[] =>
@@ -62,7 +60,7 @@ export const insertItems = (
6260
index: number,
6361
count: number,
6462
decrease: boolean,
65-
dynamicSize?: DynamicSize
63+
dynamicSize?: DynamicSizeArg
6664
) => {
6765
let i = 1;
6866
const items: IndexedItem[] = [];

tests/miscellaneous/misc.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { debounceTime, filter, take } from 'rxjs/operators';
55

66
import { TestComponentInterface } from '../scaffolding/testComponent';
77
import { TestBedConfig } from '../scaffolding/runner';
8-
import { generateItem } from './items';
8+
import { generateItem, IndexedItem } from './items';
99

1010
import { Direction, DatasourceGet, IAdapter } from '../../src/component/interfaces';
1111
import { UiScrollComponent } from '../../src/ui-scroll.component';
@@ -200,4 +200,17 @@ export class Misc {
200200
setTimeout(() => resolve(), ms)
201201
);
202202
}
203+
204+
setDatasourceProcessor(processor: Function) {
205+
const setProcessor = (this.datasource as any).setProcessGet;
206+
if (typeof processor === 'function') {
207+
setProcessor.call(this.datasource, processor);
208+
}
209+
}
210+
211+
setItemProcessor(itemUpdater: (item: IndexedItem) => any) {
212+
this.setDatasourceProcessor((result: IndexedItem[]) =>
213+
result.forEach(itemUpdater)
214+
);
215+
}
203216
}

tests/scaffolding/datasources/get.ts

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Observable, Observer } from 'rxjs';
22

33
import { generateItem, IndexedItem, Item } from '../../miscellaneous/items';
4+
import { DynamicSizeArg } from '../../miscellaneous/dynamicSize';
45

56
const datasourceGetInfinite = (index: number, count: number, suffix?: string) => {
67
const data = [];
@@ -11,7 +12,7 @@ const datasourceGetInfinite = (index: number, count: number, suffix?: string) =>
1112
};
1213

1314
export const getLimitedData = (
14-
index: number, count: number, min: number, max: number, dynamicSize: boolean | number, inverse: boolean, processor?: any
15+
index: number, count: number, min: number, max: number, dynamicSize: DynamicSizeArg, inverse: boolean, processor?: any
1516
): Item[] => {
1617
const result: IndexedItem[] = [];
1718
const start = inverse ? -index - count : index;
@@ -64,7 +65,7 @@ export const infiniteDatasourceGet = (type?: DatasourceType, delay?: number, suf
6465
};
6566

6667
export const limitedDatasourceGet = (
67-
min: number, max: number, dynamicSize: boolean, type: DatasourceType, delay: number, process?: boolean
68+
min: number, max: number, dynamicSize: DynamicSizeArg, type: DatasourceType, delay: number, process?: boolean
6869
) =>
6970
(index: number, count: number, success?: (data: any[]) => any, reject?: Function, processor?: () => any) => {
7071
switch (type) {
@@ -84,15 +85,3 @@ export const limitedDatasourceGet = (
8485
));
8586
}
8687
};
87-
88-
export const limitedDatasourceSpecialGet = (
89-
min: number, max: number, getSizeByIndex?: Function | number
90-
) => (
91-
index: number, count: number, success: Function, reject?: Function, processor?: Function
92-
) => {
93-
const dynamicSize = typeof getSizeByIndex === 'number' ? getSizeByIndex as number : false;
94-
if (typeof getSizeByIndex === 'function') {
95-
processor = (items: IndexedItem[]) => items.forEach(({ $index, data }) => data.size = getSizeByIndex($index));
96-
}
97-
success(getLimitedData(index, count, min, max, dynamicSize, false, processor));
98-
};

tests/scaffolding/datasources/store.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IDatasource } from '../../../src/component/interfaces';
2-
import { DatasourceType, infiniteDatasourceGet, limitedDatasourceGet, limitedDatasourceSpecialGet } from './get';
2+
import { DatasourceType, infiniteDatasourceGet, limitedDatasourceGet } from './get';
33

44
interface IDatasourceStore {
55
[key: string]: IDatasource;
@@ -81,20 +81,20 @@ export const datasourceStore: IDatasourceStore = {
8181
get: limitedDatasourceGet(-99, 100, true, DatasourceType.Callback, 0)
8282
},
8383

84-
'limited-1-20-dynamic-size-special': {
85-
get: limitedDatasourceSpecialGet(1, 20, (i: number) => i === 1 ? 200 : 20)
84+
'limited-1-20-dynamic-size-processor': {
85+
get: limitedDatasourceGet(1, 20, true, DatasourceType.Callback, 0, true)
8686
},
8787

8888
'limited-1-10-with-big-item-4': {
89-
get: limitedDatasourceSpecialGet(1, 10, (i: number) => i === 4 ? 93 : 20)
89+
get: limitedDatasourceGet(1, 10, false, DatasourceType.Callback, 0, true)
9090
},
9191

9292
'limited-1-100-zero-size': {
93-
get: limitedDatasourceSpecialGet(1, 100, 0)
93+
get: limitedDatasourceGet(1, 100, 0, DatasourceType.Callback, 0, true)
9494
},
9595

96-
'limited-1-100-zero-size-started-from-6': {
97-
get: limitedDatasourceSpecialGet(1, 100, (i: number) => i >= 6 ? 0 : 20)
96+
'limited-1-100-processor': {
97+
get: limitedDatasourceGet(1, 100, false, DatasourceType.Callback, 0, true)
9898
},
9999

100100
'limited--99-100-processor': {

tests/scroll-delay.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { Direction } from '../src/component/interfaces';
21
import { makeTest, TestBedConfig } from './scaffolding/runner';
3-
import { debounce } from './miscellaneous/debounce';
42
import { Misc } from './miscellaneous/misc';
53

64
const configThrottle: TestBedConfig = {

tests/validation.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { VALIDATORS, validateOne, validate } from '../src/component/inputs';
2-
import { ValidatorType, IValidator } from 'src/component/interfaces';
2+
import { IValidator } from 'src/component/interfaces';
33

44
const {
55
INTEGER,
@@ -317,10 +317,10 @@ describe('Validation', () => {
317317
true,
318318
'',
319319
() => null,
320-
function () {},
320+
function () { },
321321
[],
322322
null,
323-
class {},
323+
class { },
324324
new Map(),
325325
new Set(),
326326
Symbol(),

tests/viewport.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Direction } from '../src/component/interfaces';
21
import { makeTest, TestBedConfig } from './scaffolding/runner';
32
import { Misc } from './miscellaneous/misc';
43

0 commit comments

Comments
 (0)