Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
papafe committed Apr 19, 2024
1 parent b15b775 commit ac8a270
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 41 deletions.
36 changes: 31 additions & 5 deletions integration-tests/tests/src/hooks/open-realm-before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ export function openRealmHook(config: OpenRealmConfiguration = {}) {
export function closeThisRealm(this: RealmContext & Mocha.Context): void {
if (this.closeRealm) {
this.closeRealm({ clearTestState: true, deleteFile: true });
} else {
// Clearing the test state to ensure the sync session gets completely reset and nothing is cached between tests
Realm.clearTestState();
}
// Clearing the test state to ensure the sync session gets completely reset and nothing is cached between tests
Realm.clearTestState();
}

export function openRealmBeforeEach(config: OpenRealmConfiguration = {}): void {
Expand All @@ -84,8 +85,33 @@ export function openRealmBefore(config: OpenRealmConfiguration = {}): void {
after("closeRealmAfter", closeThisRealm);
}

export function setupRealmHook() {
return async function openRealmHandler(this: UserContext & Mocha.Context): Promise<void> {
this.longTimeout();
export async function setupRealmHook(this: AppContext & MultiRealmContext): Promise<void> {
this.openedInfo = [];

this.getRealm = async (config: OpenRealmConfiguration): Promise<Realm> => {
const user = await this.getUser(Realm.Credentials.anonymous(false));
const realmAndConfig = await openRealm(config, user);
this.openedInfo.push(realmAndConfig);
return realmAndConfig.realm;
};

this.closeAllRealms = async () => {
this.openedInfo?.forEach(({ realm, config }) => {
if (!realm?.isClosed) {
realm.close();
}
Realm.deleteFile(config);
});

Realm.clearTestState();
};
}

export function closeMultiRealms(this: AppContext & MultiRealmContext): void {
this.closeAllRealms?.();
}

export function setupMultiRealmsBeforeAndAfterEach(): void {
beforeEach("openMultiRealmsBeforeEach", setupRealmHook);
afterEach("closeMultiRealmsAfterEach", closeMultiRealms);
}
55 changes: 21 additions & 34 deletions integration-tests/tests/src/tests/sync/mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
import { expect } from "chai";
import Realm, { Credentials, Mixed, ObjectSchema } from "realm";

import { importAppBefore, authenticateUserBefore, openRealmBefore } from "../../hooks";
import {
importAppBefore,
authenticateUserBefore,
openRealmBefore,
setupMultiRealmsBeforeAndAfterEach as setupMultiRealmsBeforeAndAfterEach,
} from "../../hooks";

import { itUploadsDeletesAndDownloads } from "./upload-delete-download";
import { buildAppConfig } from "../../utils/build-app-config";
Expand Down Expand Up @@ -301,30 +306,30 @@ function describeTypes(useFlexibleSync: boolean) {
}

describe.only("mixed synced", () => {
// describe("partition-based sync roundtrip", function () {
// this.longTimeout();
// importAppBefore(buildAppConfig("with-pbs").anonAuth().partitionBasedSync());
// describeTypes(false);
// });

// describe.skipIf(environment.skipFlexibleSync, "flexible sync roundtrip", function () {
// this.longTimeout();
// importAppBefore(buildAppConfig("with-flx").anonAuth().flexibleSync());
// describeTypes(true);
// });
describe("partition-based sync roundtrip", function () {
this.longTimeout();
importAppBefore(buildAppConfig("with-pbs").anonAuth().partitionBasedSync());
describeTypes(false);
});

describe.skipIf(environment.skipFlexibleSync, "flexible sync roundtrip", function () {
this.longTimeout();
importAppBefore(buildAppConfig("with-flx").anonAuth().flexibleSync());
describeTypes(true);
});

describe.skipIf(environment.skipFlexibleSync, "mixed collections", function () {
this.longTimeout();
importAppBefore(buildAppConfig("with-flx").anonAuth().flexibleSync());
setupMultiRealmsBeforeAndAfterEach();

const realmConfig = {
schema: [MixedClass],
sync: { flexible: true },
} satisfies OpenRealmConfiguration;

it("writes", async function (this: Mocha.Context & AppContext & UserContext) {
const user1 = await this.getUser(Credentials.anonymous(false));
const { realm: realm1, config: config1 } = await openRealm(realmConfig, user1);
it("simple test", async function (this: Mocha.Context & AppContext & MultiRealmContext) {
const realm1 = await this.getRealm(realmConfig);
await setupTest(realm1, true); // this adds the subscriptions

const obId = new Realm.BSON.ObjectID();
Expand All @@ -337,8 +342,7 @@ describe.only("mixed synced", () => {

await realm1.syncSession?.uploadAllLocalChanges();

const user2 = await this.getUser(Credentials.anonymous(false));
const { realm: realm2, config: config2 } = await openRealm(realmConfig, user2);
const realm2 = await this.getRealm(realmConfig);
await setupTest(realm2, true);

const obj2 = await new Promise<MixedClass>((resolve) => {
Expand All @@ -353,23 +357,6 @@ describe.only("mixed synced", () => {
});

expect(obj2.value).equals(obj1.value);

//Cleanup
realm1.close();
realm2.close();
Realm.deleteFile(config1);
Realm.deleteFile(config2);
Realm.clearTestState();
});

/***
* What to test
* - changing from one type to another
* - string to list
* - modifying list
* - list to dictionary
* - modifying dictionary
* -
*/
});
});
5 changes: 3 additions & 2 deletions integration-tests/tests/src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ type RealmContext = {
closeRealm(options?: Partial<CloseRealmOptions>): Promise<void>;
} & Mocha.Context;
type MultiRealmContext = {
realms: Realm[];
getRealm: (config: Realm.OpenRealmBehaviorConfiguration) => Promise<Realm>;
openedInfo: { realm: Realm; config: Realm.Configuration }[];
getRealm: (config: any) => Promise<Realm>; //any should be OpenRealmConfiguration
closeAllRealms(): Promise<void>;
} & Mocha.Context;
type RealmObjectContext<T = Record<string, unknown>> = {
object: Realm.Object<T> & T;
Expand Down

0 comments on commit ac8a270

Please sign in to comment.