From 5482849c70e217c630b6ec45d70398b35089f460 Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Fri, 5 Jan 2024 16:45:30 +0100 Subject: [PATCH] Trying to reproduce #6322 (#6330) * Trying to reproduce #6322 --- integration-tests/tests/src/tests/list.ts | 81 +++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/integration-tests/tests/src/tests/list.ts b/integration-tests/tests/src/tests/list.ts index d714c0a957..40b408ed06 100644 --- a/integration-tests/tests/src/tests/list.ts +++ b/integration-tests/tests/src/tests/list.ts @@ -416,6 +416,32 @@ TodoList.schema = { }, }; +// https://github.com/realm/realm-js/issues/6322 +class Farmer extends Realm.Object { + id!: number; + currentFarm?: Farm; + static schema: Realm.ObjectSchema = { + name: "Farmer", + primaryKey: "id", + properties: { + id: "int", + currentFarm: "Farm", + }, + }; +} + +class Farm extends Realm.Object { + id!: number; + + static schema: Realm.ObjectSchema = { + name: "Farm", + primaryKey: "id", + properties: { + id: "int", + }, + }; +} + describe("Lists", () => { describe("constructor", () => { openRealmBeforeEach({ schema: [PersonSchema, PersonListSchema] }); @@ -2258,4 +2284,59 @@ describe("Lists", () => { }); }); }); + + // https://github.com/realm/realm-js/issues/6322 + describe("Link with primary key", () => { + openRealmBeforeEach({ schema: [Farm, Farmer] }); + + it("Update - assignment", function (this: RealmContext) { + this.realm.write(() => { + this.realm.create("Farmer", { id: 11, currentFarm: { id: 22 } }); + }); + + expect(this.realm.objects(Farmer).length).equals(1); + expect(this.realm.objectForPrimaryKey(Farmer, 11)).not.to.be.undefined; + expect(this.realm.objectForPrimaryKey(Farmer, 11)?.currentFarm?.id).equals(22); + + this.realm.write(() => { + const newFarm = this.realm.create(Farm, { id: 33 }); + const farmer = this.realm.objectForPrimaryKey(Farmer, 11); + expect(farmer).not.null; + //@ts-expect-error cannot be null + farmer.currentFarm = newFarm; + }); + + expect(this.realm.objects(Farm).length).equals(2); + expect(this.realm.objectForPrimaryKey(Farmer, 11)?.currentFarm?.id).equals(33); + + // change it link back to the first object + this.realm.write(() => { + const farmer = this.realm.objectForPrimaryKey(Farmer, 11); + expect(farmer).not.null; + const oldFarm = this.realm.objectForPrimaryKey(Farm, 22); + //@ts-expect-error cannot be null + farmer.currentFarm = oldFarm; + }); + + expect(this.realm.objects(Farm).length).equals(2); + expect(this.realm.objectForPrimaryKey(Farmer, 11)?.currentFarm?.id).equals(22); + }); + + it("Update - same object", function (this: RealmContext) { + this.realm.write(() => { + this.realm.create("Farmer", { id: 11, currentFarm: { id: 22 } }); + }); + + const farmer = this.realm.objectForPrimaryKey(Farmer, 11); + const farm = this.realm.objectForPrimaryKey(Farm, 22); + + this.realm.write(() => { + //@ts-expect-error cannot be null + farmer.currentFarm = farm; + }); + + expect(this.realm.objects(Farmer).length).equals(1); + expect(this.realm.objects(Farm).length).equals(1); + }); + }); });