|
| 1 | +import { setup } from '../../setup.mjs'; |
| 2 | + |
| 3 | +const appName = 'CollectionBaseTest'; |
| 4 | + |
| 5 | +setup({ |
| 6 | + appConfig: { |
| 7 | + name: appName |
| 8 | + } |
| 9 | +}); |
| 10 | + |
| 11 | +import { test, expect } from '@playwright/test'; |
| 12 | + |
| 13 | +import Neo from '../../../../src/Neo.mjs'; |
| 14 | +import * as core from '../../../../src/core/_export.mjs'; |
| 15 | +import Collection from '../../../../src/collection/Base.mjs'; |
| 16 | +import InstanceManager from '../../../../src/manager/Instance.mjs'; |
| 17 | + |
| 18 | +/** |
| 19 | + * @summary Tests for Neo.collection.Base |
| 20 | + * This suite tests the fundamental CRUD (Create, Read, Update, Delete) operations, |
| 21 | + * as well as sorting, filtering, and cloning functionalities of the base Collection class. |
| 22 | + * Ensuring the stability of Neo.collection.Base is critical as it is the foundation |
| 23 | + * for all data stores and collections within the framework. |
| 24 | + */ |
| 25 | +test.describe.serial('Neo.collection.Base', () => { |
| 26 | + let collection, collection2, collection3; |
| 27 | + |
| 28 | + test('Create collection', () => { |
| 29 | + collection = Neo.create(Collection, { |
| 30 | + keyProperty: 'githubId', |
| 31 | + items: [ |
| 32 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'}, |
| 33 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 34 | + {country: 'USA', firstname: 'Rich', githubId: 'rwaters', lastname: 'Waters'}, |
| 35 | + {country: 'Germany', firstname: 'Nils', githubId: 'mrsunshine', lastname: 'Dehl'}, |
| 36 | + {country: 'USA', firstname: 'Gerard', githubId: 'camtnbikerrwc', lastname: 'Horan'}, |
| 37 | + {country: 'Slovakia', firstname: 'Jozef', githubId: 'jsakalos', lastname: 'Sakalos'} |
| 38 | + ], |
| 39 | + sorters: [ |
| 40 | + {direction: 'ASC', property: 'firstname'}, |
| 41 | + {direction: 'DESC', property: 'lastname'} |
| 42 | + ] |
| 43 | + }); |
| 44 | + |
| 45 | + expect(collection.count).toBe(6); |
| 46 | + expect(collection.map.size).toBe(6); |
| 47 | + }); |
| 48 | + |
| 49 | + test('Modify collection items', () => { |
| 50 | + collection.add({country: 'Germany', firstname: 'Bastian', githubId: 'bhaustein', lastname: 'Haustein'}); |
| 51 | + |
| 52 | + expect(collection.count).toBe(7); |
| 53 | + expect(collection.map.size).toBe(7); |
| 54 | + |
| 55 | + collection.remove('bhaustein'); |
| 56 | + |
| 57 | + expect(collection.count).toBe(6); |
| 58 | + expect(collection.map.size).toBe(6); |
| 59 | + |
| 60 | + collection.insert(1, [ |
| 61 | + {country: 'Germany', firstname: 'Bastian', githubId: 'bhaustein', lastname: 'Haustein'}, |
| 62 | + {country: 'Argentina', firstname: 'Max', githubId: 'elmasse', lastname: 'Fierro'} |
| 63 | + ]); |
| 64 | + |
| 65 | + expect(collection.count).toBe(8); |
| 66 | + expect(collection.map.size).toBe(8); |
| 67 | + |
| 68 | + collection.insert(1, {country: 'Croatia', firstname: 'Grgur', githubId: 'grgur', lastname: 'Grisogono'}); |
| 69 | + |
| 70 | + expect(collection.count).toBe(9); |
| 71 | + expect(collection.map.size).toBe(9); |
| 72 | + |
| 73 | + expect(collection.getRange(1, 4)).toEqual([ |
| 74 | + {country: 'USA', firstname: 'Gerard', githubId: 'camtnbikerrwc', lastname: 'Horan'}, |
| 75 | + {country: 'Croatia', firstname: 'Grgur', githubId: 'grgur', lastname: 'Grisogono'}, |
| 76 | + {country: 'Slovakia', firstname: 'Jozef', githubId: 'jsakalos', lastname: 'Sakalos'} |
| 77 | + ]); |
| 78 | + |
| 79 | + expect(collection.indexOf('elmasse')).toBe(4); |
| 80 | + }); |
| 81 | + |
| 82 | + test('Sort collection items', () => { |
| 83 | + collection.sorters = [ |
| 84 | + {direction: 'DESC', property: 'lastname'}, |
| 85 | + {direction: 'ASC', property: 'firstname'} |
| 86 | + ]; |
| 87 | + |
| 88 | + expect(collection.getRange()).toEqual([ |
| 89 | + {country: 'USA', firstname: 'Rich', githubId: 'rwaters', lastname: 'Waters'}, |
| 90 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 91 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'}, |
| 92 | + {country: 'Slovakia', firstname: 'Jozef', githubId: 'jsakalos', lastname: 'Sakalos'}, |
| 93 | + {country: 'USA', firstname: 'Gerard', githubId: 'camtnbikerrwc', lastname: 'Horan'}, |
| 94 | + {country: 'Germany', firstname: 'Bastian', githubId: 'bhaustein', lastname: 'Haustein'}, |
| 95 | + {country: 'Croatia', firstname: 'Grgur', githubId: 'grgur', lastname: 'Grisogono'}, |
| 96 | + {country: 'Argentina', firstname: 'Max', githubId: 'elmasse', lastname: 'Fierro'}, |
| 97 | + {country: 'Germany', firstname: 'Nils', githubId: 'mrsunshine', lastname: 'Dehl'} |
| 98 | + ]); |
| 99 | + |
| 100 | + collection.sorters[0].property = 'country'; |
| 101 | + |
| 102 | + expect(collection.getRange()).toEqual([ |
| 103 | + {country: 'USA', firstname: 'Gerard', githubId: 'camtnbikerrwc', lastname: 'Horan'}, |
| 104 | + {country: 'USA', firstname: 'Rich', githubId: 'rwaters', lastname: 'Waters'}, |
| 105 | + {country: 'Slovakia', firstname: 'Jozef', githubId: 'jsakalos', lastname: 'Sakalos'}, |
| 106 | + {country: 'Germany', firstname: 'Bastian', githubId: 'bhaustein', lastname: 'Haustein'}, |
| 107 | + {country: 'Germany', firstname: 'Nils', githubId: 'mrsunshine', lastname: 'Dehl'}, |
| 108 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 109 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'}, |
| 110 | + {country: 'Croatia', firstname: 'Grgur', githubId: 'grgur', lastname: 'Grisogono'}, |
| 111 | + {country: 'Argentina', firstname: 'Max', githubId: 'elmasse', lastname: 'Fierro'} |
| 112 | + ]); |
| 113 | + }); |
| 114 | + |
| 115 | + test('Clone collection', () => { |
| 116 | + collection2 = collection.clone(); |
| 117 | + |
| 118 | + // todo: clone should use the current filters & sorters |
| 119 | + expect(collection2.getRange()).toEqual([ |
| 120 | + {country: 'USA', firstname: 'Rich', githubId: 'rwaters', lastname: 'Waters'}, |
| 121 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 122 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'}, |
| 123 | + {country: 'Slovakia', firstname: 'Jozef', githubId: 'jsakalos', lastname: 'Sakalos'}, |
| 124 | + {country: 'USA', firstname: 'Gerard', githubId: 'camtnbikerrwc', lastname: 'Horan'}, |
| 125 | + {country: 'Germany', firstname: 'Bastian', githubId: 'bhaustein', lastname: 'Haustein'}, |
| 126 | + {country: 'Croatia', firstname: 'Grgur', githubId: 'grgur', lastname: 'Grisogono'}, |
| 127 | + {country: 'Argentina', firstname: 'Max', githubId: 'elmasse', lastname: 'Fierro'}, |
| 128 | + {country: 'Germany', firstname: 'Nils', githubId: 'mrsunshine', lastname: 'Dehl'} |
| 129 | + ]); |
| 130 | + |
| 131 | + collection2.sorters = [ |
| 132 | + {direction: 'ASC', property: 'firstname'}, |
| 133 | + {direction: 'DESC', property: 'lastname'} |
| 134 | + ]; |
| 135 | + |
| 136 | + expect(collection2.getRange()).toEqual([ |
| 137 | + {country: 'Germany', firstname: 'Bastian', githubId: 'bhaustein', lastname: 'Haustein'}, |
| 138 | + {country: 'USA', firstname: 'Gerard', githubId: 'camtnbikerrwc', lastname: 'Horan'}, |
| 139 | + {country: 'Croatia', firstname: 'Grgur', githubId: 'grgur', lastname: 'Grisogono'}, |
| 140 | + {country: 'Slovakia', firstname: 'Jozef', githubId: 'jsakalos', lastname: 'Sakalos'}, |
| 141 | + {country: 'Argentina', firstname: 'Max', githubId: 'elmasse', lastname: 'Fierro'}, |
| 142 | + {country: 'Germany', firstname: 'Nils', githubId: 'mrsunshine', lastname: 'Dehl'}, |
| 143 | + {country: 'USA', firstname: 'Rich', githubId: 'rwaters', lastname: 'Waters'}, |
| 144 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 145 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'} |
| 146 | + ]); |
| 147 | + |
| 148 | + collection2.filters = [ |
| 149 | + {property: 'firstname', value: 'Tobias'} |
| 150 | + ]; |
| 151 | + |
| 152 | + expect(collection2.getRange()).toEqual([ |
| 153 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 154 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'} |
| 155 | + ]); |
| 156 | + |
| 157 | + collection2.add( |
| 158 | + {country: 'France', firstname: 'Nigel', githubId: 'NigeWhite', lastname: 'White'} |
| 159 | + ); |
| 160 | + |
| 161 | + expect(collection2.getRange()).toEqual([ |
| 162 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 163 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'} |
| 164 | + ]); |
| 165 | + |
| 166 | + expect(collection2.isFiltered()).toBeTruthy(); |
| 167 | + |
| 168 | + collection2.filters[0].disabled = true; |
| 169 | + |
| 170 | + expect(collection2.getRange()).toEqual([ |
| 171 | + {country: 'Germany', firstname: 'Bastian', githubId: 'bhaustein', lastname: 'Haustein'}, |
| 172 | + {country: 'USA', firstname: 'Gerard', githubId: 'camtnbikerrwc', lastname: 'Horan'}, |
| 173 | + {country: 'Croatia', firstname: 'Grgur', githubId: 'grgur', lastname: 'Grisogono'}, |
| 174 | + {country: 'Slovakia', firstname: 'Jozef', githubId: 'jsakalos', lastname: 'Sakalos'}, |
| 175 | + {country: 'Argentina', firstname: 'Max', githubId: 'elmasse', lastname: 'Fierro'}, |
| 176 | + {country: 'France', firstname: 'Nigel', githubId: 'NigeWhite', lastname: 'White'}, |
| 177 | + {country: 'Germany', firstname: 'Nils', githubId: 'mrsunshine', lastname: 'Dehl'}, |
| 178 | + {country: 'USA', firstname: 'Rich', githubId: 'rwaters', lastname: 'Waters'}, |
| 179 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 180 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'}, |
| 181 | + ]); |
| 182 | + |
| 183 | + expect(collection2.isFiltered()).toBeFalsy(); |
| 184 | + |
| 185 | + collection2.filters[0].disabled = false; |
| 186 | + |
| 187 | + expect(collection2.isFiltered()).toBeTruthy(); |
| 188 | + |
| 189 | + expect(collection2.getRange()).toEqual([ |
| 190 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 191 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'} |
| 192 | + ]); |
| 193 | + |
| 194 | + collection2.clearFilters(); |
| 195 | + |
| 196 | + expect(collection2.getRange()).toEqual([ |
| 197 | + {country: 'Germany', firstname: 'Bastian', githubId: 'bhaustein', lastname: 'Haustein'}, |
| 198 | + {country: 'USA', firstname: 'Gerard', githubId: 'camtnbikerrwc', lastname: 'Horan'}, |
| 199 | + {country: 'Croatia', firstname: 'Grgur', githubId: 'grgur', lastname: 'Grisogono'}, |
| 200 | + {country: 'Slovakia', firstname: 'Jozef', githubId: 'jsakalos', lastname: 'Sakalos'}, |
| 201 | + {country: 'Argentina', firstname: 'Max', githubId: 'elmasse', lastname: 'Fierro'}, |
| 202 | + {country: 'France', firstname: 'Nigel', githubId: 'NigeWhite', lastname: 'White'}, |
| 203 | + {country: 'Germany', firstname: 'Nils', githubId: 'mrsunshine', lastname: 'Dehl'}, |
| 204 | + {country: 'USA', firstname: 'Rich', githubId: 'rwaters', lastname: 'Waters'}, |
| 205 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 206 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'}, |
| 207 | + ]); |
| 208 | + |
| 209 | + collection2.clearSorters(true); |
| 210 | + |
| 211 | + expect(collection2.getRange()).toEqual([ |
| 212 | + {country: 'France', firstname: 'Nigel', githubId: 'NigeWhite', lastname: 'White'}, |
| 213 | + {country: 'USA', firstname: 'Rich', githubId: 'rwaters', lastname: 'Waters'}, |
| 214 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 215 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'}, |
| 216 | + {country: 'Slovakia', firstname: 'Jozef', githubId: 'jsakalos', lastname: 'Sakalos'}, |
| 217 | + {country: 'USA', firstname: 'Gerard', githubId: 'camtnbikerrwc', lastname: 'Horan'}, |
| 218 | + {country: 'Germany', firstname: 'Bastian', githubId: 'bhaustein', lastname: 'Haustein'}, |
| 219 | + {country: 'Croatia', firstname: 'Grgur', githubId: 'grgur', lastname: 'Grisogono'}, |
| 220 | + {country: 'Argentina', firstname: 'Max', githubId: 'elmasse', lastname: 'Fierro'}, |
| 221 | + {country: 'Germany', firstname: 'Nils', githubId: 'mrsunshine', lastname: 'Dehl'} |
| 222 | + ]); |
| 223 | + }); |
| 224 | + |
| 225 | + test('Filter collection', () => { |
| 226 | + collection3 = collection.clone(); |
| 227 | + |
| 228 | + expect(collection3.getRange()).toEqual([ |
| 229 | + {country: 'USA', firstname: 'Rich', githubId: 'rwaters', lastname: 'Waters'}, |
| 230 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu2', lastname: 'Uhlig2'}, |
| 231 | + {country: 'Germany', firstname: 'Tobias', githubId: 'tobiu', lastname: 'Uhlig'}, |
| 232 | + {country: 'Slovakia', firstname: 'Jozef', githubId: 'jsakalos', lastname: 'Sakalos'}, |
| 233 | + {country: 'USA', firstname: 'Gerard', githubId: 'camtnbikerrwc', lastname: 'Horan'}, |
| 234 | + {country: 'Germany', firstname: 'Bastian', githubId: 'bhaustein', lastname: 'Haustein'}, |
| 235 | + {country: 'Croatia', firstname: 'Grgur', githubId: 'grgur', lastname: 'Grisogono'}, |
| 236 | + {country: 'Argentina', firstname: 'Max', githubId: 'elmasse', lastname: 'Fierro'}, |
| 237 | + {country: 'Germany', firstname: 'Nils', githubId: 'mrsunshine', lastname: 'Dehl'} |
| 238 | + ]); |
| 239 | + |
| 240 | + collection3.remove('mrsunshine'); |
| 241 | + collection3.remove('elmasse'); |
| 242 | + |
| 243 | + expect(collection3.count).toBe(7); |
| 244 | + |
| 245 | + collection3.filters = [{ |
| 246 | + includeEmptyValues: true, |
| 247 | + operator : 'like', |
| 248 | + property : 'firstname', |
| 249 | + value : 'a' |
| 250 | + }]; |
| 251 | + |
| 252 | + expect(collection3.count).toBe(4); |
| 253 | + expect(collection3.allItems.count).toBe(7); |
| 254 | + |
| 255 | + collection3.add([ |
| 256 | + {country: 'Argentina', firstname: 'Max', githubId: 'elmasse', lastname: 'Fierro'}, |
| 257 | + {country: 'Germany', firstname: 'Nils', githubId: 'mrsunshine', lastname: 'Dehl'} |
| 258 | + ]); |
| 259 | + |
| 260 | + expect(collection3.count).toBe(5); |
| 261 | + expect(collection3.allItems.count).toBe(9); |
| 262 | + }); |
| 263 | + |
| 264 | + test('Add & remove at same time', () => { |
| 265 | + collection = Neo.create(Collection, { |
| 266 | + items: [ |
| 267 | + {id: 'a'}, |
| 268 | + {id: 'b'}, |
| 269 | + {id: 'c'}, |
| 270 | + {id: 'd'}, |
| 271 | + {id: 'e'}, |
| 272 | + {id: 'f'} |
| 273 | + ] |
| 274 | + }); |
| 275 | + |
| 276 | + collection.splice(2, [{id: 'a'}, {id: 'd'}, {id: 'f'}], [{id: 'x'}, {id: 'y'}, {id: 'z'}]); |
| 277 | + |
| 278 | + expect(collection.getRange()).toEqual([ |
| 279 | + {id: 'b'}, |
| 280 | + {id: 'c'}, |
| 281 | + {id: 'x'}, |
| 282 | + {id: 'y'}, |
| 283 | + {id: 'z'}, |
| 284 | + {id: 'e'} |
| 285 | + ]); |
| 286 | + }); |
| 287 | + |
| 288 | + test('Move collection items', () => { |
| 289 | + let moveCollection = Neo.create(Collection, { |
| 290 | + items: [ |
| 291 | + {id: 'a'}, |
| 292 | + {id: 'b'}, |
| 293 | + {id: 'c'}, |
| 294 | + {id: 'd'}, |
| 295 | + {id: 'e'} |
| 296 | + ] |
| 297 | + }); |
| 298 | + |
| 299 | + expect(moveCollection.getRange()).toEqual([{id: 'a'}, {id: 'b'}, {id: 'c'}, {id: 'd'}, {id: 'e'}]); |
| 300 | + |
| 301 | + // Move item forward |
| 302 | + moveCollection.move(1, 2); |
| 303 | + expect(moveCollection.getRange()).toEqual([{id: 'a'}, {id: 'c'}, {id: 'b'}, {id: 'd'}, {id: 'e'}]); |
| 304 | + |
| 305 | + // Swap adjacent items (backward) |
| 306 | + moveCollection.move(2, 1); |
| 307 | + expect(moveCollection.getRange()).toEqual([{id: 'a'}, {id: 'b'}, {id: 'c'}, {id: 'd'}, {id: 'e'}]); |
| 308 | + |
| 309 | + // Move item backward |
| 310 | + moveCollection.move(3, 1); |
| 311 | + expect(moveCollection.getRange()).toEqual([{id: 'a'}, {id: 'd'}, {id: 'b'}, {id: 'c'}, {id: 'e'}]); |
| 312 | + |
| 313 | + // Move item forward |
| 314 | + moveCollection.move(1, 3); |
| 315 | + expect(moveCollection.getRange()).toEqual([{id: 'a'}, {id: 'b'}, {id: 'c'}, {id: 'd'}, {id: 'e'}]); |
| 316 | + |
| 317 | + // Swap adjacent items (forward) |
| 318 | + moveCollection.move(0, 1); |
| 319 | + expect(moveCollection.getRange()).toEqual([{id: 'b'}, {id: 'a'}, {id: 'c'}, {id: 'd'}, {id: 'e'}]); |
| 320 | + |
| 321 | + // Swap adjacent items (backward) |
| 322 | + moveCollection.move(1, 0); |
| 323 | + expect(moveCollection.getRange()).toEqual([{id: 'a'}, {id: 'b'}, {id: 'c'}, {id: 'd'}, {id: 'e'}]); |
| 324 | + |
| 325 | + // Move to end |
| 326 | + moveCollection.move(0, 4); |
| 327 | + expect(moveCollection.getRange()).toEqual([{id: 'b'}, {id: 'c'}, {id: 'd'}, {id: 'e'}, {id: 'a'}]); |
| 328 | + |
| 329 | + // Move to start |
| 330 | + moveCollection.move(4, 0); |
| 331 | + expect(moveCollection.getRange()).toEqual([{id: 'a'}, {id: 'b'}, {id: 'c'}, {id: 'd'}, {id: 'e'}]); |
| 332 | + }); |
| 333 | +}); |
0 commit comments