Skip to content

Commit

Permalink
Trying to reproduce #6322 (#6330)
Browse files Browse the repository at this point in the history
* Trying to reproduce #6322
  • Loading branch information
kneth authored Jan 5, 2024
1 parent fe72acb commit 5482849
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions integration-tests/tests/src/tests/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,32 @@ TodoList.schema = {
},
};

// https://github.com/realm/realm-js/issues/6322
class Farmer extends Realm.Object<Farmer> {
id!: number;
currentFarm?: Farm;
static schema: Realm.ObjectSchema = {
name: "Farmer",
primaryKey: "id",
properties: {
id: "int",
currentFarm: "Farm",
},
};
}

class Farm extends Realm.Object<Farm> {
id!: number;

static schema: Realm.ObjectSchema = {
name: "Farm",
primaryKey: "id",
properties: {
id: "int",
},
};
}

describe("Lists", () => {
describe("constructor", () => {
openRealmBeforeEach({ schema: [PersonSchema, PersonListSchema] });
Expand Down Expand Up @@ -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>(Farmer).length).equals(1);
expect(this.realm.objectForPrimaryKey<Farmer>(Farmer, 11)).not.to.be.undefined;
expect(this.realm.objectForPrimaryKey<Farmer>(Farmer, 11)?.currentFarm?.id).equals(22);

this.realm.write(() => {
const newFarm = this.realm.create(Farm, { id: 33 });
const farmer = this.realm.objectForPrimaryKey<Farmer>(Farmer, 11);
expect(farmer).not.null;
//@ts-expect-error cannot be null
farmer.currentFarm = newFarm;
});

expect(this.realm.objects<Farm>(Farm).length).equals(2);
expect(this.realm.objectForPrimaryKey<Farmer>(Farmer, 11)?.currentFarm?.id).equals(33);

// change it link back to the first object
this.realm.write(() => {
const farmer = this.realm.objectForPrimaryKey<Farmer>(Farmer, 11);
expect(farmer).not.null;
const oldFarm = this.realm.objectForPrimaryKey<Farm>(Farm, 22);
//@ts-expect-error cannot be null
farmer.currentFarm = oldFarm;
});

expect(this.realm.objects<Farm>(Farm).length).equals(2);
expect(this.realm.objectForPrimaryKey<Farmer>(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>(Farmer, 11);
const farm = this.realm.objectForPrimaryKey<Farm>(Farm, 22);

this.realm.write(() => {
//@ts-expect-error cannot be null
farmer.currentFarm = farm;
});

expect(this.realm.objects<Farmer>(Farmer).length).equals(1);
expect(this.realm.objects<Farm>(Farm).length).equals(1);
});
});
});

0 comments on commit 5482849

Please sign in to comment.