Skip to content

Commit

Permalink
Development: Fix load rating api spam and fix flaky e2e tests (#9665)
Browse files Browse the repository at this point in the history
  • Loading branch information
EneaGore authored Nov 4, 2024
1 parent 55a39e0 commit fdc2501
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class RatingComponent implements OnInit, OnChanges {
public rating: number;
public disableRating = false;
@Input() result?: Result;
private previousResultId?: number;

constructor(
private ratingService: RatingService,
Expand All @@ -26,7 +27,8 @@ export class RatingComponent implements OnInit, OnChanges {
}

ngOnChanges(changes: SimpleChanges): void {
if (changes['result'] && !changes['result'].isFirstChange()) {
if (changes['result'] && changes['result'].currentValue?.id !== this.previousResultId) {
this.previousResultId = changes['result'].currentValue?.id;
this.loadRating();
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/javascript/spec/component/rating/rating.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ describe('RatingComponent', () => {
expect(ratingComponent.rating).toBe(2);
});

it('should not call loadRating if result ID remains the same', () => {
// without this condition the loadRating might be spammed making unnecessary api calls
const loadRatingSpy = jest.spyOn(ratingComponent, 'loadRating');
ratingComponent.result = { id: 90 } as Result;
ratingComponent.result.submission = { id: 1 } as Submission;
ratingComponent.result.participation = { id: 1 } as Participation;
jest.spyOn(ratingService, 'getRating').mockReturnValue(of(2));
ratingComponentFixture.detectChanges();
ratingComponent.result = { id: 90 } as Result;
ratingComponentFixture.detectChanges();
expect(loadRatingSpy).toHaveBeenCalledOnce();
expect(ratingComponent.rating).toBe(2);
});

describe('OnRate', () => {
beforeEach(() => {
ratingComponent.rating = 0;
Expand Down

0 comments on commit fdc2501

Please sign in to comment.