(autoIndex: `off` | `eager`)
| 254 | } |
| 255 | |
| 256 | function createOrderByTests(autoIndex: `off` | `eager`): void { |
| 257 | describe(`with autoIndex ${autoIndex}`, () => { |
| 258 | // Some tests require an index for incremental updates (loadMoreIfNeeded). |
| 259 | // These only work with autoIndex: 'eager' which auto-creates the needed indexes. |
| 260 | const itWhenAutoIndexEager = autoIndex === `eager` ? it : it.skip |
| 261 | |
| 262 | let employeesCollection: ReturnType<typeof createEmployeesCollection> |
| 263 | let departmentsCollection: ReturnType<typeof createDepartmentsCollection> |
| 264 | |
| 265 | beforeEach(() => { |
| 266 | employeesCollection = createEmployeesCollection(autoIndex) |
| 267 | departmentsCollection = createDepartmentsCollection(autoIndex) |
| 268 | }) |
| 269 | |
| 270 | describe(`Basic OrderBy`, () => { |
| 271 | it(`orders by single column ascending`, async () => { |
| 272 | const collection = createLiveQueryCollection((q) => |
| 273 | q |
| 274 | .from({ employees: employeesCollection }) |
| 275 | .orderBy(({ employees }) => employees.name, `asc`) |
| 276 | .select(({ employees }) => ({ |
| 277 | id: employees.id, |
| 278 | name: employees.name, |
| 279 | })), |
| 280 | ) |
| 281 | await collection.preload() |
| 282 | |
| 283 | const results = Array.from(collection.values()) |
| 284 | |
| 285 | expect(results).toHaveLength(5) |
| 286 | expect(results.map((r) => r.name)).toEqual([ |
| 287 | `Alice`, |
| 288 | `Bob`, |
| 289 | `Charlie`, |
| 290 | `Diana`, |
| 291 | `Eve`, |
| 292 | ]) |
| 293 | }) |
| 294 | |
| 295 | it(`orders by single column descending`, async () => { |
| 296 | const collection = createLiveQueryCollection((q) => |
| 297 | q |
| 298 | .from({ employees: employeesCollection }) |
| 299 | .orderBy(({ employees }) => employees.salary, `desc`) |
| 300 | .select(({ employees }) => ({ |
| 301 | id: employees.id, |
| 302 | name: employees.name, |
| 303 | salary: employees.salary, |
| 304 | })), |
| 305 | ) |
| 306 | await collection.preload() |
| 307 | |
| 308 | const results = Array.from(collection.values()) |
| 309 | |
| 310 | expect(results).toHaveLength(5) |
| 311 | expect(results.map((r) => r.salary)).toEqual([ |
| 312 | 65000, 60000, 55000, 52000, 50000, |
| 313 | ]) |
no test coverage detected