-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a slew of Node.js tests and regenerate some snippets (#3352)
## 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
1 parent
f7ef092
commit be7913a
Showing
35 changed files
with
1,072 additions
and
531 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
}); | ||
}); |
Oops, something went wrong.