forked from angular-architects/ngrx-toolkit
-
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.
feat(devtools): add
withMapper
feature
`withMapper` allows mapping the state before it is sent to the DevTools.
- Loading branch information
1 parent
9212013
commit 97c0031
Showing
11 changed files
with
141 additions
and
20 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
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
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 |
---|---|---|
@@ -1,20 +1,28 @@ | ||
export const DEVTOOLS_FEATURE = Symbol('DEVTOOLS_FEATURE'); | ||
|
||
export type Mapper = (state: object) => object; | ||
|
||
export type DevtoolsOptions = { | ||
indexNames: boolean; // defines if names should be indexed. | ||
map: Mapper; // defines a mapper for the state. | ||
}; | ||
|
||
/** | ||
* A DevtoolsFeature adds or modifies the behavior of the | ||
* devtools extension. | ||
* | ||
* We use them (function calls) instead of a config object, | ||
* because of tree-shaking. | ||
*/ | ||
export interface DevtoolsFeature { | ||
export type DevtoolsFeature = { | ||
[DEVTOOLS_FEATURE]: true; | ||
indexNames: boolean | undefined; // defines if names should be indexed. | ||
} | ||
} & Partial<DevtoolsOptions>; | ||
|
||
export function createDevtoolsFeature(indexNames = true): DevtoolsFeature { | ||
export function createDevtoolsFeature( | ||
options: Partial<DevtoolsOptions> | ||
): DevtoolsFeature { | ||
return { | ||
[DEVTOOLS_FEATURE]: true, | ||
indexNames, | ||
...options, | ||
}; | ||
} |
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
69 changes: 69 additions & 0 deletions
69
libs/ngrx-toolkit/src/lib/devtools/tests/with-mapper.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,69 @@ | ||
import { setupExtensions } from './helpers.spec'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { signalStore, withState } from '@ngrx/signals'; | ||
import { withMapper } from '../with-mapper'; | ||
import { withDevtools } from '../with-devtools'; | ||
|
||
function domRemover(state: Record<string, unknown>) { | ||
return Object.keys(state).reduce((acc, key) => { | ||
const value = state[key]; | ||
|
||
if (value instanceof HTMLElement) { | ||
return acc; | ||
} else { | ||
return { ...acc, [key]: value }; | ||
} | ||
}, {}); | ||
} | ||
|
||
describe('with-mapper', () => { | ||
it('should remove DOM Nodes', () => { | ||
const { sendSpy } = setupExtensions(); | ||
|
||
const Store = signalStore( | ||
{ providedIn: 'root' }, | ||
withState({ | ||
name: 'Car', | ||
carElement: document.createElement('div'), | ||
}), | ||
withDevtools('shop', withMapper(domRemover)) | ||
); | ||
|
||
TestBed.inject(Store); | ||
TestBed.flushEffects(); | ||
expect(sendSpy).toHaveBeenCalledWith( | ||
{ type: 'Store Update' }, | ||
{ shop: { name: 'Car' } } | ||
); | ||
}); | ||
|
||
it('should every property ending with *Key', () => { | ||
const { sendSpy } = setupExtensions(); | ||
const Store = signalStore( | ||
{ providedIn: 'root' }, | ||
withState({ | ||
name: 'Car', | ||
unlockKey: '1234', | ||
}), | ||
withDevtools( | ||
'shop', | ||
withMapper((state: Record<string, unknown>) => | ||
Object.keys(state).reduce((acc, key) => { | ||
if (key.endsWith('Key')) { | ||
return acc; | ||
} else { | ||
return { ...acc, [key]: state[key] }; | ||
} | ||
}, {}) | ||
) | ||
) | ||
); | ||
|
||
TestBed.inject(Store); | ||
TestBed.flushEffects(); | ||
expect(sendSpy).toHaveBeenCalledWith( | ||
{ type: 'Store Update' }, | ||
{ shop: { name: 'Car' } } | ||
); | ||
}); | ||
}); |
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,15 @@ | ||
import { createDevtoolsFeature, Mapper } from './devtools-feature'; | ||
|
||
/** | ||
* Allows you to define a function to map the state. | ||
* | ||
* It is needed for huge states, that slows down the Devtools and where | ||
* you don't need to see the whole state. | ||
* | ||
* @param map function which maps the state | ||
*/ | ||
export function withMapper<State extends object>( | ||
map: (state: State) => Record<string, unknown> | ||
) { | ||
return createDevtoolsFeature({ map: map as Mapper }); | ||
} |