-
Notifications
You must be signed in to change notification settings - Fork 844
Implement the data model for web renderers v0.9 #606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jacobsimionato
wants to merge
9
commits into
google:main
Choose a base branch
from
jacobsimionato:implement-data-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6ab973e
Add gemignore
jacobsimionato d32c5e3
Add data model
jacobsimionato 2373b3c
Merge branch 'main' into implement-data-model
jacobsimionato a473089
Try to fix CI
jacobsimionato 1e2e678
fix: Address PR feedback
jacobsimionato 2fc9919
Revert standard catalog change
jacobsimionato 758aa1a
fix: Address PR feedback
jacobsimionato 4d6c1df
fix: Address PR feedback
jacobsimionato 89ebdc6
Fix standard catalog def
jacobsimionato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,203 @@ | ||
|
|
||
| import assert from 'node:assert'; | ||
| import { test, describe, it, beforeEach } from 'node:test'; | ||
| import { DataModel } from './data-model.js'; | ||
|
|
||
| describe('DataModel', () => { | ||
| let model: DataModel; | ||
|
|
||
| beforeEach(() => { | ||
| model = new DataModel({ | ||
| user: { | ||
| name: 'Alice', | ||
| settings: { | ||
| theme: 'dark' | ||
| } | ||
| }, | ||
| items: ['a', 'b', 'c'] | ||
| }); | ||
| }); | ||
|
|
||
| // --- Basic Retrieval --- | ||
|
|
||
| it('retrieves root data', () => { | ||
| assert.deepStrictEqual(model.get('/'), { user: { name: 'Alice', settings: { theme: 'dark' } }, items: ['a', 'b', 'c'] }); | ||
| }); | ||
|
|
||
| it('retrieves nested path', () => { | ||
| assert.strictEqual(model.get('/user/name'), 'Alice'); | ||
| assert.strictEqual(model.get('/user/settings/theme'), 'dark'); | ||
| }); | ||
|
|
||
| it('retrieves array items', () => { | ||
| assert.strictEqual(model.get('/items/0'), 'a'); | ||
| assert.strictEqual(model.get('/items/1'), 'b'); | ||
| }); | ||
|
|
||
| it('returns undefined for non-existent paths', () => { | ||
| assert.strictEqual(model.get('/user/age'), undefined); | ||
| assert.strictEqual(model.get('/unknown/path'), undefined); | ||
| }); | ||
|
|
||
| // --- Updates --- | ||
|
|
||
| it('sets value at existing path', () => { | ||
| model.set('/user/name', 'Bob'); | ||
| assert.strictEqual(model.get('/user/name'), 'Bob'); | ||
| }); | ||
|
|
||
| it('sets value at new path', () => { | ||
| model.set('/user/age', 30); | ||
| assert.strictEqual(model.get('/user/age'), 30); | ||
| }); | ||
|
|
||
| it('creates intermediate objects', () => { | ||
| model.set('/a/b/c', 'foo'); | ||
| assert.strictEqual(model.get('/a/b/c'), 'foo'); | ||
| assert.notStrictEqual(model.get('/a/b'), undefined); | ||
| }); | ||
|
|
||
| it('removes keys when value is undefined', () => { | ||
| model.set('/user/name', undefined); | ||
| assert.strictEqual(model.get('/user/name'), undefined); | ||
| assert.strictEqual(Object.keys(model.get('/user')).includes('name'), false); | ||
| }); | ||
|
|
||
| it('replaces root object on root update', () => { | ||
| model.set('/', { newRoot: true }); | ||
| assert.deepStrictEqual(model.get('/'), { newRoot: true }); | ||
| }); | ||
|
|
||
| // --- Array / List Handling (Flutter Parity) --- | ||
|
|
||
| it('List: set and get', () => { | ||
| model.set('/list/0', 'hello'); | ||
| assert.strictEqual(model.get('/list/0'), 'hello'); | ||
| assert.ok(Array.isArray(model.get('/list'))); | ||
| }); | ||
|
|
||
| it('List: append and get', () => { | ||
| model.set('/list/0', 'hello'); | ||
| model.set('/list/1', 'world'); | ||
| assert.strictEqual(model.get('/list/0'), 'hello'); | ||
| assert.strictEqual(model.get('/list/1'), 'world'); | ||
| assert.strictEqual(model.get('/list').length, 2); | ||
| }); | ||
|
|
||
| it('List: update existing index', () => { | ||
| model.set('/items/1', 'updated'); | ||
| assert.strictEqual(model.get('/items/1'), 'updated'); | ||
| }); | ||
|
|
||
| it('Nested structures are created automatically', () => { | ||
| // Should create nested map and list: { a: { b: [ { c: 123 } ] } } | ||
| model.set('/a/b/0/c', 123); | ||
| assert.strictEqual(model.get('/a/b/0/c'), 123); | ||
| assert.ok(Array.isArray(model.get('/a/b'))); | ||
| assert.ok(!Array.isArray(model.get('/a/b/0'))); | ||
|
|
||
| // Should create nested maps | ||
| model.set('/x/y/z', 'hello'); | ||
| assert.strictEqual(model.get('/x/y/z'), 'hello'); | ||
|
|
||
| // Should create nested lists | ||
| model.set('/nestedList/0/0', 'inner'); | ||
| assert.strictEqual(model.get('/nestedList/0/0'), 'inner'); | ||
| assert.ok(Array.isArray(model.get('/nestedList'))); | ||
| assert.ok(Array.isArray(model.get('/nestedList/0'))); | ||
| }); | ||
|
|
||
| // --- Subscriptions --- | ||
|
|
||
| it('returns a subscription object', () => { | ||
| model.set('/a', 1); | ||
| const sub = model.subscribe<number>('/a'); | ||
| assert.strictEqual(sub.value, 1); | ||
|
|
||
| let updatedValue: number | undefined; | ||
| sub.onChange = (val) => updatedValue = val; | ||
|
|
||
| model.set('/a', 2); | ||
| assert.strictEqual(sub.value, 2); | ||
| assert.strictEqual(updatedValue, 2); | ||
|
|
||
| sub.unsubscribe(); | ||
| // Verify listener removed | ||
| model.set('/a', 3); | ||
| assert.strictEqual(updatedValue, 2); | ||
| }); | ||
|
|
||
| it('notifies subscribers on exact match', (_: any, done: (result?: any) => void) => { | ||
| const sub = model.subscribe('/user/name'); | ||
| sub.onChange = (val) => { | ||
| assert.strictEqual(val, 'Charlie'); | ||
| done(); | ||
| }; | ||
| model.set('/user/name', 'Charlie'); | ||
| }); | ||
|
|
||
| it('notifies ancestor subscribers (Container Semantics)', (_: any, done: (result?: any) => void) => { | ||
| const sub = model.subscribe('/user'); | ||
| sub.onChange = (val: any) => { | ||
| assert.strictEqual(val.name, 'Dave'); | ||
| done(); | ||
| }; | ||
| model.set('/user/name', 'Dave'); | ||
| }); | ||
|
|
||
| it('notifies descendant subscribers', (_: any, done: (result?: any) => void) => { | ||
| const sub = model.subscribe('/user/settings/theme'); | ||
| sub.onChange = (val) => { | ||
| assert.strictEqual(val, 'light'); | ||
| done(); | ||
| }; | ||
|
|
||
| // We update the parent object | ||
| model.set('/user/settings', { theme: 'light' }); | ||
| }); | ||
|
|
||
| it('notifies root subscriber', (_: any, done: (result?: any) => void) => { | ||
| const sub = model.subscribe('/'); | ||
| sub.onChange = (val: any) => { | ||
| assert.strictEqual(val.newProp, 'test'); | ||
| done(); | ||
| }; | ||
| model.set('/newProp', 'test'); | ||
| }); | ||
|
|
||
| it('notifies parent when child updates', () => { | ||
| model.set('/parent', { child: 'initial' }); | ||
|
|
||
| const sub = model.subscribe('/parent'); | ||
| let parentValue: any; | ||
| sub.onChange = (val) => parentValue = val; | ||
|
|
||
| model.set('/parent/child', 'updated'); | ||
| assert.deepStrictEqual(parentValue, { child: 'updated' }); | ||
| }); | ||
|
|
||
| it('stops notifying after dispose', () => { | ||
| let count = 0; | ||
| const sub = model.subscribe('/'); | ||
| sub.onChange = () => count++; | ||
|
|
||
| model.dispose(); | ||
| model.set('/foo', 'bar'); | ||
| assert.strictEqual(count, 0); | ||
| }); | ||
|
|
||
| it('throws when trying to set nested property through a primitive', () => { | ||
| model.set('/user/name', 'not an object'); | ||
| assert.strictEqual(model.get('/user/name'), 'not an object'); | ||
|
|
||
| assert.throws(() => { | ||
| model.set('/user/name/first', 'Alice'); | ||
| }, /Cannot set path/); | ||
| }); | ||
|
|
||
| it('throws when using non-numeric segment on an array', () => { | ||
| assert.throws(() => { | ||
| model.set('/items/foo', 'bar'); | ||
| }, /Cannot use non-numeric segment/); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great set of tests! To make it even more robust, I recommend adding a test case to verify the behavior of
setwhen a path segment contains a primitive value. This will ensure the fix for the issue I mentioned indata-model.tsis covered and prevent regressions.Here's a suggested test case:
References
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test case for this, but verify failure as explained above.