Skip to content

Commit

Permalink
Refactored manual transaction tests (#4881)
Browse files Browse the repository at this point in the history
* Refactored manual transaction tests

* Update transaction.ts
  • Loading branch information
kraenhansen committed Sep 13, 2022
1 parent c7d259a commit c779326
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions integration-tests/tests/src/tests/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,55 @@
//
////////////////////////////////////////////////////////////////////////////
import { expect } from "chai";
import Realm from "realm";
import { openRealmBeforeEach } from "../hooks";

import { PersonSchema } from "../schemas/person-and-dogs";

describe("Realm transactions", () => {
beforeEach(() => {
Realm.clearTestState();
});
openRealmBeforeEach({ schema: [PersonSchema] });

describe("Manual transactions", () => {
it("throw exception in transaction", () => {
// https://github.com/realm/realm-js/issues/4747
const message = "Something is wrong";
const realm = new Realm({ schema: [PersonSchema] });
it("rolls back when cancelled", function (this: RealmContext) {
const { realm } = this;
realm.beginTransaction();
try {
realm.create(PersonSchema.name, {
name: "John Doe",
age: 42,
});
throw new Error(message);
realm.commitTransaction();
} catch (err) {
expect((err as Error).message).equals(message);
expect(realm.isInTransaction).to.be.true;
realm.cancelTransaction();
} finally {
expect(realm.objects(PersonSchema.name).length).equals(0);
realm.close();
}
expect(realm.isClosed).to.be.true;

const persons = realm.objects(PersonSchema.name);
expect(persons.length).equals(0);

realm.create(PersonSchema.name, {
name: "John Doe",
age: 42,
});

expect(persons.length).equals(1);
expect(realm.isInTransaction).to.be.true;

realm.cancelTransaction();
expect(persons.length).equals(0);
expect(realm.isInTransaction).to.be.false;
});

it("invalid object", () => {
// https://github.com/realm/realm-js/issues/4747
const message = "Person.age must be of type 'number', got 'string' ('five')";
const realm = new Realm({ schema: [PersonSchema] });
it("throws on an invalid object", function (this: RealmContext) {
const { realm } = this;
realm.beginTransaction();
try {

const persons = realm.objects(PersonSchema.name);
expect(persons.length).equals(0);

expect(() => {
realm.create(PersonSchema.name, {
name: "John Doe",
age: "five", // wrong type
});
realm.commitTransaction();
} catch (err) {
expect((err as Error).message).equals(message);
expect(realm.isInTransaction).to.be.true;
realm.cancelTransaction();
} finally {
expect(realm.objects(PersonSchema.name).length).equals(0);
realm.close();
}
expect(realm.isClosed).to.be.true;
realm.commitTransaction(); // We don't expect this to be called
}).throws("Person.age must be of type 'number', got 'string' ('five')");

// TODO: Fix 👇 ... its a bit surprising that an object gets created at all
expect(persons.length).equals(1);
expect(realm.isInTransaction).to.be.true;

realm.cancelTransaction();
expect(persons.length).equals(0);
});
});
});

0 comments on commit c779326

Please sign in to comment.