Skip to content

Commit

Permalink
Extract common logic to separate function to make the tests more focu…
Browse files Browse the repository at this point in the history
…sed.
  • Loading branch information
elle-j committed Dec 4, 2023
1 parent 88de993 commit 38656a9
Showing 1 changed file with 34 additions and 45 deletions.
79 changes: 34 additions & 45 deletions integration-tests/tests/src/tests/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,32 @@ describe("Realm.Object", () => {
describe("embedded properties", () => {
openRealmBeforeEach({ schema: [EmbeddedObjectSchema, ObjectWithEmbeddedObjectSchema] });

/**
* Creates the Realm object and asserts (using `expect()`) that its embedded value
* is equal to the one provided.
*
* @param realm The realm to use.
* @param objectToCreate The object to create without the primary key (`_id`, will be generated).
* @returns The created Realm object.
*/
function createObjectWithEmbeddedObject(realm: Realm, objectToCreate: Omit<IObjectWithEmbeddedObject, "_id">) {
const objectCreated = realm.write(() => {
return realm.create<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name, objectToCreate);
});

const objects = realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;

if (objectToCreate.embeddedValue) {
expect(objectCreated.embeddedValue?.intValue).to.equal(objectToCreate.embeddedValue.intValue);
} else {
expect(objectCreated.embeddedValue).to.equal(objectToCreate.embeddedValue);
}

return objectCreated;
}

it("sets an embedded property to null on creation", function (this: Mocha.Context & RealmContext) {
this.realm.write(() => {
this.realm.create(ObjectWithEmbeddedObjectSchema.name, { embeddedValue: null });
Expand All @@ -1294,31 +1320,21 @@ describe("Realm.Object", () => {
});

it("updates an embedded property to null", function (this: Mocha.Context & RealmContext) {
this.realm.write(() => {
this.realm.create(ObjectWithEmbeddedObjectSchema.name, { embeddedValue: { intValue: 1 } });
});
createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });

const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
const firstObject = objects[0];
expect(firstObject.embeddedValue?.intValue).to.equal(1);

this.realm.write(() => {
firstObject.embeddedValue = null;
});
expect(firstObject.embeddedValue).to.be.null;
});

it("updates an embedded property to null when set to undefined", function (this: Mocha.Context & RealmContext) {
this.realm.write(() => {
this.realm.create(ObjectWithEmbeddedObjectSchema.name, { embeddedValue: { intValue: 1 } });
});
createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });

const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
const firstObject = objects[0];
expect(firstObject.embeddedValue?.intValue).to.equal(1);

this.realm.write(() => {
firstObject.embeddedValue = undefined;
});
Expand All @@ -1329,16 +1345,7 @@ describe("Realm.Object", () => {

it("updates an embedded property to null with `UpdateMode.Modified`", function (this: Mocha.Context &
RealmContext) {
const objectCreated = this.realm.write(() => {
return this.realm.create<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name, {
embeddedValue: { intValue: 1 },
});
});

let objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;
expect(objects[0].embeddedValue?.intValue).to.equal(1);
const objectCreated = createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });

this.realm.write(() => {
this.realm.create(
Expand All @@ -1350,23 +1357,14 @@ describe("Realm.Object", () => {
UpdateMode.Modified,
);
});
objects = this.realm.objects(ObjectWithEmbeddedObjectSchema.name);
const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;
expect(objects[0].embeddedValue).to.be.null;
});

it("updates an embedded property to null with `UpdateMode.All`", function (this: Mocha.Context & RealmContext) {
const objectCreated = this.realm.write(() => {
return this.realm.create<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name, {
embeddedValue: { intValue: 1 },
});
});

let objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;
expect(objects[0].embeddedValue?.intValue).to.equal(1);
const objectCreated = createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });

this.realm.write(() => {
this.realm.create(
Expand All @@ -1378,24 +1376,15 @@ describe("Realm.Object", () => {
UpdateMode.All,
);
});
objects = this.realm.objects(ObjectWithEmbeddedObjectSchema.name);
const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;
expect(objects[0].embeddedValue).to.be.null;
});

it("does not update an embedded property to null when omitted with `UpdateMode.Modified`", function (this: Mocha.Context &
RealmContext) {
const objectCreated = this.realm.write(() => {
return this.realm.create<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name, {
embeddedValue: { intValue: 1 },
});
});

let objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id));
expect(objects[0].embeddedValue?.intValue).to.equal(1);
const objectCreated = createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });

// Create an object without the `embeddedValue` field.
this.realm.write(() => {
Expand All @@ -1407,7 +1396,7 @@ describe("Realm.Object", () => {
UpdateMode.Modified,
);
});
objects = this.realm.objects(ObjectWithEmbeddedObjectSchema.name);
const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id));
expect(objects[0].embeddedValue?.intValue).to.equal(1);
Expand Down

0 comments on commit 38656a9

Please sign in to comment.