Skip to content

Commit

Permalink
fix(database): Adding toJSON() method to TransactionResult (#104)
Browse files Browse the repository at this point in the history
* fix(database): Adding toJSON() method to TransactionResult

* fix(database): Making return type of toJSON explicit
  • Loading branch information
schmidt-sebastian committed Jul 25, 2017
1 parent 1c300d9 commit d9e0ce9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/database/api/Reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class Reference extends Query {
*/
transaction(transactionUpdate: (a: any) => any,
onComplete?: (a: Error | null, b: boolean, c: DataSnapshot | null) => void,
applyLocally?: boolean): Promise<any> {
applyLocally?: boolean): Promise<TransactionResult> {
validateArgCount('Reference.transaction', 1, 3, arguments.length);
validateWritablePath('Reference.transaction', this.path);
validateCallback('Reference.transaction', 1, transactionUpdate, false);
Expand Down
9 changes: 9 additions & 0 deletions src/database/api/TransactionResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { DataSnapshot } from './DataSnapshot';
import { validateArgCount } from '../../utils/validation';

export class TransactionResult {
/**
Expand All @@ -27,4 +28,12 @@ export class TransactionResult {
constructor(public committed: boolean, public snapshot: DataSnapshot) {

}

// Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
// for end-users
toJSON(): object {
validateArgCount('TransactionResult.toJSON', 0, 1, arguments.length);
return { committed: this.committed, snapshot: this.snapshot.toJSON() };
}

}
10 changes: 10 additions & 0 deletions tests/database/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ describe('Transaction Tests', function() {
expect(eventHelper.waiter()).to.equal(true);
});

it('Transaction result can be converted to JSON.', function() {
const node = (getRandomNode() as Reference);

return node.transaction(() => {
return 42;
}).then(transactionResult => {
expect(transactionResult.toJSON()).to.deep.equal({ committed: true, snapshot: 42 });
});
});

it('Non-aborted transaction sets committed to true in callback.', function(done) {
const node = (getRandomNode() as Reference);

Expand Down

0 comments on commit d9e0ce9

Please sign in to comment.