Skip to content

Commit ced0c87

Browse files
committed
issue-171 many-to-one replacement spec
1 parent 35a58f1 commit ced0c87

File tree

2 files changed

+97
-13
lines changed

2 files changed

+97
-13
lines changed

tests/adapter.replace.spec.ts

+76-12
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,69 @@ const configList: TestBedConfig[] = [{
2929
indexToReplace: baseSettings.maxIndex,
3030
token: 'last'
3131
}
32-
}].map(config => ({ ...config, datasourceClass: getDatasourceReplacementsClass(config.datasourceSettings) }));
32+
}].map(config => ({
33+
...config,
34+
datasourceClass: getDatasourceReplacementsClass(config.datasourceSettings)
35+
}));
36+
37+
const someToOneConfigList: TestBedConfig[] = [{
38+
datasourceSettings: { ...baseSettings },
39+
custom: {
40+
indexesToReplace: [
41+
baseSettings.minIndex + 1,
42+
baseSettings.minIndex + 2,
43+
baseSettings.minIndex + 3
44+
],
45+
token: 'middle'
46+
}
47+
}, {
48+
datasourceSettings: { ...baseSettings },
49+
custom: {
50+
indexesToReplace: [
51+
baseSettings.minIndex,
52+
baseSettings.minIndex + 1,
53+
baseSettings.minIndex + 2
54+
],
55+
token: 'first'
56+
}
57+
}, {
58+
datasourceSettings: { ...baseSettings, startIndex: baseSettings.maxIndex },
59+
custom: {
60+
indexesToReplace: [
61+
baseSettings.maxIndex - 2,
62+
baseSettings.maxIndex - 1,
63+
baseSettings.maxIndex
64+
],
65+
token: 'last'
66+
}
67+
}].map(config => ({
68+
...config,
69+
datasourceClass: getDatasourceReplacementsClass(config.datasourceSettings)
70+
}));
71+
3372

3473
const shouldReplace = (config: TestBedConfig) => (misc: Misc) => async (done: Function) => {
3574
await misc.relaxNext();
3675
const { adapter } = misc;
37-
const { custom: { indexToReplace: index, token } } = config;
76+
const { custom: { indexToReplace: index, indexesToReplace: indexes, token } } = config;
3877
const { datasourceSettings: { minIndex, itemSize } } = config;
39-
const maxScrollPosition = misc.getMaxScrollPosition();
40-
const position = token === 'last' ? maxScrollPosition : (index - 1 + minIndex - 1) * itemSize;
41-
const newItem = generateItem(index);
78+
const diff = indexes ? indexes.length : 1;
79+
const maxScrollPosition = misc.getMaxScrollPosition() - (diff - 1) * misc.getItemSize();
80+
const newIndex = indexes ? indexes[0] : index;
81+
const position = token === 'last' ? maxScrollPosition : (newIndex - 1 + minIndex - 1) * itemSize;
82+
const newItem = generateItem(newIndex);
4283
newItem.text += '*';
4384

44-
// replace at the Datasource level
45-
(misc.datasource as any).replaceOne(index, newItem);
85+
// replace at the Datasource level (component)
86+
if (index) {
87+
(misc.datasource as any).replaceOneToOne(index, newItem);
88+
} else if (indexes) {
89+
(misc.datasource as any).replaceManyToOne(indexes, newItem);
90+
}
4691

47-
// replace at the Viewport level (Adapter)
92+
// replace at the Viewport level (scroller)
4893
await adapter.replace({
49-
predicate: ({ $index }) => [index].includes($index),
94+
predicate: ({ $index }) => (indexes || [index]).includes($index),
5095
items: [newItem]
5196
});
5297

@@ -58,12 +103,21 @@ const shouldReplace = (config: TestBedConfig) => (misc: Misc) => async (done: Fu
58103
await misc.relaxNext();
59104
}
60105

106+
// check replaced item
107+
if (token === 'last') {
108+
expect(adapter.lastVisible.$index).toEqual(newIndex);
109+
} else {
110+
expect(adapter.firstVisible.$index).toEqual(newIndex);
111+
}
112+
expect(misc.getElementText(newIndex)).toEqual(newIndex + ': ' + newItem.text);
113+
114+
// check the next item
61115
if (token === 'last') {
62-
expect(adapter.lastVisible.$index).toEqual(index);
116+
expect(misc.checkElementContent(newIndex - 1, newIndex - 1)).toEqual(true);
63117
} else {
64-
expect(adapter.firstVisible.$index).toEqual(index);
118+
expect(misc.checkElementContent(newIndex + 1, newIndex + diff)).toEqual(true);
65119
}
66-
expect(misc.getElementText(index)).toEqual(index + ': ' + newItem.text);
120+
67121
done();
68122
};
69123

@@ -79,4 +133,14 @@ describe('Adapter Replace Spec', () => {
79133
)
80134
);
81135

136+
describe('some-to-one replacement', () =>
137+
someToOneConfigList.forEach(config =>
138+
makeTest({
139+
title: `should work (${config.custom.token})`,
140+
config,
141+
it: shouldReplace(config)
142+
})
143+
)
144+
);
145+
82146
});

tests/scaffolding/datasources/class.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,30 @@ export const getDatasourceReplacementsClass = (settings: Settings) =>
6868
};
6969
}
7070

71-
replaceOne(idToReplace: number, item: Item) {
71+
replaceOneToOne(idToReplace: number, item: Item) {
7272
const itemToReplace = this.data.find(({ id }) => id === idToReplace);
7373
if (itemToReplace) {
7474
Object.assign(itemToReplace, item);
7575
}
7676
}
77+
78+
replaceManyToOne(idsToReplace: number[], item: Item) {
79+
idsToReplace.sort((a, b) => a - b);
80+
const minId = idsToReplace[0];
81+
const maxId = idsToReplace.slice(1).reduce((acc, id) =>
82+
// only continuous series allowed
83+
id === acc + 1 ? id : acc, minId
84+
);
85+
const diff = maxId - minId;
86+
this.data = this.data.reduce((acc, _item: Item) => {
87+
if (_item.id < minId) {
88+
acc.push(_item);
89+
} else if (_item.id === minId) {
90+
acc.push(item);
91+
} else if (_item.id > maxId) {
92+
acc.push({ ..._item, id: _item.id - diff });
93+
}
94+
return acc;
95+
}, [] as Item[]);
96+
}
7797
};

0 commit comments

Comments
 (0)