Skip to content

Commit aed5af8

Browse files
committed
issue-171 spec datasources infrastructure refactoring
1 parent 5c38ee4 commit aed5af8

20 files changed

+425
-457
lines changed

src/component/inputs/adapter.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { VALIDATORS } from './validation';
2-
import { DatasourceProps } from './datasource';
2+
import { DatasourceProps as AdapterResetParams } from './datasource';
33
import { ICommonProps, AdapterProcessMap, AdapterProcess as Process } from '../interfaces/index';
44

55
const {
@@ -18,14 +18,14 @@ const {
1818
enum AdapterNoParams { }
1919
const NO_METHOD_PARAMS: ICommonProps<AdapterNoParams> = {};
2020

21-
const RESET_METHOD_PARAMS: ICommonProps<DatasourceProps> = {
22-
[DatasourceProps.get]: {
21+
const RESET_METHOD_PARAMS: ICommonProps<AdapterResetParams> = {
22+
[AdapterResetParams.get]: {
2323
validators: [FUNC_WITH_X_AND_MORE_ARGUMENTS(2)]
2424
},
25-
[DatasourceProps.settings]: {
25+
[AdapterResetParams.settings]: {
2626
validators: [OBJECT]
2727
},
28-
[DatasourceProps.devSettings]: {
28+
[AdapterResetParams.devSettings]: {
2929
validators: [OBJECT]
3030
},
3131
};
@@ -164,7 +164,7 @@ const FIX_METHOD_PARAMS: ICommonProps<AdapterFixParams> = {
164164
};
165165

166166
export const AdapterMethods: AdapterProcessMap<any> = {
167-
[Process.reset]: DatasourceProps,
167+
[Process.reset]: AdapterResetParams,
168168
[Process.reload]: AdapterReloadParams,
169169
[Process.append]: AdapterAppendParams,
170170
[Process.check]: AdapterNoParams,

tests/adapter.check.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { makeTest, TestBedConfig } from './scaffolding/runner';
22
import { Misc } from './miscellaneous/misc';
3+
import { IndexedItem } from './miscellaneous/items';
34

45
const MIN_INDEX = -99;
56
const MAX_INDEX = 100;
@@ -101,8 +102,8 @@ const updateDOM = (misc: Misc, { min, max, size, initialSize }: any) => {
101102
for (let i = min; i <= max; i++) {
102103
updateDOMElement(misc, i, size);
103104
// persist new sizes on the datasource level
104-
(datasource as any).setProcessGet((result: any[]) =>
105-
result.forEach(item => item.size = item.id >= min && item.id <= max ? size : initialSize)
105+
(datasource as any).setProcessGet((result: IndexedItem[]) =>
106+
result.forEach(({ data }) => data.size = data.id >= min && data.id <= max ? size : initialSize)
106107
);
107108
}
108109
};
@@ -128,8 +129,8 @@ const shouldCheck = (config: TestBedConfig) => (misc: Misc) => (done: Function)
128129
const { min, max, size } = config.custom;
129130
const changedCount = (max - min + 1);
130131
let firstVisibleIndex = NaN;
131-
(misc.datasource as any).setProcessGet((result: any[]) =>
132-
result.forEach(item => item.size = initialSize)
132+
(misc.datasource as any).setProcessGet((result: IndexedItem[]) =>
133+
result.forEach(({ data }) => data.size = initialSize)
133134
);
134135
spyOn(misc.workflow, 'finalize').and.callFake(() => {
135136
const cycle = state.cycle.count;

tests/adapter.insert.spec.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Direction } from '../src/component/interfaces';
21
import { makeTest, TestBedConfig } from './scaffolding/runner';
32
import { Misc } from './miscellaneous/misc';
4-
import { generateItems, Item, insertItems } from './miscellaneous/items';
3+
import { generateItems, insertItems, IndexedItem } from './miscellaneous/items';
54

65
describe('Adapter Insert Spec', () => {
76

@@ -25,28 +24,28 @@ describe('Adapter Insert Spec', () => {
2524
};
2625

2726
const configList: TestBedConfig[] = [{
28-
...configBase
27+
...configBase
2928
}, {
30-
...configBase,
31-
custom: {
32-
...configBase.custom,
33-
before: true
34-
}
35-
}, {
36-
...configBase,
37-
datasourceSettings: {
38-
...configBase.datasourceSettings,
39-
startIndex: MAX
40-
},
41-
custom: { before: false, index: MAX, amount: 3, desc: ' (append case)' }
42-
}, {
43-
...configBase,
44-
datasourceSettings: {
45-
...configBase.datasourceSettings,
46-
startIndex: MIN
47-
},
48-
custom: { before: true, index: 1, amount: 3, desc: ' (prepend case)' }
29+
...configBase,
30+
custom: {
31+
...configBase.custom,
32+
before: true
4933
}
34+
}, {
35+
...configBase,
36+
datasourceSettings: {
37+
...configBase.datasourceSettings,
38+
startIndex: MAX
39+
},
40+
custom: { before: false, index: MAX, amount: 3, desc: ' (append case)' }
41+
}, {
42+
...configBase,
43+
datasourceSettings: {
44+
...configBase.datasourceSettings,
45+
startIndex: MIN
46+
},
47+
custom: { before: true, index: 1, amount: 3, desc: ' (prepend case)' }
48+
}
5049
];
5150

5251
const configOutList: TestBedConfig[] = [{
@@ -136,7 +135,7 @@ describe('Adapter Insert Spec', () => {
136135

137136
const shouldCheckStaticProcess = (
138137
config: TestBedConfig, shouldInsert: boolean, callback?: Function
139-
) => (misc: Misc) => (done: any) => {
138+
) => (misc: Misc) => (done: any) => {
140139
const { adapter } = misc;
141140
const { before, decrease, amount, index } = config.custom;
142141
spyOn(misc.workflow, 'finalize').and.callFake(() => {
@@ -145,7 +144,7 @@ describe('Adapter Insert Spec', () => {
145144
}
146145
// insert items to the original datasource
147146
(misc.datasource as any).setProcessGet((
148-
result: any[], _index: number, _count: number, _min: number, _max: number
147+
result: IndexedItem[], _index: number, _count: number, _min: number, _max: number
149148
) =>
150149
insertItems(result, _index, _count, _min, _max, index + (before ? 0 : 1), amount, decrease)
151150
);

tests/adapter.reload.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ const doReload = ({ custom }: TestBedConfig, { adapter }: Misc) => {
148148
const doReloadOnFirstDatasourceGetCall = (config: TestBedConfig, misc: Misc) => {
149149
const { datasource } = misc.fixture.componentInstance;
150150
let reloaded = false;
151-
(datasource as any).setProcessGet((result: any[]) => {
151+
(datasource as any).setProcessGet(() => {
152152
if (!reloaded) {
153153
reloaded = true;
154154
doReload(config, misc);

tests/adapter.remove.spec.ts

Lines changed: 2 additions & 2 deletions
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 { removeItems } from './miscellaneous/items';
3+
import { IndexedItem, removeItems } from './miscellaneous/items';
44
import { AdapterProcess, ItemsPredicate } from '../src/component/interfaces/index';
55

66
const baseConfig: TestBedConfig = {
@@ -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: any[]) =>
124+
(misc.datasource as any).setProcessGet((result: IndexedItem[]) =>
125125
[removeBwd, removeFwd, remove].forEach(list =>
126126
list && removeItems(result, list, -99, 100, increase)
127127
)

tests/adapter.replace.spec.ts

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { makeTest, TestBedConfig } from './scaffolding/runner';
2-
import { generateItem, Item } from './miscellaneous/items';
2+
import { generateItem } from './miscellaneous/items';
33
import { Misc } from './miscellaneous/misc';
4-
import { Settings, DevSettings, DatasourceGet } from '../src/component/interfaces/index';
4+
import { getDatasourceReplacementsClass } from './scaffolding/datasources/class';
55

66
const baseSettings = {
77
startIndex: 1,
@@ -29,70 +29,24 @@ const configList: TestBedConfig[] = [{
2929
indexToReplace: baseSettings.maxIndex,
3030
token: 'last'
3131
}
32-
}];
33-
34-
const getDatasourceClass = (settings: Settings) =>
35-
class {
36-
private data: Item[] = [];
37-
38-
settings: Settings;
39-
devSettings: DevSettings;
40-
get: DatasourceGet;
41-
42-
constructor() {
43-
const min = settings.minIndex || 0;
44-
const max = settings.maxIndex || 0;
45-
46-
for (let i = min; i < min + max; ++i) {
47-
this.data.push(generateItem(i));
48-
}
49-
50-
this.settings = { ...settings };
51-
// this.devSettings = { debug: true, logProcessRun: true };
52-
53-
this.get = (index: number, count: number, success: Function) => {
54-
const data = [];
55-
const start = index;
56-
const end = start + count - 1;
57-
if (start <= end) {
58-
for (let i = start; i <= end; i++) {
59-
const item = this.data.find(({ id }) => id === i);
60-
if (!item) {
61-
continue;
62-
}
63-
data.push(item);
64-
}
65-
}
66-
success(data);
67-
};
68-
}
69-
70-
replaceOne(idToReplace: number, item: Item) {
71-
const itemToReplace = this.data.find(({ id }) => id === idToReplace);
72-
if (itemToReplace) {
73-
Object.assign(itemToReplace, item);
74-
}
75-
}
76-
};
77-
78-
configList.forEach(config => config.datasourceClass = getDatasourceClass(config.datasourceSettings));
32+
}].map(config => ({ ...config, datasourceClass: getDatasourceReplacementsClass(config.datasourceSettings) }));
7933

8034
const shouldReplace = (config: TestBedConfig) => (misc: Misc) => async (done: Function) => {
8135
await misc.relaxNext();
8236
const { adapter } = misc;
83-
const { custom: { indexToReplace, token }, datasourceSettings: { minIndex, itemSize } } = config;
84-
const replaceOne = (misc.datasource as any).replaceOne.bind(misc.datasource);
37+
const { custom: { indexToReplace: index, token } } = config;
38+
const { datasourceSettings: { minIndex, itemSize } } = config;
8539
const maxScrollPosition = misc.getMaxScrollPosition();
86-
const position = token === 'last' ? maxScrollPosition : (indexToReplace - 1 + minIndex - 1) * itemSize;
87-
const newItem = generateItem(indexToReplace);
40+
const position = token === 'last' ? maxScrollPosition : (index - 1 + minIndex - 1) * itemSize;
41+
const newItem = generateItem(index);
8842
newItem.text += '*';
8943

9044
// replace at the Datasource level
91-
replaceOne(indexToReplace, newItem);
45+
(misc.datasource as any).replaceOne(index, newItem);
9246

9347
// replace at the Viewport level (Adapter)
9448
await adapter.replace({
95-
predicate: ({ $index }) => $index === indexToReplace,
49+
predicate: ({ $index }) => [index].includes($index),
9650
items: [newItem]
9751
});
9852

@@ -105,11 +59,11 @@ const shouldReplace = (config: TestBedConfig) => (misc: Misc) => async (done: Fu
10559
}
10660

10761
if (token === 'last') {
108-
expect(adapter.lastVisible.$index).toEqual(indexToReplace);
62+
expect(adapter.lastVisible.$index).toEqual(index);
10963
} else {
110-
expect(adapter.firstVisible.$index).toEqual(indexToReplace);
64+
expect(adapter.firstVisible.$index).toEqual(index);
11165
}
112-
expect(misc.getElementText(indexToReplace)).toEqual(indexToReplace + ': ' + newItem.text);
66+
expect(misc.getElementText(index)).toEqual(index + ': ' + newItem.text);
11367
done();
11468
};
11569

tests/adapter.reset-persistence.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ADAPTER_PROPS } from '../src/component/classes/adapter/props';
55
import { IDatasourceOptional, IAdapter, IDatasource } from '../src/component/interfaces';
66

77
import { makeTest, TestBedConfig } from './scaffolding/runner';
8-
import { datasourceStore } from './scaffolding/datasources';
8+
import { datasourceStore } from './scaffolding/datasources/store';
99
import { Misc } from './miscellaneous/misc';
1010

1111
const ADAPTER_PROPS_STUB = ADAPTER_PROPS(null);

tests/adapter.reset.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AdapterProcess, IDatasourceOptional, Direction } from '../src/component/interfaces';
22
import { makeTest, TestBedConfig } from './scaffolding/runner';
3-
import { datasourceStore } from './scaffolding/datasources';
3+
import { datasourceStore } from './scaffolding/datasources/store';
44
import { Misc } from './miscellaneous/misc';
55
import { generateItem } from './miscellaneous/items';
66

tests/bug.spec.ts

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { makeTest } from './scaffolding/runner';
2-
import { generateItem, Item, removeItems } from './miscellaneous/items';
2+
import { IndexedItem, removeItems } from './miscellaneous/items';
33
import { Misc } from './miscellaneous/misc';
44
import { configureTestBedSub } from './scaffolding/testBed';
5-
import { Settings, DevSettings, DatasourceGet } from '../src/component/interfaces/index';
65

76
describe('Bug Spec', () => {
87

@@ -102,60 +101,22 @@ describe('Bug Spec', () => {
102101
makeTest({
103102
title: 'should not extend forward padding element',
104103
config: {
105-
datasourceClass: class {
106-
private messages: Item[] = [];
107-
private MIN = 0;
108-
private START = 0;
109-
private COUNT = 7;
110-
111-
settings: Settings;
112-
devSettings: DevSettings;
113-
get: DatasourceGet;
114-
115-
constructor() {
116-
const inverse = true;
117-
118-
for (let i = this.MIN; i < this.MIN + this.COUNT; ++i) {
119-
const item = generateItem(i);
120-
if (i === 4) {
121-
item.size = 93;
122-
}
123-
this.messages.push(item);
124-
}
125-
126-
this.settings = {
127-
startIndex: inverse ? this.MIN - this.START - 1 : this.START,
128-
bufferSize: this.COUNT,
129-
adapter: true,
130-
itemSize: 20,
131-
inverse
132-
};
133-
134-
this.get = (index: number, count: number, success: Function) => {
135-
const data = [];
136-
const start = inverse ? -index - count + this.MIN : index;
137-
const end = start + count - 1;
138-
if (start <= end) {
139-
for (let i = start; i <= end; i++) {
140-
if (!this.messages[i]) {
141-
continue;
142-
}
143-
data.push(this.messages[i]);
144-
}
145-
}
146-
success(inverse ? data.reverse() : data);
147-
};
148-
}
104+
datasourceName: 'limited-1-10-with-big-item-4',
105+
datasourceSettings: {
106+
startIndex: -1,
107+
bufferSize: 7,
108+
adapter: true,
109+
itemSize: 20,
110+
inverse: true
149111
},
150112
templateSettings: { viewportHeight: 200, dynamicSize: 'size' }
151113
},
152-
it: (misc: Misc) => (done: Function) => {
153-
spyOn(misc.workflow, 'finalize').and.callFake(() => {
154-
const { scroller: { viewport } } = misc;
155-
expect(viewport.paddings.backward.size).toEqual(0);
156-
expect(viewport.paddings.forward.size).toEqual(0);
157-
done();
158-
});
114+
it: (misc: Misc) => async (done: Function) => {
115+
await misc.relaxNext();
116+
const { paddings } = misc.scroller.viewport;
117+
expect(paddings.backward.size).toEqual(0);
118+
expect(paddings.forward.size).toEqual(0);
119+
done();
159120
}
160121
})
161122
);
@@ -167,13 +128,12 @@ describe('Bug Spec', () => {
167128
misc = new Misc(configureTestBedSub());
168129
});
169130

170-
it('should work', (done) => {
171-
const { adapter, workflow, testComponent } = misc;
172-
spyOn(workflow, 'finalize').and.callFake(() => {
173-
expect(testComponent.firstVisible).toEqual(adapter.firstVisible);
174-
expect(testComponent.firstVisible.$index).toEqual(1);
175-
done();
176-
});
131+
it('should work', async (done) => {
132+
const { adapter, testComponent } = misc;
133+
await misc.relaxNext();
134+
expect(testComponent.firstVisible).toEqual(adapter.firstVisible);
135+
expect(testComponent.firstVisible.$index).toEqual(1);
136+
done();
177137
});
178138
});
179139

@@ -192,7 +152,7 @@ describe('Bug Spec', () => {
192152
expect(misc.adapter.firstVisible.$index).toEqual(startIndex);
193153

194154
// remove item from the original datasource
195-
(misc.datasource as any).setProcessGet((result: any[]) =>
155+
(misc.datasource as any).setProcessGet((result: IndexedItem[]) =>
196156
removeItems(result, Array.from({ length: MAX - MIN + 1 }).map((j, i) => MIN + i), -99, 100)
197157
);
198158
await misc.adapter.remove({

0 commit comments

Comments
 (0)