Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/shared/db/__tests__/entity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { TransactionOnchainEntity } from 'src/subdomains/lightning/entities/transaction-onchain.entity';

// Pins "useDefineForClassFields": false in tsconfig.json. With define semantics (the default
// for target >= es2022), fresh entities carry own undefined class fields, so the
// Object.assign(dbEntity, freshEntity) update pattern wipes the loaded row's id and
// save() inserts a duplicate row instead of updating (issue #205).
describe('entity class-field semantics', () => {
it('fresh entity carries no own id property', () => {
const fresh = new TransactionOnchainEntity();

expect(Object.getOwnPropertyNames(fresh)).not.toContain('id');
});

it('Object.assign of a fresh entity onto a loaded row preserves the row id', () => {
const dbRow = new TransactionOnchainEntity();
dbRow.id = 42;
dbRow.transaction = 'txid';
dbRow.amount = 1;

const fresh = new TransactionOnchainEntity();
fresh.transaction = 'txid';
fresh.amount = 2;

Object.assign(dbRow, fresh);

expect(dbRow.id).toBe(42);
expect(dbRow.amount).toBe(2);
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2022",
"useDefineForClassFields": false,
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
Expand Down
Loading