|
| 1 | +import { Stack } from 'aws-cdk-lib'; |
| 2 | +import { AttributeType, Table } from 'aws-cdk-lib/aws-dynamodb'; |
| 3 | +import { getGlobalSecondaryIndexes } from '../../utils/schema-utils'; |
| 4 | + |
| 5 | +describe('getGlobalSecondaryIndexes', () => { |
| 6 | + it('reads the indexes off a real DynamoDB L2 Table regardless of the installed aws-cdk-lib field name', () => { |
| 7 | + const table = new Table(new Stack(), 'TestTable', { |
| 8 | + partitionKey: { name: 'id', type: AttributeType.STRING }, |
| 9 | + }); |
| 10 | + table.addGlobalSecondaryIndex({ |
| 11 | + indexName: 'byName', |
| 12 | + partitionKey: { name: 'name', type: AttributeType.STRING }, |
| 13 | + }); |
| 14 | + |
| 15 | + const indexes = getGlobalSecondaryIndexes(table); |
| 16 | + |
| 17 | + expect(indexes).toBeDefined(); |
| 18 | + expect(indexes.find((gsi: any) => gsi.indexName === 'byName')).toBeDefined(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns the private _globalSecondaryIndexes ArrayBox when present', () => { |
| 22 | + const indexes = [{ indexName: 'byName', keySchema: [{ attributeName: 'name', keyType: 'HASH' }] }]; |
| 23 | + |
| 24 | + expect(getGlobalSecondaryIndexes({ _globalSecondaryIndexes: indexes })).toBe(indexes); |
| 25 | + }); |
| 26 | + |
| 27 | + it('falls back to the public globalSecondaryIndexes array (Amplify managed table)', () => { |
| 28 | + const indexes = [{ indexName: 'byOwner', keySchema: [{ attributeName: 'owner', keyType: 'HASH' }] }]; |
| 29 | + |
| 30 | + expect(getGlobalSecondaryIndexes({ globalSecondaryIndexes: indexes })).toBe(indexes); |
| 31 | + }); |
| 32 | + |
| 33 | + it('prefers _globalSecondaryIndexes when both fields are populated', () => { |
| 34 | + const renamed = [{ indexName: 'renamed' }]; |
| 35 | + const legacy = [{ indexName: 'legacy' }]; |
| 36 | + |
| 37 | + expect(getGlobalSecondaryIndexes({ _globalSecondaryIndexes: renamed, globalSecondaryIndexes: legacy })).toBe(renamed); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns undefined when neither field is present', () => { |
| 41 | + expect(getGlobalSecondaryIndexes({})).toBeUndefined(); |
| 42 | + }); |
| 43 | +}); |
0 commit comments