Skip to content

Commit 853e78d

Browse files
committed
Adapter.clip method settings spec
1 parent 4468f21 commit 853e78d

File tree

1 file changed

+59
-11
lines changed

1 file changed

+59
-11
lines changed

tests/adapter.clip.spec.ts

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import { makeTest, TestBedConfig } from './scaffolding/runner';
22
import { Misc } from './miscellaneous/misc';
33
import { testItemsCounter, ItemsCounter } from './miscellaneous/itemsCounter';
4+
import { ClipOptions } from 'src/component/interfaces';
45

56
const configList: TestBedConfig[] = [{
6-
datasourceSettings: { startIndex: 100, bufferSize: 4, padding: 0.22, itemSize: 20 },
7-
templateSettings: { viewportHeight: 71, itemHeight: 20 }
8-
}, {
97
datasourceSettings: { startIndex: 1, bufferSize: 5, padding: 0.2, itemSize: 20 },
108
templateSettings: { viewportHeight: 100 }
119
}, {
12-
datasourceSettings: { startIndex: -15, bufferSize: 12, padding: 0.98, itemSize: 20 },
13-
templateSettings: { viewportHeight: 66, itemHeight: 20 }
10+
datasourceSettings: { startIndex: -158, bufferSize: 11, padding: 0.68, itemSize: 20 },
11+
templateSettings: { viewportHeight: 77, itemHeight: 20 }
1412
}, {
1513
datasourceSettings: { startIndex: 1, bufferSize: 5, padding: 1, horizontal: true, itemSize: 90 },
1614
templateSettings: { viewportWidth: 450, itemWidth: 90, horizontal: true }
1715
}, {
18-
datasourceSettings: { startIndex: -74, bufferSize: 4, padding: 0.72, horizontal: true, itemSize: 75 },
19-
templateSettings: { viewportWidth: 300, itemWidth: 75, horizontal: true }
16+
datasourceSettings: { startIndex: -274, bufferSize: 3, padding: 1.22, horizontal: true, itemSize: 75 },
17+
templateSettings: { viewportWidth: 320, itemWidth: 75, horizontal: true }
2018
}];
2119

20+
const configByDirectionList = configList.map((config: TestBedConfig, index: number) => ({
21+
...config,
22+
custom: { forward: index % 2 === 0, backward: index % 2 !== 0 }
23+
}));
24+
2225
configList.forEach(config => config.datasourceSettings.adapter = true);
2326

2427
export const getItemsCounter = (
25-
settings: TestBedConfig, misc: Misc, itemSize: number, firstIndex: number, lastIndex: number
28+
settings: TestBedConfig, misc: Misc, itemSize: number, firstIndex: number, lastIndex: number, clipOptions: ClipOptions
2629
): ItemsCounter => {
2730
const { startIndex, padding } = misc.scroller.settings;
2831
const viewportSize = misc.getViewportSize(settings);
@@ -40,7 +43,16 @@ export const getItemsCounter = (
4043

4144
itemsCounter.backward.padding = (backward.index - firstIndex) * itemSize;
4245
itemsCounter.forward.padding = (lastIndex - forward.index) * itemSize;
43-
46+
47+
if (clipOptions) {
48+
if (clipOptions.forwardOnly) {
49+
backward.padding = 0;
50+
backward.index = firstIndex;
51+
} else {
52+
forward.padding = 0;
53+
forward.index = lastIndex;
54+
}
55+
}
4456
return itemsCounter;
4557
};
4658

@@ -49,6 +61,7 @@ const shouldClipAfterAppend = (config: TestBedConfig) => (misc: Misc) => (done:
4961
const NEW_ITEMS_COUNT = 50;
5062
const { itemSize } = config.datasourceSettings;
5163
let firstIndex: number, lastIndex: number;
64+
const clipSettings = getClipArgument(config);
5265

5366
spyOn(misc.workflow, 'finalize').and.callFake(() => {
5467
const cycles = misc.workflow.cyclesDone;
@@ -64,18 +77,45 @@ const shouldClipAfterAppend = (config: TestBedConfig) => (misc: Misc) => (done:
6477
lastIndex = <number>misc.scroller.buffer.lastIndex;
6578
expect(lastIndex).toEqual(indexToAppend + NEW_ITEMS_COUNT - 1);
6679
expect(misc.padding.backward.getSize()).toEqual(0);
67-
misc.datasource.adapter.clip();
80+
misc.datasource.adapter.clip(clipSettings);
6881
} else {
6982
// user clip requires additional reflow to remove DOM elements
7083
setTimeout(() => {
71-
const itemsCounter = getItemsCounter(config, misc, itemSize, firstIndex, lastIndex);
84+
const itemsCounter = getItemsCounter(config, misc, itemSize, firstIndex, lastIndex, clipSettings);
7285
testItemsCounter(config, misc, itemsCounter);
7386
done();
7487
});
7588
}
7689
});
7790
};
7891

92+
const getClipArgument = ({ custom }: TestBedConfig): any => {
93+
let argument;
94+
if (custom && custom.forward) {
95+
argument = { forwardOnly: true };
96+
}
97+
if (custom && custom.backward) {
98+
argument = { backwardOnly: true };
99+
}
100+
return argument;
101+
};
102+
103+
const getClipDirection = (config: TestBedConfig): string => {
104+
if (!config.custom) {
105+
return '';
106+
}
107+
if (config.custom.forward && config.custom.backward) {
108+
return 'forward and backward';
109+
}
110+
if (config.custom.forward) {
111+
return 'forward';
112+
}
113+
if (config.custom.backward) {
114+
return 'backward';
115+
}
116+
return '';
117+
}
118+
79119
describe('Adapter Clip Spec', () => {
80120

81121
configList.forEach(config =>
@@ -86,5 +126,13 @@ describe('Adapter Clip Spec', () => {
86126
})
87127
);
88128

129+
configByDirectionList.forEach(config =>
130+
makeTest({
131+
config,
132+
title: `should clip ${getClipDirection(config)} after append many`,
133+
it: shouldClipAfterAppend(config)
134+
})
135+
);
136+
89137
});
90138

0 commit comments

Comments
 (0)