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

fix(rating): prevent rateChange to be fired when element is pristine #1306

Closed
wants to merge 1 commit into from

Conversation

fbasso
Copy link
Member

@fbasso fbasso commented Feb 9, 2017

Close #1282

@@ -85,6 +85,33 @@ describe('ngb-rating', () => {
expect(stars.length).toBe(3);
});

it('handles the change event', fakeAsync(() => {
const fixture =
createTestComponent('<ngb-rating [(rate)]="currentRate" (rateChange)="onRateChange($event)"></ngb-rating>');

Choose a reason for hiding this comment

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

[(rate)]="currentRate" is a shortcut for [rate]="currentRate" (rateChange)="currentRate = $event". As such I don't think that we want to have 2 rateChange event handlers in this test.

More generally, IMO the simplest possible test for the issue we are trying to solve here could be:

  it('should now fire change event initially',  fakeAsync(() => {
    const fixture = createTestComponent('<ngb-rating [rate]="3" (rateChange)="changed = true"></ngb-rating>');
    tick();
    expect(fixture.componentInstance.changed).toBeFalsy();
  }));

@pkozlowski-opensource
Copy link
Member

@fbasso thnx for the PR - I left one comment about a test but it actually took me long time to understand the logic of the current rating control and the fix. When I finally got what is going on I think that we could a simpler fix.

Your solution fixes the issue and does so in little code change which is good. But at the same time it introduces new state variable (_initDone) which is one more thing to worry about in the future.

After going through the code of the rating directive I think that the main issue is that we are using the rate variable to hold both "current" state and "next" state (potential one, based on mouseover). This means that we can't use rate to do easy comparisons (since it can mean 2 different things at different times).

Given all the above I would like to propose a refactoring where we introduce explicit state for hovered value and don't change rate till the selection is done. This means that we can reliably use rate in change comparisons. I've got an alternative PR that fixes #1282 as well but without introducing new state (no need for _initDone). I believe that the proposed alternative is slightly simpler as it doesn't require new state and just refactors the existing code instead of introducing new code.

I've just opened #1357 with an alternative implementation - please have a look and let me know what you think and which one you would prefer to be merged.

@pkozlowski-opensource pkozlowski-opensource added this to the 1.0.0-alpha.21 milestone Mar 3, 2017
@fbasso
Copy link
Member Author

fbasso commented Mar 9, 2017

@pkozlowski-opensource Agree. I focused only on the issue but it's right that using the rate attribute for both current value and mouse over create a mess. The code you proposed render the rate component easier to understand (and fix this issue at the same time).

So let's go for your changes !

@fbasso fbasso deleted the rating branch March 9, 2017 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants