Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@
"test:watch": "jest --updateSnapshot --watchAll"
},
"types": "dist/index.d.ts",
"version": "2.0.1"
"version": "2.0.2"
}
77 changes: 74 additions & 3 deletions src/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

import isEqual from 'lodash.isequal';

import { GLOBAL_SELECTOR, type Value } from './';
import { DELETE_TAG, GLOBAL_SELECTOR, MOVE_TAG, type Value } from './';

import AccessorCache from './model/accessor-cache';
import { Immutable } from './main';
Expand Down Expand Up @@ -60,6 +60,8 @@ describe( 'Connection class', () => {
const propertyPaths = [ 'valid', 'b.message' ];

let d = connection.get( ...propertyPaths );
jest.runAllTimers();

passedNoneFoundTest =
Object.keys( d ).length === 2
&& d[ 'b.message' ] === undefined
Expand Down Expand Up @@ -94,6 +96,7 @@ describe( 'Connection class', () => {
}

const v = connection.get( ...propertyPaths );
jest.runAllTimers();

passedFoundTest =
Object.keys( v ).length === 2
Expand Down Expand Up @@ -128,15 +131,24 @@ describe( 'Connection class', () => {
const gsData = connection.get(
GLOBAL_SELECTOR, 'a', 'valid'
)[ GLOBAL_SELECTOR ];
jest.runAllTimers();
expect( gsData ).toStrictEqual( changes );
expect( gsData ).not.toBe( changes );
jest.useRealTimers()
jest.useRealTimers();
} );
test( 'fetches the GLOBAL_SELECTOR path by default', () => {
jest.useFakeTimers();
expect( setup( clonedeep( protectedData ) ).connection.get() ).toEqual({[ GLOBAL_SELECTOR ]: protectedData });
jest.runAllTimers();
jest.useRealTimers();
});
} );
describe( 'disconnection', () => {
test(
'discards internally held refs and sets `disconnected` flag',
() => {
jest.useFakeTimers();

const { cache, connection, teardown } = setup();

const cacheGetSpy = jest
Expand All @@ -147,6 +159,7 @@ describe( 'Connection class', () => {
.mockReturnValue( undefined );

connection.get( expect.any( Array ) as unknown as string );
jest.runAllTimers();
expect( cacheGetSpy ).toHaveBeenCalledTimes( 1 );
connection.set( {} );
expect( setSpy ).toHaveBeenCalledTimes( 1 );
Expand All @@ -162,6 +175,8 @@ describe( 'Connection class', () => {
connection.set( {} );
expect( setSpy ).not.toHaveBeenCalled();

jest.useRealTimers();

jest.restoreAllMocks();

teardown();
Expand Down Expand Up @@ -192,6 +207,7 @@ describe( 'Connection class', () => {
);
} );
test( 'runs full fetch on complex paths', () => {
jest.useFakeTimers();
const source = createSourceData();
type Data = typeof source;
const { connection } = setup( source );
Expand All @@ -215,6 +231,7 @@ describe( 'Connection class', () => {
'tags[6]': source.tags[ 6 ],
'@@GLOBAL': source
});
jest.runAllTimers();
connection.set({
isActive: true,
friends: { 1: { name: { last: 'NEW LNAME' } } },
Expand Down Expand Up @@ -242,12 +259,54 @@ describe( 'Connection class', () => {
'tags[6]': source.tags[ 6 ],
'@@GLOBAL': updatedDataEquiv
});
jest.runAllTimers();
jest.useRealTimers();
connection.disconnect();
} );
test( 'handles deep value computed update requests', () => {
jest.useFakeTimers();

const source = createSourceData();
type Data = typeof source;
const { connection } = setup( source );

expect( connection.get() ).toEqual({[ GLOBAL_SELECTOR ]: source });
jest.runAllTimers();

connection.set({
friends: { [ MOVE_TAG ]: [ -1, 1 ] },
isActive: true,
history: {
places: {
'2': {
city: 'Marakesh',
country: 'Morocco'
} as unknown as Data["history"]["places"][0]
} as unknown as Data["history"]["places"]
},
tags: { [ DELETE_TAG ]: [ 3, 5 ] }
} as unknown as Data );
const defaultState = createSourceData();
const expectedValue = { ...defaultState };
expectedValue.friends = [ 0, 2, 1 ].map( i => defaultState.friends[ i ] );
expectedValue.history.places[ 2 ].city = 'Marakesh';
expectedValue.history.places[ 2 ].country = 'Morocco';
expectedValue.isActive = true;
expectedValue.tags = [ 0, 1, 2, 4, 6 ].map( i => defaultState.tags[ i ] );

expect( connection.get() ).toEqual({[ GLOBAL_SELECTOR ]: expectedValue });
jest.runAllTimers();

connection.disconnect();

jest.useRealTimers();

} );
test( 'handles atoms sharing', () => {
jest.useFakeTimers();
const source = createSourceData();
const { connection } = setup( source );
connection.get(
const data1 = connection.get(
'history.places[2].city',
'history.places[2].country',
'history.places[2].year',
Expand All @@ -256,13 +315,24 @@ describe( 'Connection class', () => {
'tags[6]',
'@@GLOBAL'
);
jest.runAllTimers();
const data2 = connection.get(
'friends[1].name.last',
'history.places[2].country',
'history.places[2].year',
'company',
'tags[5]',
);
jest.runAllTimers();
expect( data1 ).toEqual({
'history.places[2].city': source.history.places[ 2 ].city,
'history.places[2].country': source.history.places[ 2 ].country,
'history.places[2].year': source.history.places[ 2 ].year,
'isActive': source.isActive,
'tags[5]': source.tags[ 5 ],
'tags[6]': source.tags[ 6 ],
'@@GLOBAL': source
});
expect( data2 ).toEqual({
'friends[1].name.last': source.friends[ 1 ].name.last,
'history.places[2].country': source.history.places[ 2 ].country,
Expand All @@ -271,5 +341,6 @@ describe( 'Connection class', () => {
'tags[5]': source.tags[ 5 ],
});
connection.disconnect();
jest.useRealTimers();
} );
} );
1 change: 1 addition & 0 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class Connection<T extends Value> {
this._source = undefined;
}

/** @param - defaults to the `GLOBAL_SELECTOR` via the `accessorCache.get` method */
@invoke
get( ...propertyPaths : Array<string> ) {
return this._source.map
Expand Down
16 changes: 7 additions & 9 deletions src/model/accessor-cache/repository/atom-value/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,16 @@ class AtomNode<T extends Value>{
}
return;
}
if( activeNode.isRoot ) {
activeNode.value = set( activeNode.value, fullPath, value ) as T;
return;
}
const nodePathLen = activeNode.fullPath.length;
if( fullPathLen === nodePathLen ) {
activeNode.value = activeNode.isRoot
? set( activeNode.value, fullPath, value ) as T
: value;
if( fullPathLen !== nodePathLen ) {
activeNode.value = set( activeNode.value, fullPath.slice( nodePathLen ), value ) as T;
return;
}
activeNode.value = set(
activeNode.value,
fullPath.slice( nodePathLen ),
value
) as T;
activeNode.value = value;
}

/**
Expand Down