From c20361403f257ed87b65a4182750cb4bf01ba175 Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Tue, 7 Jun 2022 12:26:29 +0200 Subject: [PATCH] Fix FLX error scenario tests (compensating writes) (#4615) * Fix FLX error scenario tests (compensating writes) Co-authored-by: Andrew Meyer --- CHANGELOG.md | 1 + .../tests/src/tests/sync/flexible.ts | 59 ++++++++----------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1163e5e6f..0a5dbeb983 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ x.x.x Release notes (yyyy-MM-dd) * * * +* Fix FLX error scenario tests. * Fixed a bug preventing opening a synced Realm as a local Realm. (since v10.18.0) 10.19.0 Release notes (2022-6-2) diff --git a/integration-tests/tests/src/tests/sync/flexible.ts b/integration-tests/tests/src/tests/sync/flexible.ts index 332cf1c607..816f4fd45b 100644 --- a/integration-tests/tests/src/tests/sync/flexible.ts +++ b/integration-tests/tests/src/tests/sync/flexible.ts @@ -33,7 +33,6 @@ import Realm, { BSON, ClientResetMode, FlexibleSyncConfiguration, SessionStopPol import { authenticateUserBefore, importAppBefore, openRealmBeforeEach } from "../../hooks"; import { DogSchema, IPerson, PersonSchema } from "../../schemas/person-and-dog-with-object-ids"; -import { expectInvalidWriteSyncError } from "../../utils/expect-sync-error"; import { closeAndReopenRealm, closeRealm } from "../../utils/close-realm"; const FlexiblePersonSchema = { ...PersonSchema, properties: { ...PersonSchema.properties, nonQueryable: "string?" } }; @@ -1569,56 +1568,50 @@ describe.skipIf(environment.missingServer, "Flexible sync", function () { // TODO test more complex integration scenarios, e.g. links, embedded objects, collections, complex queries describe("error scenarios", function () { - it("triggers an error if items are added without a subscription", async function (this: RealmContext) { - await expectInvalidWriteSyncError(this.config, this.user, (realm) => { - realm.write(() => { - return realm.create(FlexiblePersonSchema.name, { + it("throw an exception if items are added without a subscription", function (this: RealmContext) { + this.realm.write(() => { + expect(() => { + this.realm.create(FlexiblePersonSchema.name, { _id: new BSON.ObjectId(), name: "Tom", age: 36, }); - }); + }).to.throw("Cannot write to class Person when no flexible sync subscription has been created."); }); }); - it("triggers an error and deletes the item if an item not matching the filter is created", async function (this: RealmContext) { + it("deletes the item if an item not matching the filter is created", async function (this: RealmContext) { await addSubscriptionAndSync(this.realm, this.realm.objects(FlexiblePersonSchema.name).filtered("age < 30")); - await expectInvalidWriteSyncError( - this.config, - this.user, - (realm) => { - realm.write(() => { - realm.create(FlexiblePersonSchema.name, { - _id: new BSON.ObjectId(), - name: "Tom Old", - age: 99, - }); - }); - }, - () => { - expect(this.realm.objects(FlexiblePersonSchema.name)).to.have.length(0); - }, - ); + this.realm.write(() => { + this.realm.create(FlexiblePersonSchema.name, { + _id: new BSON.ObjectId(), + name: "Tom Old", + age: 99, + }); + }); + + await this.realm.syncSession?.downloadAllServerChanges(); + expect(this.realm.objects(FlexiblePersonSchema.name)).to.have.length(0); }); - it("triggers an error if you remove a subscription without waiting for server acknowledgement, then modify objects that were only matched by the now removed subscription", async function (this: RealmContext) { - await expectInvalidWriteSyncError(this.config, this.user, async (realm) => { - const { sub } = await addSubscriptionForPersonAndSync(this.realm); + it("throw an exception if you remove a subscription without waiting for server acknowledgement, then modify objects that were only matched by the now removed subscription", async function (this: RealmContext) { + const { sub } = await addSubscriptionForPersonAndSync(this.realm); - this.realm.subscriptions.update((mutableSubs) => { - mutableSubs.removeSubscription(sub); - }); + this.realm.subscriptions.update((mutableSubs) => { + mutableSubs.removeSubscription(sub); + }); - // Deliberately not waiting for synchronisation here + // Deliberately not waiting for synchronisation here - realm.write(function () { - return realm.create(FlexiblePersonSchema.name, { + this.realm.write(() => { + expect(() => { + this.realm.create(FlexiblePersonSchema.name, { _id: new BSON.ObjectId(), name: "Tom", age: 36, }); - }); + }).to.throw("Cannot write to class Person when no flexible sync subscription has been created."); }); });