Skip to content
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

[DOC beta] Assert that both modelName and id are passed to peekRecord #4998

Merged
merged 2 commits into from
Nov 15, 2017

Conversation

bantic
Copy link
Member

@bantic bantic commented May 25, 2017

I've seen folks get confused by forgetting that a modelName is a required param and then do something like store.peekRecord('my-id'). This assertion would point out that both values must be passed. Thanks for considering it. :)

@stefanpenner
Copy link
Member

mind adding an expectAssertion test ?

@bantic
Copy link
Member Author

bantic commented May 25, 2017

sure thing

@bantic
Copy link
Member Author

bantic commented May 25, 2017

Updated. Also added an expectAssertion for the existing assertion that a model name (not a class) must be passed (e.g. when calling store.peekRecord(modelClass, id)).

});
});

test('peekRecord should assert if passed a model class instead of model name', function(assert) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bantic could you use testInDebug instead of test for these tests. The assert gets stripped for the production build and is causing the CI to fail when we run tests in production mode.

import testInDebug from 'dummy/tests/helpers/test-in-debug';

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmac Thanks for pointing that out. I made the change and updated this PR. I'll keep an eye on it to confirm the tests now pass.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The appveyor tests appear to have passed, but appveyor reports a timeout. Might just need a kick to re-run the tests; afaict everything should be passing now.

Adds expectAssertion test for the new assertion, as well as an
expectAssertion test for the existing assertion that a model name
(string) must be passed instead of a model class.
@pangratz pangratz merged commit 1d68d9a into emberjs:master Nov 15, 2017
@pangratz
Copy link
Member

Thanks a bunch @bantic!

bmac pushed a commit that referenced this pull request Nov 19, 2017
…d` (#4998)

Adds expectAssertion test for the new assertion, as well as an
expectAssertion test for the existing assertion that a model name
(string) must be passed instead of a model class.
(cherry picked from commit 1d68d9a)
BryanCrotaz added a commit to BryanCrotaz/data that referenced this pull request Nov 19, 2017
* commit 'dd3cd69462943530039428a0203f7d66beb1bfd0':
  Bump master version to 3.0.0-canary
  Update changelog for Ember Data 2.17.0 release
  [DOC beta] Assert that both modelName and id are passed to `peekRecord` (emberjs#4998)
  [doc] Update urlForFindRecord example
@sumeetattree
Copy link
Contributor

sumeetattree commented May 8, 2018

So this thing crept up on me today while upgrading from ember-data 2.16 to 3.1.

I am not sure if this is correct way to ensure the passing of a 2nd parameter to the method. I have a valid use case where I am fetching the record in an existing component. And the 2nd parameter may be absent null/undefined. In that case peekRecord should return null instead now it throws an Exception.

// some-component.js
...
colorId: undefined, // set on the component
fruit: computed('colorId', function() {
  // colorId can be null/undefined
  return this.get('store').peekRecord('fruit', this.get('colorId'));
}),

If you still want to keep the assertion maybe modify the peekRecord method to check for number of arguments instead of just checking if id is present?

@stefanpenner @bantic Thoughts?

@pangratz
Copy link
Member

pangratz commented May 9, 2018

peekRecord without an id IMHO doesn't make sense and I would say this should be handled a layer up in your app code, so basically:

import { isNone } from '@ember/utils';

colorId: undefined, // set on the component

fruit: computed('colorId', function() {
  let colorId = this.get('colorId');

  if (!isNone(colorId)) {
    return this.get('store').peekRecord('fruit', colorId);
  }

  return null;
})

I would argue that this is much saner to reason about on what the expected behaviour of that computed property is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants