Skip to content

Commit

Permalink
Implement feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
krollins-mdb committed Aug 6, 2024
1 parent 2dae749 commit d569cb8
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 130 deletions.
78 changes: 39 additions & 39 deletions examples/node/v12/__tests__/data-types.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Realm, { UpdateMode } from "realm";

import { ClassWithCounter } from "./models/models.js";
import { SiteVisitTracker } from "./models/models.js";

// This test exists to ensure that the JS model definition works that same way
// as the TS model definition. There are no snippets generated from this file,
Expand All @@ -9,89 +9,89 @@ import { ClassWithCounter } from "./models/models.js";
describe("Counter Updates", () => {
test("testing normal methods", async () => {
const realm = await Realm.open({
schema: [ClassWithCounter],
schema: [SiteVisitTracker],
});

const myObject = realm.write(() => {
return realm.create(ClassWithCounter, { myCounter: 0 });
const siteVisitTracker = realm.write(() => {
return realm.create(SiteVisitTracker, { siteVisits: 0 });
});

realm.write(() => {
myObject.myCounter.increment();
myObject.myCounter.value; // 1
expect(myObject.myCounter.value).toBe(1);
myObject.myCounter.increment(2);
myObject.myCounter.value; // 3
expect(myObject.myCounter.value).toBe(3);
myObject.myCounter.decrement(2);
myObject.myCounter.value; // 1
expect(myObject.myCounter.value).toBe(1);
myObject.myCounter.increment(-2);
myObject.myCounter.value; // -1
expect(myObject.myCounter.value).toBe(-1);
myObject.myCounter.set(0); // reset counter value to 0

expect(myObject.myCounter.value).toBe(0);
siteVisitTracker.siteVisits.increment();
siteVisitTracker.siteVisits.value; // 1
expect(siteVisitTracker.siteVisits.value).toBe(1);
siteVisitTracker.siteVisits.increment(2);
siteVisitTracker.siteVisits.value; // 3
expect(siteVisitTracker.siteVisits.value).toBe(3);
siteVisitTracker.siteVisits.decrement(2);
siteVisitTracker.siteVisits.value; // 1
expect(siteVisitTracker.siteVisits.value).toBe(1);
siteVisitTracker.siteVisits.increment(-2);
siteVisitTracker.siteVisits.value; // -1
expect(siteVisitTracker.siteVisits.value).toBe(-1);
siteVisitTracker.siteVisits.set(0); // reset counter value to 0

expect(siteVisitTracker.siteVisits.value).toBe(0);
});
});

test("testing nullability switching", async () => {
const realm = await Realm.open({
schema: [ClassWithCounter],
schema: [SiteVisitTracker],
});

const myObject = realm.write(() => {
return realm.create(ClassWithCounter, {
nullableCounter: 0,
myCounter: 1,
const siteVisitTracker = realm.write(() => {
return realm.create(SiteVisitTracker, {
nullableSiteVisits: 0,
siteVisits: 1,
});
});

const myID = myObject._id;
expect(myObject.nullableCounter?.value).toBe(0);
const myID = siteVisitTracker._id;
expect(siteVisitTracker.nullableSiteVisits?.value).toBe(0);

realm.write(() => {
realm.create(
ClassWithCounter,
{ _id: myID, nullableCounter: null },
SiteVisitTracker,
{ _id: myID, nullableSiteVisits: null },
UpdateMode.Modified
);
});
expect(myObject.nullableCounter).toBe(null);
expect(siteVisitTracker.nullableSiteVisits).toBe(null);

realm.write(() => {
realm.create(
ClassWithCounter,
{ _id: myID, nullableCounter: 0 },
SiteVisitTracker,
{ _id: myID, nullableSiteVisits: 0 },
UpdateMode.Modified
);
});

expect(myObject.nullableCounter?.value).toBe(0);
expect(siteVisitTracker.nullableSiteVisits?.value).toBe(0);
});

test("testing filtering", async () => {
const realm = await Realm.open({
schema: [ClassWithCounter],
schema: [SiteVisitTracker],
});

const belowThreshold = realm.write(() => {
return realm.create(ClassWithCounter, { myCounter: 0 });
return realm.create(SiteVisitTracker, { siteVisits: 0 });
});

const atThreshold = realm.write(() => {
return realm.create(ClassWithCounter, { myCounter: 1 });
return realm.create(SiteVisitTracker, { siteVisits: 1 });
});

const aboveThreshold = realm.write(() => {
return realm.create(ClassWithCounter, { myCounter: 2 });
return realm.create(SiteVisitTracker, { siteVisits: 2 });
});

const allObjects = realm.objects("ClassWithCounter");
const allObjects = realm.objects("SiteVisitTracker");

let filteredObjects = allObjects.filtered(
"myCounter >= $0",
atThreshold.myCounter.value
"siteVisits >= $0",
atThreshold.siteVisits.value
);

let unfilteredExpected = [belowThreshold, atThreshold, aboveThreshold];
Expand Down
76 changes: 38 additions & 38 deletions examples/node/v12/__tests__/data-types.test.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,99 @@
import Realm, { UpdateMode } from "realm";

import { ClassWithCounter } from "./models/models.ts";
import { SiteVisitTracker } from "./models/models.ts";

describe("Counter Updates", () => {
test("testing normal methods", async () => {
const realm = await Realm.open({
schema: [ClassWithCounter],
schema: [SiteVisitTracker],
});

// :snippet-start:initialize-counter
const myObject = realm.write(() => {
return realm.create(ClassWithCounter, { myCounter: 0 });
const siteVisitTracker = realm.write(() => {
return realm.create(SiteVisitTracker, { siteVisits: 0 });
});
// :snippet-end:

realm.write(() => {
// :snippet-start:update-counter
myObject.myCounter.increment();
myObject.myCounter.value; // 1
expect(myObject.myCounter.value).toBe(1); // :remove:
myObject.myCounter.increment(2);
myObject.myCounter.value; // 3
expect(myObject.myCounter.value).toBe(3); // :remove:
myObject.myCounter.decrement(2);
myObject.myCounter.value; // 1
expect(myObject.myCounter.value).toBe(1); // :remove:
myObject.myCounter.increment(-2);
myObject.myCounter.value; // -1
expect(myObject.myCounter.value).toBe(-1); // :remove:
myObject.myCounter.set(0); // reset counter value to 0
siteVisitTracker.siteVisits.increment();
siteVisitTracker.siteVisits.value; // 1
expect(siteVisitTracker.siteVisits.value).toBe(1); // :remove:
siteVisitTracker.siteVisits.increment(2);
siteVisitTracker.siteVisits.value; // 3
expect(siteVisitTracker.siteVisits.value).toBe(3); // :remove:
siteVisitTracker.siteVisits.decrement(2);
siteVisitTracker.siteVisits.value; // 1
expect(siteVisitTracker.siteVisits.value).toBe(1); // :remove:
siteVisitTracker.siteVisits.increment(-2);
siteVisitTracker.siteVisits.value; // -1
expect(siteVisitTracker.siteVisits.value).toBe(-1); // :remove:
siteVisitTracker.siteVisits.set(0); // reset counter value to 0
// :snippet-end:
expect(myObject.myCounter.value).toBe(0);
expect(siteVisitTracker.siteVisits.value).toBe(0);
});
});

test("testing nullability switching", async () => {
const realm = await Realm.open({
schema: [ClassWithCounter],
schema: [SiteVisitTracker],
});

// :snippet-start:null-updating
const myObject = realm.write(() => {
return realm.create(ClassWithCounter, {
nullableCounter: 0,
myCounter: 1,
const siteVisitTracker = realm.write(() => {
return realm.create(SiteVisitTracker, {
nullableSiteVisits: 0,
siteVisits: 1,
});
});

const myID = myObject._id;
expect(myObject.nullableCounter?.value).toBe(0); // :remove:
const myID = siteVisitTracker._id;
expect(siteVisitTracker.nullableSiteVisits?.value).toBe(0); // :remove:

realm.write(() => {
realm.create(
ClassWithCounter,
{ _id: myID, nullableCounter: null },
SiteVisitTracker,
{ _id: myID, nullableSiteVisits: null },
UpdateMode.Modified
);
});
expect(myObject.nullableCounter).toBe(null); // :remove:
expect(siteVisitTracker.nullableSiteVisits).toBe(null); // :remove:

realm.write(() => {
realm.create(
ClassWithCounter,
{ _id: myID, nullableCounter: 0 },
SiteVisitTracker,
{ _id: myID, nullableSiteVisits: 0 },
UpdateMode.Modified
);
});
// :snippet-end:

expect(myObject.nullableCounter?.value).toBe(0);
expect(siteVisitTracker.nullableSiteVisits?.value).toBe(0);
});

test("testing filtering", async () => {
const realm = await Realm.open({
schema: [ClassWithCounter],
schema: [SiteVisitTracker],
});

// :snippet-start:filtering-with-counter
const belowThreshold = realm.write(() => {
return realm.create(ClassWithCounter, { myCounter: 0 });
return realm.create(SiteVisitTracker, { siteVisits: 0 });
});

const atThreshold = realm.write(() => {
return realm.create(ClassWithCounter, { myCounter: 1 });
return realm.create(SiteVisitTracker, { siteVisits: 1 });
});

const aboveThreshold = realm.write(() => {
return realm.create(ClassWithCounter, { myCounter: 2 });
return realm.create(SiteVisitTracker, { siteVisits: 2 });
});

const allObjects = realm.objects("ClassWithCounter");
const allObjects = realm.objects("SiteVisitTracker");

let filteredObjects = allObjects.filtered(
"myCounter >= $0",
atThreshold.myCounter.value
"siteVisits >= $0",
atThreshold.siteVisits.value
);
// :snippet-end:

Expand Down
16 changes: 10 additions & 6 deletions examples/node/v12/__tests__/models/models.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import Realm, { BSON } from "realm";

// :snippet-start: declare-counter-schema
export class ClassWithCounter extends Realm.Object {
export class SiteVisitTracker extends Realm.Object {
static schema = {
name: "ClassWithCounter",
name: "SiteVisitTracker",
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
myCounter: { type: "int", presentation: "counter" },
// or myCounter: "counter"
nullableCounter: { type: "int", presentation: "counter", optional: true },
// or nullableCounter: "counter?"
siteVisits: { type: "int", presentation: "counter" },
// or siteVisits: "counter"
nullableSiteVisits: {
type: "int",
presentation: "counter",
optional: true,
},
// or nullableSiteVisits: "counter?"
},
};
}
Expand Down
20 changes: 12 additions & 8 deletions examples/node/v12/__tests__/models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ export class Task extends Realm.Object<Task> {
}

// :snippet-start: declare-counter-schema
export class ClassWithCounter extends Realm.Object<ClassWithCounter> {
export class SiteVisitTracker extends Realm.Object<SiteVisitTracker> {
_id!: BSON.ObjectId;
myCounter!: Counter;
nullableCounter?: Counter | null;
siteVisits!: Counter;
nullableSiteVisits?: Counter | null;

static schema: ObjectSchema = {
name: "ClassWithCounter",
name: "SiteVisitTracker",
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
myCounter: { type: "int", presentation: "counter" },
// or myCounter: "counter"
nullableCounter: { type: "int", presentation: "counter", optional: true },
// or nullableCounter: "counter?"
siteVisits: { type: "int", presentation: "counter" },
// or siteVisits: "counter"
nullableSiteVisits: {
type: "int",
presentation: "counter",
optional: true,
},
// or nullableSiteVisits: "counter?"
},
};
}
Expand Down
29 changes: 23 additions & 6 deletions examples/node/v12/__tests__/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// :replace-start: {
// "terms": {
// "js-flexible-oseso": "<yourAppId>"
// }
// }

import Realm, { BSON } from "realm";
import { QuickstartTask } from "./models/models.ts";

Expand Down Expand Up @@ -146,23 +152,32 @@ describe("QuickStart Local", () => {
describe("Quickstart Sync", () => {
test("should open a Flexible Sync realm with initial subscriptions", async () => {
// :snippet-start: open-realm-with-subscriptions
// :replace-start: {
// "terms": {
// "js-flexible-oseso": "<yourAppId>"
// }
// }
// Initialize your App.
const app = new Realm.App({
id: "js-flexible-oseso",
});
// :replace-end:
expect(app).toBeTruthy(); // :remove:
expect(app.id).toBe("js-flexible-oseso"); // :remove:

// Authenticate an anonymous user.
const anonymousUser = await app.logIn(Realm.Credentials.anonymous());
expect(app.currentUser).toBeTruthy(); // :remove:

// Add a test object throught the MongoDB client so that we have something
// to look for with initial subscriptions.
const testTask = {
_id: new BSON.ObjectID(),
name: "go grocery shopping",
status: "Open",
owner_id: anonymousUser.id,
};
const mongodb = app.currentUser?.mongoClient("mongodb-atlas");
const tasks = mongodb?.db("JSFlexibleSyncDB").collection("Task");

if (tasks) {
await tasks.insertOne(testTask);
}

// Create a `SyncConfiguration` object.
const config = {
schema: [QuickstartTask],
Expand Down Expand Up @@ -201,3 +216,5 @@ describe("Quickstart Sync", () => {

expect.hasAssertions();
});

// :replace-end:
Loading

0 comments on commit d569cb8

Please sign in to comment.