-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Entity): log a message when selectId returns undefined in dev mode #1169
Changes from all commits
deee679
742b4b2
7663972
52ccd89
c7d7f43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You think it would be better to just set prod mode here and reset it after the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Just didn't want to use the wildcard import if we didn't have to. |
||
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(); | ||
}); | ||
}); | ||
}); |
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by that? You would also log this when the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. We also want to warn if the selected id value is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My initial thinking was to only check on For me it would also make more sense to check if the id value is false, but as I said I'm not sure how this is used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, that makes sense. My rationale was that we're checking for values that aren't valid ids. |
||
'@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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need an additional test with a valid key that's undefined.