-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed serialization of sets
- Loading branch information
Showing
7 changed files
with
56 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ObjectModel, { initObject, Plugin } from "../src"; | ||
import { fullModel } from "./update"; | ||
|
||
test("clone", () => { | ||
const model = new ObjectModel(); | ||
model.update(fullModel); | ||
model.plugins.set("Test", initObject(Plugin, { id: "Test", name: "Test Plugin" })); | ||
|
||
const serializedFullModel = JSON.stringify(model); | ||
const secondModel = new ObjectModel(); | ||
secondModel.update(JSON.parse(serializedFullModel)); | ||
expect(JSON.stringify(secondModel)).toBe(serializedFullModel); | ||
|
||
expect(secondModel.plugins.size).toBe(1); | ||
expect(secondModel.plugins.get("Test")).toBeInstanceOf(Plugin); | ||
expect(secondModel.plugins.get("Test")?.name).toBe("Test Plugin"); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Wrapper around a standard set to make sure it serializes to an array | ||
*/ | ||
export default class ModelSet<T> extends Set<T> { | ||
/** | ||
* Convert this object to JSON | ||
* @returns JSON object | ||
*/ | ||
toJSON() { return Array.from(this); } | ||
} |
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