From 39b10a70e10db35e25aca3eed8f434bdb1ba0cf8 Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Thu, 18 Apr 2024 17:00:54 +0200 Subject: [PATCH] RJS-2143: Tests to clarify number conversion (#6623) * Tests to clarify number conversion --- .../tests/src/tests/shared-realms.ts | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/integration-tests/tests/src/tests/shared-realms.ts b/integration-tests/tests/src/tests/shared-realms.ts index f88d8f67af..02afcfc1af 100644 --- a/integration-tests/tests/src/tests/shared-realms.ts +++ b/integration-tests/tests/src/tests/shared-realms.ts @@ -134,4 +134,64 @@ describe("SharedRealm operations", () => { expect(this.realm.objectForPrimaryKey("Person", "Bob")).primaryKey.equals("Bob"); }); }); + + describe("Number conversion", () => { + beforeEach(() => { + Realm.clearTestState(); + }); + + it("Int field does not accept Infinity", async () => { + const IntSchema = { + name: "IntSchema", + properties: { + id: "int", + number: "int", + }, + primaryKey: "id", + }; + + const realm = await Realm.open({ + inMemory: true, + schema: [IntSchema], + }); + + // Infinity is not a valid integer. Node and RN throw different error messages so we only + // check if throws. + // node: "The number Infinity cannot be converted to a BigInt because it is not an integer" + // RN: "number is not integral" + expect(() => { + realm.write(() => { + realm.create(IntSchema.name, { + id: 1, + number: Infinity, + }); + }); + }).to.throw(); + }); + + it("Double field accepts Infinity", async () => { + const DoubleSchema = { + name: "DoubleSchema", + properties: { + id: "int", + number: "double", + }, + primaryKey: "id", + }; + + const realm = await Realm.open({ + inMemory: true, + schema: [DoubleSchema], + }); + + const obj = realm.write(() => { + return realm.create(DoubleSchema.name, { + id: 1, + number: Infinity, + }); + }); + + expect(obj.number).equals(Infinity); + }); + }); });