From 370efab1c76315aa020cf0997f71fcb76d7758e0 Mon Sep 17 00:00:00 2001 From: = Enea_Gore Date: Mon, 4 Nov 2024 00:57:26 +0100 Subject: [PATCH 1/3] fix load rating api spam --- .../webapp/app/exercises/shared/rating/rating.component.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/webapp/app/exercises/shared/rating/rating.component.ts b/src/main/webapp/app/exercises/shared/rating/rating.component.ts index 41173d0c219f..918caf3876d3 100644 --- a/src/main/webapp/app/exercises/shared/rating/rating.component.ts +++ b/src/main/webapp/app/exercises/shared/rating/rating.component.ts @@ -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, @@ -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(); } } From 1c823138f53e1182afac438b5eebdaef0252124d Mon Sep 17 00:00:00 2001 From: = Enea_Gore Date: Mon, 4 Nov 2024 02:35:59 +0100 Subject: [PATCH 2/3] add client test --- .../component/rating/rating.component.spec.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/test/javascript/spec/component/rating/rating.component.spec.ts b/src/test/javascript/spec/component/rating/rating.component.spec.ts index 87f4018ec8a9..694eafa02881 100644 --- a/src/test/javascript/spec/component/rating/rating.component.spec.ts +++ b/src/test/javascript/spec/component/rating/rating.component.spec.ts @@ -94,6 +94,22 @@ describe('RatingComponent', () => { ratingComponent.result.participation = { id: 1 } as Participation; jest.spyOn(ratingService, 'getRating').mockReturnValue(of(2)); ratingComponentFixture.detectChanges(); + ratingComponent.result = { id: 91 } as Result; + ratingComponentFixture.detectChanges(); + expect(loadRatingSpy).toHaveBeenCalledTimes(2); + 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 unneccesary 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); }); From 9c9ac6d6d9144e721346f7d7813ce8b4e4e4f919 Mon Sep 17 00:00:00 2001 From: = Enea_Gore Date: Mon, 4 Nov 2024 02:53:27 +0100 Subject: [PATCH 3/3] typo --- .../spec/component/rating/rating.component.spec.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/test/javascript/spec/component/rating/rating.component.spec.ts b/src/test/javascript/spec/component/rating/rating.component.spec.ts index 694eafa02881..cab6a9d11e14 100644 --- a/src/test/javascript/spec/component/rating/rating.component.spec.ts +++ b/src/test/javascript/spec/component/rating/rating.component.spec.ts @@ -94,14 +94,12 @@ describe('RatingComponent', () => { ratingComponent.result.participation = { id: 1 } as Participation; jest.spyOn(ratingService, 'getRating').mockReturnValue(of(2)); ratingComponentFixture.detectChanges(); - ratingComponent.result = { id: 91 } as Result; - ratingComponentFixture.detectChanges(); - expect(loadRatingSpy).toHaveBeenCalledTimes(2); + expect(loadRatingSpy).toHaveBeenCalledOnce(); 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 unneccesary api calls + // 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;