-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(entity): log a warning message when selectId returns undefined i…
- Loading branch information
1 parent
28e2cc9
commit 8f05f1f
Showing
4 changed files
with
85 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as ngCore from '@angular/core'; | ||
import { selectIdValue } from '../src/utils'; | ||
import { BookModel, AClockworkOrange } from './fixtures/book'; | ||
|
||
describe('Entity utils', () => { | ||
describe(`selectIdValue()`, () => { | ||
it('should not warn when key does exist', () => { | ||
const spy = spyOn(console, 'warn'); | ||
|
||
const key = selectIdValue(AClockworkOrange, book => book.id); | ||
|
||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should warn when key does not exist in dev mode', () => { | ||
const spy = spyOn(console, 'warn'); | ||
|
||
const key = selectIdValue(AClockworkOrange, (book: any) => book.foo); | ||
|
||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should warn when key is undefined in dev mode', () => { | ||
const spy = spyOn(console, 'warn'); | ||
|
||
const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }; | ||
const key = selectIdValue( | ||
undefinedAClockworkOrange, | ||
(book: any) => book.id | ||
); | ||
|
||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not warn when key does not exist in prod mode', () => { | ||
spyOn(ngCore, 'isDevMode').and.returnValue(false); | ||
const spy = spyOn(console, 'warn'); | ||
|
||
const key = selectIdValue(AClockworkOrange, (book: any) => book.foo); | ||
|
||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not warn when key is undefined in prod mode', () => { | ||
spyOn(ngCore, 'isDevMode').and.returnValue(false); | ||
const spy = spyOn(console, 'warn'); | ||
|
||
const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }; | ||
const key = selectIdValue( | ||
undefinedAClockworkOrange, | ||
(book: any) => book.id | ||
); | ||
|
||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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,19 @@ | ||
import { isDevMode } from '@angular/core'; | ||
import { IdSelector } from './models'; | ||
|
||
export function selectIdValue<T>(entity: T, selectId: IdSelector<T>) { | ||
const key = selectId(entity); | ||
|
||
if (isDevMode() && key === undefined) { | ||
console.warn( | ||
'@ngrx/entity: The entity passed to the `selectId` implementation returned undefined.', | ||
'You should probably provide your own `selectId` implementation.', | ||
'The entity that was passed:', | ||
entity, | ||
'The `selectId` implementation:', | ||
selectId.toString() | ||
); | ||
} | ||
|
||
return key; | ||
} |