Skip to content

Commit

Permalink
Fix a slew of Node.js tests and regenerate some snippets (#3352)
Browse files Browse the repository at this point in the history
## Pull Request Info - SDK Docs Consolidation

This PR mostly fixes fixes tests, but made some snippet changes for a
couple of pages too.

-
[Counters](https://deploy-preview-3352--device-sdk.netlify.app/sdk/node/model-data/data-types/counters/)
-
[Quickstart](https://deploy-preview-3352--device-sdk.netlify.app/sdk/node/quick-start/)

### Release Notes

- Mostly internal: Node SDK
  - Fixed failing tests
  • Loading branch information
krollins-mdb authored Aug 6, 2024
1 parent f7ef092 commit be7913a
Show file tree
Hide file tree
Showing 35 changed files with 1,072 additions and 531 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ describe("Configure & Open a Synced Realm", () => {
});
});

// Make sure realm is empty
realm.write(() => {
realm.deleteAll();
});

const dogs = realm.objects(Doggie);

await realm.syncSession?.downloadAllServerChanges();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Realm, { BSON, Credentials, ObjectSchema } from "realm";
import { APP_ID } from "../config";
import { APP_ID } from "../config.ts";

describe("Configure & Open a Synced Realm", () => {
test("test cancelWaitsOnNonFatalError is valid config", async () => {
Expand Down Expand Up @@ -65,6 +65,11 @@ describe("Configure & Open a Synced Realm", () => {
});
});

// Make sure realm is empty
realm.write(() => {
realm.deleteAll();
});

const dogs = realm.objects<Doggie>(Doggie);

// :snippet-start: wait-for-download
Expand Down
178 changes: 88 additions & 90 deletions examples/node/v12/__tests__/data-types.test.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,107 @@
import Realm, { BSON, Counter, ObjectSchema, UpdateMode } from "realm";

// :snippet-start:declare-counter-schema
class myClass extends Realm.Object {
_id!;
myCounter!;
nullableCounter;

static schema = {
name: "myClass",
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?"
},
}
};
// :snippet-end:

const realm = await Realm.open({
schema: [myClass],
});
import Realm, { UpdateMode } from "realm";

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,
// as it's the exact same code as what's in the TS file.

describe("Counter Updates", () => {
test("testing normal methods", async () => {

// :snippet-start:initialize-counter
const myObject = realm.write(() => {
return realm.create( myClass, { myCounter: 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
// :snippet-end:
expect(myObject.myCounter.value).toBe(0)
});

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

test("testing nullability switching", async () => {

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

const myID = myObject._id
expect(myObject.nullableCounter?.value).toBe(0) // :remove:
realm.write(() => {
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);
});
});

realm.write(() => {
realm.create( myClass, { _id: myID, nullableCounter: null }, UpdateMode.Modified );
});
expect(myObject.nullableCounter).toBe(null) // :remove:
test("testing nullability switching", async () => {
const realm = await Realm.open({
schema: [SiteVisitTracker],
});

realm.write(() => {
realm.create( myClass, { _id: myID, nullableCounter: 0 }, UpdateMode.Modified );
});
// :snippet-end:
const siteVisitTracker = realm.write(() => {
return realm.create(SiteVisitTracker, {
nullableSiteVisits: 0,
siteVisits: 1,
});
});

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

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

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

test("testing filtering", async () => {
expect(siteVisitTracker.nullableSiteVisits?.value).toBe(0);
});

// :snippet-start:filtering-with-counter
const belowThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 0 } );
});
test("testing filtering", async () => {
const realm = await Realm.open({
schema: [SiteVisitTracker],
});

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

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

const allObjects = realm.objects('myClass');
const aboveThreshold = realm.write(() => {
return realm.create(SiteVisitTracker, { siteVisits: 2 });
});

let filteredObjects = allObjects.filtered('counter >= $0', atThreshold.myCounter.value);
// :snippet-end:
const allObjects = realm.objects("SiteVisitTracker");

let unfilteredExpected = [belowThreshold, atThreshold, aboveThreshold]
let filteredExpected = [atThreshold, aboveThreshold]
let filteredObjects = allObjects.filtered(
"siteVisits >= $0",
atThreshold.siteVisits.value
);

expect(JSON.stringify(allObjects)).toMatch(JSON.stringify(unfilteredExpected));
expect(JSON.stringify(filteredObjects)).toMatch(JSON.stringify(filteredExpected));
let unfilteredExpected = [belowThreshold, atThreshold, aboveThreshold];
let filteredExpected = [atThreshold, aboveThreshold];

});
});
expect(JSON.stringify(allObjects)).toMatch(
JSON.stringify(unfilteredExpected)
);
expect(JSON.stringify(filteredObjects)).toMatch(
JSON.stringify(filteredExpected)
);
});
});
Loading

0 comments on commit be7913a

Please sign in to comment.