Skip to content

Commit

Permalink
Added new tests
Browse files Browse the repository at this point in the history
to validate behavior with proxified state. Also included more descriptive error messages for re-proxification attempts.
  • Loading branch information
michalczaplinski committed Sep 17, 2024
1 parent 2392742 commit d11f743
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/interactivity/src/proxies/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const contextHandlers: ProxyHandler< object > = {
},
set: ( target, key, value ) => {
const fallback = contextObjectToFallback.get( target );

// If the property exists in the current context, modify it. Otherwise,
// add it to the current context.
const obj = key in target || ! ( key in fallback ) ? target : fallback;
obj[ key ] = value;

Expand Down
23 changes: 22 additions & 1 deletion packages/interactivity/src/proxies/test/context-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ describe( 'Interactivity API', () => {
expect( 'a' in context.obj ).toBe( false );
expect( context.obj.b ).toBe( 2 );
} );

it( 'should work with the proxified state', () => {
const state = proxifyState( 'test', { a: 1 } );
const fallback: any = proxifyContext( state, {} );
const context: any = proxifyContext( state, fallback );

expect( context.a ).toBe( 1 );
} );
} );

describe( 'set', () => {
Expand Down Expand Up @@ -118,6 +126,17 @@ describe( 'Interactivity API', () => {
expect( context.prop ).toBe( 'modified' );
expect( fallback.prop ).toBeUndefined();
} );

it( 'should work with the proxified state', () => {
const state = proxifyState( 'test', { a: 1 } );
const fallback: any = proxifyContext( state, {} );
const context: any = proxifyContext( {}, fallback );

context.a = 2;

expect( context.a ).toBe( 2 );
expect( state.a ).toBe( 2 );
} );
} );

describe( 'computations', () => {
Expand Down Expand Up @@ -263,7 +282,9 @@ describe( 'Interactivity API', () => {
describe( 'proxifyContext', () => {
it( 'should throw when trying to re-proxify a proxy object', () => {
const context = proxifyContext( {}, {} );
expect( () => proxifyContext( context, {} ) ).toThrow();
expect( () => proxifyContext( context, {} ) ).toThrow(
'This object cannot be proxified.'
);
} );
} );
} );
Expand Down

0 comments on commit d11f743

Please sign in to comment.