Newer
Older
import { DropListMock, DropLogic } from 'common/components/input-elements/drop-list/drop-logic';
import { DragNDropValueObject } from 'common/models/elements/label-interfaces';
describe('DropLogic', () => {
const dragItemPreset: DragNDropValueObject = {
id: 'testID',
text: 'bla',
originListID: 'droplist_1',
originListIndex: 0,
imgSrc: null,
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
imgPosition: 'above'
};
let allListsPreset: { [id: string]: DropListMock } = {
droplist_1: {
id: 'droplist_1',
value: [dragItemPreset],
isSortList: false,
onlyOneItem: false,
connectedTo: ['droplist_2'],
copyOnDrop: false,
allowReplacement: false
},
droplist_2: {
id: 'droplist_2',
value: [],
isSortList: false,
onlyOneItem: false,
connectedTo: [],
copyOnDrop: false,
allowReplacement: false
}
};
beforeEach(() => {
allListsPreset = {
droplist_1: {
id: 'droplist_1',
value: [dragItemPreset],
isSortList: false,
onlyOneItem: false,
connectedTo: ['droplist_2'],
copyOnDrop: false,
allowReplacement: false
},
droplist_2: {
id: 'droplist_2',
value: [],
isSortList: false,
onlyOneItem: false,
connectedTo: [],
copyOnDrop: false,
allowReplacement: false
}
};
});
it('fail if dropping into startlist', () => {
expect(DropLogic.isDropAllowed(dragItemPreset, 'droplist_1', 'droplist_1', allListsPreset))
.toBe(false);
});
it('allow dropping into startlist if it is a sortlist', () => {
const allLists = { ...allListsPreset };
allLists.droplist_1.isSortList = true;
expect(DropLogic.isDropAllowed(dragItemPreset, 'droplist_1', 'droplist_1', allLists))
.toBe(true);
});
it('fail if dropping into NOT connected list', () => {
expect(DropLogic.isDropAllowed(dragItemPreset, 'droplist_2', 'droplist_1', allListsPreset))
.toBe(false);
});
it('pass if dropping into another connected list', () => {
expect(DropLogic.isDropAllowed(dragItemPreset, 'droplist_1', 'droplist_2', allListsPreset))
.toBe(true);
});
// ### Put back (Copy-lists) ###
it('pass if putting a copied item back to its origin', () => {
const draggedItem = { ...dragItemPreset, originListID: 'droplist_1' };
const allLists = { ...allListsPreset };
allLists.droplist_1.copyOnDrop = true;
expect(DropLogic.isPutBack(draggedItem, allLists.droplist_1)).toBe(true);
});
it('fail if moving a foreign item to a copy-list', () => {
const draggedItem = { ...dragItemPreset, originListID: 'droplist_2' };
const allLists = { ...allListsPreset };
allLists.droplist_1.copyOnDrop = true;
expect(DropLogic.isPutBack(draggedItem, allLists.droplist_1)).toBe(false);
});
// ### Only One Item ###
it('pass if putting an item to an empty only-one list', () => {
const allLists = { ...allListsPreset };
allLists.droplist_2.onlyOneItem = true;
expect(DropLogic.isDropAllowed(dragItemPreset, 'droplist_1', 'droplist_2', allLists))
.toBe(true);
});
it('fail if putting an item to a only-one list which contains an item', () => {
// const targetList = { ...testList2, onlyOneItem: true, value: [dragItemPreset] };
const allLists = { ...allListsPreset };
allLists.droplist_2.onlyOneItem = true;
allLists.droplist_2.value = [dragItemPreset];
expect(DropLogic.isDropAllowed(dragItemPreset, 'droplist_1', 'droplist_2', allLists))
.toBe(false);
});
it('pass if putting an item to an only-one list, where item is replaceable', () => {
const allLists = { ...allListsPreset };
allLists.droplist_2.onlyOneItem = true;
allLists.droplist_2.allowReplacement = true;
allLists.droplist_2.value = [dragItemPreset];
expect(DropLogic.isDropAllowed(dragItemPreset, 'droplist_1', 'droplist_2', allLists))
.toBe(true);
});
it('fail if putting an item to an only-one list, where item is replaceable, but already in its origin', () => {
const allLists = { ...allListsPreset };
allLists.droplist_2.onlyOneItem = true;
allLists.droplist_2.allowReplacement = true;
allLists.droplist_2.value = [{ ...dragItemPreset, originListID: 'droplist_2' }];
expect(DropLogic.isDropAllowed(dragItemPreset, 'droplist_1', 'droplist_2', allLists))
.toBe(false);
});
it('doesnt enter endless loop when replacing items', () => {
const dragItem = { ...dragItemPreset, id: 'testID3', originListID: 'droplist_3' };
const list1 = {
id: 'droplist_1',
connectedTo: ['droplist_2', 'droplist_3'],
onlyOneItem: true,
allowReplacement: true,
value: [{ ...dragItemPreset, id: 'testID2', originListID: 'droplist_2' }],
isSortList: false,
copyOnDrop: false
};
const list2 = {
id: 'droplist_2',
connectedTo: ['droplist_1', 'droplist_3'],
onlyOneItem: true,
allowReplacement: true,
value: [{ ...dragItemPreset, id: 'testID1', originListID: 'droplist_1' }],
isSortList: false,
copyOnDrop: false
};
const list3 = {
id: 'droplist_3',
connectedTo: ['droplist_1', 'droplist_2'],
onlyOneItem: true,
allowReplacement: true,
value: [dragItem],
isSortList: false,
copyOnDrop: false
};
const allLists: { [id: string]: DropListMock } = {
droplist_1: list1,
droplist_2: list2,
droplist_3: list3
};
expect(DropLogic.isDropAllowed(dragItem, 'droplist_3', 'droplist_2', allLists)).toBe(false);
});
});