|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { extractRecords } from '../extract-records'; |
| 11 | + |
| 12 | +describe('extractRecords', () => { |
| 13 | + const sampleData = [{ id: 1, name: 'A' }, { id: 2, name: 'B' }]; |
| 14 | + |
| 15 | + it('should return the array directly when results is an array', () => { |
| 16 | + expect(extractRecords(sampleData)).toEqual(sampleData); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should extract from results.records', () => { |
| 20 | + expect(extractRecords({ records: sampleData })).toEqual(sampleData); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should extract from results.data', () => { |
| 24 | + expect(extractRecords({ data: sampleData })).toEqual(sampleData); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should extract from results.value', () => { |
| 28 | + expect(extractRecords({ value: sampleData })).toEqual(sampleData); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return empty array for null/undefined', () => { |
| 32 | + expect(extractRecords(null)).toEqual([]); |
| 33 | + expect(extractRecords(undefined)).toEqual([]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should return empty array for non-array/non-object', () => { |
| 37 | + expect(extractRecords('string')).toEqual([]); |
| 38 | + expect(extractRecords(42)).toEqual([]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return empty array for object without recognized keys', () => { |
| 42 | + expect(extractRecords({ total: 100 })).toEqual([]); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should prefer records over data and value', () => { |
| 46 | + const records = [{ id: 1 }]; |
| 47 | + const data = [{ id: 2 }]; |
| 48 | + expect(extractRecords({ records, data })).toEqual(records); |
| 49 | + }); |
| 50 | +}); |
0 commit comments