-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
libs/ngrx-entity-relationship/e2e/custom-keys-for-entities.spec.ts
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,85 @@ | ||
import { | ||
ngrxEntityRelationshipReducer, | ||
reduceGraph, | ||
relatedEntitySelector, | ||
rootEntitySelector, | ||
stateKeys, | ||
} from 'ngrx-entity-relationship'; | ||
|
||
describe('custom-keys-for-entities', () => { | ||
interface Item { | ||
id: string; | ||
name: string; | ||
parentId?: string; | ||
parent?: Item; | ||
} | ||
|
||
const state: { | ||
records: { | ||
ids: Array<string>; | ||
byId: Record<string, Item | undefined>; | ||
}; | ||
} = { | ||
records: { | ||
ids: [], | ||
byId: {}, | ||
}, | ||
}; | ||
|
||
const selectItemsState = (s: typeof state) => s.records; | ||
const featureSelector = stateKeys(selectItemsState, 'byId'); | ||
|
||
const sItem = rootEntitySelector(featureSelector); | ||
const sItemParent = relatedEntitySelector(featureSelector, 'parentId', 'parent'); | ||
|
||
const selector = sItem(sItemParent()); | ||
|
||
it('handles custom values correctly', () => { | ||
const action = reduceGraph({ | ||
data: [ | ||
{ | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
parent: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}, | ||
], | ||
selector, | ||
}); | ||
|
||
const reducer = ngrxEntityRelationshipReducer<typeof state>(((s: typeof state) => s) as any); | ||
const testingState = reducer(state, action); | ||
|
||
// data has been reduced. | ||
expect(testingState).toEqual({ | ||
records: { | ||
ids: ['i1', 'i2'], | ||
byId: { | ||
i1: { | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
}, | ||
i2: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// data has been reached. | ||
expect(selector(testingState, 'i1')).toEqual({ | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
parent: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}); | ||
}); | ||
}); |
82 changes: 82 additions & 0 deletions
82
libs/ngrx-entity-relationship/e2e/custom-keys-no-id.spec.ts
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,82 @@ | ||
import { | ||
ngrxEntityRelationshipReducer, | ||
reduceGraph, | ||
relatedEntitySelector, | ||
rootEntitySelector, | ||
stateKeys, | ||
} from 'ngrx-entity-relationship'; | ||
|
||
describe('custom-keys-no-id', () => { | ||
interface Item { | ||
id: string; | ||
name: string; | ||
parentId?: string; | ||
parent?: Item; | ||
} | ||
|
||
const state: { | ||
records: { | ||
byId: Record<string, Item | undefined>; | ||
}; | ||
} = { | ||
records: { | ||
byId: {}, | ||
}, | ||
}; | ||
|
||
const selectItemsState = (s: typeof state) => s.records; | ||
const featureSelector = stateKeys(selectItemsState, 'byId'); | ||
|
||
const sItem = rootEntitySelector(featureSelector); | ||
const sItemParent = relatedEntitySelector(featureSelector, 'parentId', 'parent'); | ||
|
||
const selector = sItem(sItemParent()); | ||
|
||
it('handles custom values correctly', () => { | ||
const action = reduceGraph({ | ||
data: [ | ||
{ | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
parent: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}, | ||
], | ||
selector, | ||
}); | ||
|
||
const reducer = ngrxEntityRelationshipReducer<typeof state>(((s: typeof state) => s) as any); | ||
const testingState = reducer(state, action); | ||
|
||
// data has been reduced. | ||
expect(testingState).toEqual({ | ||
records: { | ||
byId: { | ||
i1: { | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
}, | ||
i2: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// data has been reached. | ||
expect(selector(testingState, 'i1')).toEqual({ | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
parent: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}); | ||
}); | ||
}); |
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