|
| 1 | +import { makeTest, TestBedConfig } from './scaffolding/runner'; |
| 2 | +import { Misc } from './miscellaneous/misc'; |
| 3 | +import { testItemsCounter, ItemsCounter } from './miscellaneous/itemsCounter'; |
| 4 | + |
| 5 | +const configList: TestBedConfig[] = [{ |
| 6 | + datasourceSettings: { startIndex: 100, bufferSize: 4, padding: 0.22, itemSize: 20 }, |
| 7 | + templateSettings: { viewportHeight: 71, itemHeight: 20 } |
| 8 | +}, { |
| 9 | + datasourceSettings: { startIndex: 1, bufferSize: 5, padding: 0.2, itemSize: 20 }, |
| 10 | + templateSettings: { viewportHeight: 100 } |
| 11 | +}, { |
| 12 | + datasourceSettings: { startIndex: -15, bufferSize: 12, padding: 0.98, itemSize: 20 }, |
| 13 | + templateSettings: { viewportHeight: 66, itemHeight: 20 } |
| 14 | +}, { |
| 15 | + datasourceSettings: { startIndex: 1, bufferSize: 5, padding: 1, horizontal: true, itemSize: 90 }, |
| 16 | + templateSettings: { viewportWidth: 450, itemWidth: 90, horizontal: true } |
| 17 | +}, { |
| 18 | + datasourceSettings: { startIndex: -74, bufferSize: 4, padding: 0.72, horizontal: true, itemSize: 75 }, |
| 19 | + templateSettings: { viewportWidth: 300, itemWidth: 75, horizontal: true } |
| 20 | +}]; |
| 21 | + |
| 22 | +configList.forEach(config => config.datasourceSettings.adapter = true); |
| 23 | + |
| 24 | +export const getItemsCounter = ( |
| 25 | + settings: TestBedConfig, misc: Misc, itemSize: number, firstIndex: number, lastIndex: number |
| 26 | +): ItemsCounter => { |
| 27 | + const { startIndex, padding } = misc.scroller.settings; |
| 28 | + const viewportSize = misc.getViewportSize(settings); |
| 29 | + |
| 30 | + const backwardLimit = viewportSize * padding; |
| 31 | + const forwardLimit = viewportSize + backwardLimit; |
| 32 | + const itemsCounter = new ItemsCounter(); |
| 33 | + const { backward, forward } = itemsCounter; |
| 34 | + |
| 35 | + backward.count = Math.ceil(backwardLimit / itemSize); |
| 36 | + forward.count = Math.ceil(forwardLimit / itemSize); |
| 37 | + |
| 38 | + backward.index = startIndex - backward.count; |
| 39 | + forward.index = startIndex + forward.count - 1; |
| 40 | + |
| 41 | + itemsCounter.backward.padding = (backward.index - firstIndex) * itemSize; |
| 42 | + itemsCounter.forward.padding = (lastIndex - forward.index) * itemSize; |
| 43 | + |
| 44 | + return itemsCounter; |
| 45 | +}; |
| 46 | + |
| 47 | +const shouldClipAfterAppend = (config: TestBedConfig) => (misc: Misc) => (done: Function) => { |
| 48 | + let indexToAppend = -Infinity; |
| 49 | + const NEW_ITEMS_COUNT = 50; |
| 50 | + const { itemSize } = config.datasourceSettings; |
| 51 | + let firstIndex: number, lastIndex: number; |
| 52 | + |
| 53 | + spyOn(misc.workflow, 'finalize').and.callFake(() => { |
| 54 | + const cycles = misc.workflow.cyclesDone; |
| 55 | + if (cycles === 1) { |
| 56 | + indexToAppend = misc.scroller.buffer.getIndexToAppend(); |
| 57 | + const itemsToAppend = Array.from(Array(NEW_ITEMS_COUNT), (_, x) => ({ |
| 58 | + id: indexToAppend - x, |
| 59 | + text: `!item #${indexToAppend - x}` |
| 60 | + })); |
| 61 | + misc.datasource.adapter.append(itemsToAppend); |
| 62 | + } else if (cycles === 2) { |
| 63 | + firstIndex = <number>misc.scroller.buffer.firstIndex; |
| 64 | + lastIndex = <number>misc.scroller.buffer.lastIndex; |
| 65 | + expect(lastIndex).toEqual(indexToAppend + NEW_ITEMS_COUNT - 1); |
| 66 | + expect(misc.padding.backward.getSize()).toEqual(0); |
| 67 | + misc.datasource.adapter.clip(); |
| 68 | + } else { |
| 69 | + // user clip requires additional reflow to remove DOM elements |
| 70 | + setTimeout(() => { |
| 71 | + const itemsCounter = getItemsCounter(config, misc, itemSize, firstIndex, lastIndex); |
| 72 | + testItemsCounter(config, misc, itemsCounter); |
| 73 | + done(); |
| 74 | + }); |
| 75 | + } |
| 76 | + }); |
| 77 | +}; |
| 78 | + |
| 79 | +describe('Adapter Clip Spec', () => { |
| 80 | + |
| 81 | + configList.forEach(config => |
| 82 | + makeTest({ |
| 83 | + config, |
| 84 | + title: 'should clip after append many', |
| 85 | + it: shouldClipAfterAppend(config) |
| 86 | + }) |
| 87 | + ); |
| 88 | + |
| 89 | +}); |
| 90 | + |
0 commit comments