From 537c423af660b19994a4f3233a48869a470e983c Mon Sep 17 00:00:00 2001 From: Camille Moinier Date: Thu, 13 Jul 2023 15:48:36 +0200 Subject: [PATCH 1/9] (fix): favorite star bug on page change --- .../favorite-star.component.html | 11 +++---- .../favorite-star.component.spec.ts | 20 +++++++------ .../favorite-star/favorite-star.component.ts | 30 ++++++++++++++++--- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.html b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.html index 401e9b1ee2..a5b397165d 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.html +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.html @@ -1,10 +1,7 @@
{{ favoriteCount - }} - -
+ }} + + \ No newline at end of file diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts index 1ede0b62b4..f0b41df16b 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts @@ -115,9 +115,7 @@ describe('FavoriteStarComponent', () => { describe('if record is not part of favorite', () => { beforeEach(() => { ;(favoritesService as any).myFavoritesUuid$.next([ - 'aaa', - 'bbb', - 'ccc', + component.record.uuid, ]) starToggle.newValue.emit(true) fixture.detectChanges() @@ -138,7 +136,6 @@ describe('FavoriteStarComponent', () => { beforeEach(() => { ;(favoritesService as any).myFavoritesUuid$.next([ 'aaa', - 'bbb', component.record.uuid, ]) starToggle.newValue.emit(false) @@ -150,6 +147,15 @@ describe('FavoriteStarComponent', () => { ]) expect(favoritesService.addToFavorites).not.toHaveBeenCalled() }) + beforeEach(() => { + component.record = { + ...RECORDS_SUMMARY_FIXTURE[0], + favoriteCount: 42, + } + ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) + starToggle.newValue.emit(false) + fixture.detectChanges() + }) it('decrease record favorite count by one', () => { expect(favoriteCountEl.textContent).toEqual( (component.record.favoriteCount - 1).toString() @@ -158,11 +164,7 @@ describe('FavoriteStarComponent', () => { }) describe('two subsequent changes', () => { beforeEach(() => { - ;(favoritesService as any).myFavoritesUuid$.next([ - 'aaa', - 'bbb', - component.record.uuid, - ]) + ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) starToggle.newValue.emit(false) starToggle.newValue.emit(true) fixture.detectChanges() diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts index 47bf84df70..b2d244c2f0 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts @@ -10,7 +10,7 @@ import { } from '@angular/core' import { FavoritesService } from '../favorites.service' import { MetadataRecord } from '@geonetwork-ui/util/shared' -import { map } from 'rxjs/operators' +import { first, map, pairwise, take } from 'rxjs/operators' import { AuthService } from '@geonetwork-ui/feature/auth' import tippy from 'tippy.js' import { TranslateService } from '@ngx-translate/core' @@ -49,6 +49,7 @@ export class FavoriteStarComponent implements AfterViewInit, OnDestroy { @ViewChild(StarToggleComponent, { read: ElementRef }) starToggleRef: ElementRef subscription: Subscription + countSubscription: Subscription get hasFavoriteCount() { return this.favoriteCount !== null @@ -74,10 +75,34 @@ export class FavoriteStarComponent implements AfterViewInit, OnDestroy { }) } }) + this.countSubscription = this.favoritesService.myFavoritesUuid$ + .pipe(pairwise()) + .subscribe((fav) => { + const oldFavs = fav[0] + const newFavs = fav[1] + let editedDs = [] + if (oldFavs.length < newFavs.length) { + editedDs.push(newFavs.slice(-1)[0]) + } else { + const deletedFav = oldFavs.filter((fav) => !newFavs.includes(fav))[0] + editedDs.push(deletedFav) + } + editedDs.forEach((ds) => { + if (this.hasFavoriteCount && editedDs.includes(this.record.uuid)) { + if (newFavs.includes(ds)) { + this.favoriteCount += 1 + } else { + this.favoriteCount += -1 + } + } + }) + editedDs = [] + }) } ngOnDestroy(): void { this.subscription.unsubscribe() + this.countSubscription.unsubscribe() } toggleFavorite(isFavorite) { @@ -87,9 +112,6 @@ export class FavoriteStarComponent implements AfterViewInit, OnDestroy { : this.favoritesService.removeFromFavorites([this.record.uuid]) ).subscribe({ complete: () => { - if (this.hasFavoriteCount) { - this.favoriteCount += isFavorite ? 1 : -1 - } this.loading = false this.changeDetector.detectChanges() }, From 8cf81bdf92e5d5d1ae7744735613bcf2bc86e083 Mon Sep 17 00:00:00 2001 From: Camille Moinier Date: Thu, 13 Jul 2023 15:55:46 +0200 Subject: [PATCH 2/9] (chore): format:write --- .../favorite-star/favorite-star.component.html | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.html b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.html index a5b397165d..401e9b1ee2 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.html +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.html @@ -1,7 +1,10 @@
{{ favoriteCount - }} - -
\ No newline at end of file + }} + + From 42b49217bb58403f47c7cfefb13012b616690841 Mon Sep 17 00:00:00 2001 From: Camille Moinier <132347903+cmoinier@users.noreply.github.com> Date: Mon, 17 Jul 2023 09:45:32 +0200 Subject: [PATCH 3/9] Update libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts refactor pairwise function Co-authored-by: Florent Gravin --- .../src/lib/favorites/favorite-star/favorite-star.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts index b2d244c2f0..f725436744 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts @@ -77,7 +77,7 @@ export class FavoriteStarComponent implements AfterViewInit, OnDestroy { }) this.countSubscription = this.favoritesService.myFavoritesUuid$ .pipe(pairwise()) - .subscribe((fav) => { + .subscribe(([oldFavs, newFavs]) => { const oldFavs = fav[0] const newFavs = fav[1] let editedDs = [] From d60f24067acdbfd43f13488cb39e30e0c9c3f124 Mon Sep 17 00:00:00 2001 From: Camille Moinier Date: Mon, 17 Jul 2023 11:23:20 +0200 Subject: [PATCH 4/9] (fix): refactor after reviews + add tests --- .../favorite-star.component.spec.ts | 11 +++++++++ .../favorite-star/favorite-star.component.ts | 23 ++++++++----------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts index f0b41df16b..c07e31bf34 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts @@ -131,6 +131,11 @@ describe('FavoriteStarComponent', () => { (component.record.favoriteCount + 1).toString() ) }) + it('adds the record to myFavoritesUuid$', async () => { + await expect(favoritesService.myFavoritesUuid$['_value'][0]).toBe( + component.record.uuid + ) + }) }) describe('if record is part of favorite', () => { beforeEach(() => { @@ -161,6 +166,12 @@ describe('FavoriteStarComponent', () => { (component.record.favoriteCount - 1).toString() ) }) + it('removes the record from myFavoritesUuid$', async () => { + console.log(favoritesService.myFavoritesUuid$) + await expect(favoritesService.myFavoritesUuid$['_value'][0]).not.toBe( + component.record.uuid + ) + }) }) describe('two subsequent changes', () => { beforeEach(() => { diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts index f725436744..8da3d9c934 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts @@ -78,25 +78,20 @@ export class FavoriteStarComponent implements AfterViewInit, OnDestroy { this.countSubscription = this.favoritesService.myFavoritesUuid$ .pipe(pairwise()) .subscribe(([oldFavs, newFavs]) => { - const oldFavs = fav[0] - const newFavs = fav[1] - let editedDs = [] + let editedFavs = '' if (oldFavs.length < newFavs.length) { - editedDs.push(newFavs.slice(-1)[0]) + editedFavs = newFavs.slice(-1)[0] } else { const deletedFav = oldFavs.filter((fav) => !newFavs.includes(fav))[0] - editedDs.push(deletedFav) + editedFavs = deletedFav } - editedDs.forEach((ds) => { - if (this.hasFavoriteCount && editedDs.includes(this.record.uuid)) { - if (newFavs.includes(ds)) { - this.favoriteCount += 1 - } else { - this.favoriteCount += -1 - } + if (this.hasFavoriteCount && editedFavs === this.record.uuid) { + if (newFavs.includes(editedFavs)) { + this.favoriteCount += 1 + } else { + this.favoriteCount += -1 } - }) - editedDs = [] + } }) } From 8f954ae2c78b1fad4d11a60d1cefdb137c5e3524 Mon Sep 17 00:00:00 2001 From: Camille Moinier <132347903+cmoinier@users.noreply.github.com> Date: Mon, 17 Jul 2023 18:50:21 +0200 Subject: [PATCH 5/9] Update libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts refactor pairwise logic Co-authored-by: Florent Gravin --- .../favorite-star/favorite-star.component.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts index 8da3d9c934..cbad5bfa93 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.ts @@ -78,13 +78,11 @@ export class FavoriteStarComponent implements AfterViewInit, OnDestroy { this.countSubscription = this.favoritesService.myFavoritesUuid$ .pipe(pairwise()) .subscribe(([oldFavs, newFavs]) => { - let editedFavs = '' - if (oldFavs.length < newFavs.length) { - editedFavs = newFavs.slice(-1)[0] - } else { - const deletedFav = oldFavs.filter((fav) => !newFavs.includes(fav))[0] - editedFavs = deletedFav - } + const editedFavs = ( + oldFavs.length < newFavs.length + ? newFavs.slice(-1) + : oldFavs.filter((fav) => !newFavs.includes(fav)) + )[0] if (this.hasFavoriteCount && editedFavs === this.record.uuid) { if (newFavs.includes(editedFavs)) { this.favoriteCount += 1 From 2a1ec012a0fa0bd60696e6816a0c2184cb9e7088 Mon Sep 17 00:00:00 2001 From: Camille Moinier Date: Sun, 23 Jul 2023 21:48:46 +0200 Subject: [PATCH 6/9] (fix): tests --- .../favorite-star.component.spec.ts | 103 +++++++++++------- 1 file changed, 63 insertions(+), 40 deletions(-) diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts index c07e31bf34..299422a216 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts @@ -117,24 +117,36 @@ describe('FavoriteStarComponent', () => { ;(favoritesService as any).myFavoritesUuid$.next([ component.record.uuid, ]) - starToggle.newValue.emit(true) fixture.detectChanges() }) - it('adds record to favorites', () => { - expect(favoritesService.addToFavorites).toHaveBeenCalledWith([ - component.record.uuid, - ]) - expect(favoritesService.removeFromFavorites).not.toHaveBeenCalled() + describe('if change is made from this component', () => { + beforeEach(() => { + starToggle.newValue.emit(true) + fixture.detectChanges() + }) + it('adds record to favorites', () => { + expect(favoritesService.addToFavorites).toHaveBeenCalledWith([ + component.record.uuid, + ]) + expect(favoritesService.removeFromFavorites).not.toHaveBeenCalled() + }) + it('increase record favorite count by one', () => { + expect(favoriteCountEl.textContent).toEqual( + (component.record.favoriteCount + 1).toString() + ) + }) + it('adds the record to myFavoritesUuid$', async () => { + await expect(favoritesService.myFavoritesUuid$['_value'][0]).toBe( + component.record.uuid + ) + }) }) - it('increase record favorite count by one', () => { - expect(favoriteCountEl.textContent).toEqual( - (component.record.favoriteCount + 1).toString() - ) - }) - it('adds the record to myFavoritesUuid$', async () => { - await expect(favoritesService.myFavoritesUuid$['_value'][0]).toBe( - component.record.uuid - ) + describe('if change is made from another component', () => { + it('adds the record to myFavoritesUuid$', async () => { + await expect(favoritesService.myFavoritesUuid$['_value'][0]).toBe( + component.record.uuid + ) + }) }) }) describe('if record is part of favorite', () => { @@ -143,36 +155,47 @@ describe('FavoriteStarComponent', () => { 'aaa', component.record.uuid, ]) - starToggle.newValue.emit(false) - fixture.detectChanges() - }) - it('removes record from favorites', () => { - expect(favoritesService.removeFromFavorites).toHaveBeenCalledWith([ - component.record.uuid, - ]) - expect(favoritesService.addToFavorites).not.toHaveBeenCalled() - }) - beforeEach(() => { - component.record = { - ...RECORDS_SUMMARY_FIXTURE[0], - favoriteCount: 42, - } - ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) - starToggle.newValue.emit(false) fixture.detectChanges() }) - it('decrease record favorite count by one', () => { - expect(favoriteCountEl.textContent).toEqual( - (component.record.favoriteCount - 1).toString() - ) + describe('if change is made from this component', () => { + beforeEach(() => { + component.record = { + ...RECORDS_SUMMARY_FIXTURE[0], + favoriteCount: 42, + } + ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) + starToggle.newValue.emit(false) + fixture.detectChanges() + }) + it('removes record from favorites', () => { + expect(favoritesService.removeFromFavorites).toHaveBeenCalledWith([ + component.record.uuid, + ]) + expect(favoritesService.addToFavorites).not.toHaveBeenCalled() + }) + it('decrease record favorite count by one', () => { + expect(favoriteCountEl.textContent).toEqual( + (component.record.favoriteCount - 1).toString() + ) + }) }) - it('removes the record from myFavoritesUuid$', async () => { - console.log(favoritesService.myFavoritesUuid$) - await expect(favoritesService.myFavoritesUuid$['_value'][0]).not.toBe( - component.record.uuid - ) + describe('if change is made from another component', () => { + beforeEach(() => { + component.record = { + ...RECORDS_SUMMARY_FIXTURE[0], + favoriteCount: 42, + } + ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) + fixture.detectChanges() + }) + it('removes the record from myFavoritesUuid$', async () => { + await expect( + favoritesService.myFavoritesUuid$['_value'][0] + ).not.toBe(component.record.uuid) + }) }) }) + describe('two subsequent changes', () => { beforeEach(() => { ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) From 97b99a8bf76c588839110add8f540a7153d89e41 Mon Sep 17 00:00:00 2001 From: Camille Moinier Date: Thu, 27 Jul 2023 17:25:30 +0200 Subject: [PATCH 7/9] (tests): edit tests --- .../favorite-star.component.spec.ts | 82 ++++++++----------- 1 file changed, 33 insertions(+), 49 deletions(-) diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts index 299422a216..d5898aebb3 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts @@ -106,21 +106,35 @@ describe('FavoriteStarComponent', () => { it('does not create tippy tooltip', () => { expect(tippy).not.toHaveBeenCalled() }) + describe('on toggle state change', () => { beforeEach(() => { favoriteCountEl = fixture.debugElement.query( By.css('.favorite-count') ).nativeElement }) - describe('if record is not part of favorite', () => { - beforeEach(() => { - ;(favoritesService as any).myFavoritesUuid$.next([ - component.record.uuid, - ]) - fixture.detectChanges() + describe('When I click on the star', () => { + // TEST THAT FUNCTIONS ARE CALLED + describe('When the record is already in favorites', () => { + beforeEach(() => { + ;(favoritesService as any).myFavoritesUuid$.next([ + component.record.uuid, + ]) + starToggle.newValue.emit(false) + fixture.detectChanges() + }) + it('removes record from favorites', () => { + expect(favoritesService.removeFromFavorites).toHaveBeenCalledWith([ + component.record.uuid, + ]) + expect(favoritesService.addToFavorites).not.toHaveBeenCalled() + }) }) - describe('if change is made from this component', () => { + describe('When the record is not in favorites', () => { beforeEach(() => { + ;(favoritesService as any).myFavoritesUuid$.next([ + component.record.uuid, + ]) starToggle.newValue.emit(true) fixture.detectChanges() }) @@ -130,68 +144,38 @@ describe('FavoriteStarComponent', () => { ]) expect(favoritesService.removeFromFavorites).not.toHaveBeenCalled() }) - it('increase record favorite count by one', () => { - expect(favoriteCountEl.textContent).toEqual( - (component.record.favoriteCount + 1).toString() - ) - }) - it('adds the record to myFavoritesUuid$', async () => { - await expect(favoritesService.myFavoritesUuid$['_value'][0]).toBe( - component.record.uuid - ) - }) - }) - describe('if change is made from another component', () => { - it('adds the record to myFavoritesUuid$', async () => { - await expect(favoritesService.myFavoritesUuid$['_value'][0]).toBe( - component.record.uuid - ) - }) }) }) - describe('if record is part of favorite', () => { + describe('When myFavoritesUuid has been updated', () => { beforeEach(() => { ;(favoritesService as any).myFavoritesUuid$.next([ - 'aaa', component.record.uuid, ]) fixture.detectChanges() }) - describe('if change is made from this component', () => { + // TEST THAT THE AMOUNT IS CORRECT + describe('When my record has been updated', () => { beforeEach(() => { - component.record = { - ...RECORDS_SUMMARY_FIXTURE[0], - favoriteCount: 42, - } - ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) - starToggle.newValue.emit(false) - fixture.detectChanges() - }) - it('removes record from favorites', () => { - expect(favoritesService.removeFromFavorites).toHaveBeenCalledWith([ + ;(favoritesService as any).myFavoritesUuid$.next([ component.record.uuid, ]) - expect(favoritesService.addToFavorites).not.toHaveBeenCalled() + fixture.detectChanges() }) - it('decrease record favorite count by one', () => { + it('increase record favorite count by one', () => { expect(favoriteCountEl.textContent).toEqual( - (component.record.favoriteCount - 1).toString() + (component.record.favoriteCount + 1).toString() ) }) }) - describe('if change is made from another component', () => { + describe('When my record has not been updated', () => { beforeEach(() => { - component.record = { - ...RECORDS_SUMMARY_FIXTURE[0], - favoriteCount: 42, - } ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) fixture.detectChanges() }) - it('removes the record from myFavoritesUuid$', async () => { - await expect( - favoritesService.myFavoritesUuid$['_value'][0] - ).not.toBe(component.record.uuid) + it('increase record favorite count by one', () => { + expect(favoriteCountEl.textContent).toEqual( + component.record.favoriteCount.toString() + ) }) }) }) From e12d662dbb3d78be52fddfb04cf6b07b768208eb Mon Sep 17 00:00:00 2001 From: Florent gravin Date: Fri, 28 Jul 2023 11:50:21 +0200 Subject: [PATCH 8/9] test(favorite): work scenario --- .../favorite-star.component.spec.ts | 280 +++++++++--------- 1 file changed, 140 insertions(+), 140 deletions(-) diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts index d5898aebb3..9429cfd2b7 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts @@ -31,7 +31,7 @@ describe('FavoriteStarComponent', () => { let fixture: ComponentFixture let authService: AuthService let favoritesService: FavoritesService - let favoriteCountEl: HTMLElement + let favoriteCountHTMLEl: HTMLElement let starToggle: StarToggleComponent beforeEach(async () => { @@ -65,7 +65,6 @@ describe('FavoriteStarComponent', () => { favoritesService = TestBed.inject(FavoritesService) fixture = TestBed.createComponent(FavoriteStarComponent) component = fixture.componentInstance - component.record = { ...RECORDS_SUMMARY_FIXTURE[0], favoriteCount: 42 } fixture.detectChanges() starToggle = fixture.debugElement.query( By.directive(StarToggleComponent) @@ -76,166 +75,167 @@ describe('FavoriteStarComponent', () => { expect(component).toBeTruthy() }) - describe('when a record has a favorite count', () => { - beforeEach(() => { - favoriteCountEl = fixture.debugElement.query( - By.css('.favorite-count') - ).nativeElement + describe('Favorite count', () => { + describe('when a record has a favorite count', () => { + beforeEach(() => { + component.record = { ...RECORDS_SUMMARY_FIXTURE[0], favoriteCount: 42 } + fixture.detectChanges() + }) + it('shows the amount of favorites on the record', () => { + favoriteCountHTMLEl = fixture.debugElement.query( + By.css('.favorite-count') + ).nativeElement + expect(favoriteCountHTMLEl).toBeTruthy() + expect(favoriteCountHTMLEl.textContent).toEqual( + component.record.favoriteCount.toString() + ) + }) }) - it('shows the amount of favorites on the record', () => { - expect(favoriteCountEl).toBeTruthy() - expect(favoriteCountEl.textContent).toEqual( - component.record.favoriteCount.toString() - ) + describe('when a record does not have a favorite count', () => { + beforeEach(() => { + component.record = { ...RECORDS_SUMMARY_FIXTURE[0] } + fixture.detectChanges() + }) + it('does not show the amount of favorites on the record', () => { + const favoriteCountEl = fixture.debugElement.query( + By.css('.favorite-count') + ) + expect(favoriteCountEl).toBeFalsy() + }) }) }) - describe('when a record does not have a favorite count', () => { + describe('Favorite component availability', () => { + describe('when authenticated', () => { + it('star toggle is enabled', () => { + expect(starToggle.disabled).toBe(false) + }) + it('does not create tippy tooltip', () => { + expect(tippy).not.toHaveBeenCalled() + }) + }) + describe('when not authenticated', () => { + beforeEach(() => { + ;(authService as any).isAnonymous$.next(true) + fixture.detectChanges() + }) + it('star toggle is disabled', () => { + expect(starToggle.disabled).toBe(true) + }) + it('creates tippy tooltip', () => { + expect(tippy).toHaveBeenCalledWith( + expect.any(Object), + expect.objectContaining({ + content: 'You can log in here', + allowHTML: true, + interactive: true, + zIndex: 40, + maxWidth: 250, + }) + ) + }) + }) + }) + describe('On favorite click', () => { beforeEach(() => { - component.record = { ...RECORDS_SUMMARY_FIXTURE[0] } - delete component.record.favoriteCount + component.record = { ...RECORDS_SUMMARY_FIXTURE[0], favoriteCount: 42 } fixture.detectChanges() }) - it('does not show the amount of favorites on the record', () => { - expect(fixture.debugElement.query(By.css('.favorite-count'))).toBeFalsy() + describe('When the record is already in favorites', () => { + beforeEach(() => { + starToggle.newValue.emit(false) + fixture.detectChanges() + }) + it('removes record from favorites', () => { + expect(favoritesService.removeFromFavorites).toHaveBeenCalledWith([ + component.record.uuid, + ]) + expect(favoritesService.addToFavorites).not.toHaveBeenCalled() + }) + }) + describe('When the record is not in favorites', () => { + beforeEach(() => { + starToggle.newValue.emit(true) + fixture.detectChanges() + }) + it('adds record to favorites', () => { + expect(favoritesService.addToFavorites).toHaveBeenCalledWith([ + component.record.uuid, + ]) + expect(favoritesService.removeFromFavorites).not.toHaveBeenCalled() + }) }) }) - describe('when authenticated', () => { - it('star toggle is enabled', () => { - expect(starToggle.disabled).toBe(false) + + describe('On favorites array update', () => { + beforeEach(() => { + component.record = { ...RECORDS_SUMMARY_FIXTURE[0], favoriteCount: 42 } + fixture.detectChanges() + favoriteCountHTMLEl = fixture.debugElement.query( + By.css('.favorite-count') + ).nativeElement }) - it('does not create tippy tooltip', () => { - expect(tippy).not.toHaveBeenCalled() + describe('When my record is part of the updates', () => { + beforeEach(() => { + ;(favoritesService as any).myFavoritesUuid$.next([ + component.record.uuid, + ]) + fixture.detectChanges() + }) + it('increase record favorite count by one', () => { + expect(favoriteCountHTMLEl.textContent).toEqual( + (component.record.favoriteCount + 1).toString() + ) + }) }) - - describe('on toggle state change', () => { + describe('When my record is not part of the updates', () => { beforeEach(() => { - favoriteCountEl = fixture.debugElement.query( - By.css('.favorite-count') - ).nativeElement + ;(favoritesService as any).myFavoritesUuid$.next(['aaa', 'bbb']) + fixture.detectChanges() }) - describe('When I click on the star', () => { - // TEST THAT FUNCTIONS ARE CALLED - describe('When the record is already in favorites', () => { - beforeEach(() => { - ;(favoritesService as any).myFavoritesUuid$.next([ - component.record.uuid, - ]) - starToggle.newValue.emit(false) - fixture.detectChanges() - }) - it('removes record from favorites', () => { - expect(favoritesService.removeFromFavorites).toHaveBeenCalledWith([ - component.record.uuid, - ]) - expect(favoritesService.addToFavorites).not.toHaveBeenCalled() - }) - }) - describe('When the record is not in favorites', () => { - beforeEach(() => { - ;(favoritesService as any).myFavoritesUuid$.next([ - component.record.uuid, - ]) - starToggle.newValue.emit(true) - fixture.detectChanges() - }) - it('adds record to favorites', () => { - expect(favoritesService.addToFavorites).toHaveBeenCalledWith([ - component.record.uuid, - ]) - expect(favoritesService.removeFromFavorites).not.toHaveBeenCalled() - }) - }) - }) - describe('When myFavoritesUuid has been updated', () => { - beforeEach(() => { - ;(favoritesService as any).myFavoritesUuid$.next([ - component.record.uuid, - ]) - fixture.detectChanges() - }) - // TEST THAT THE AMOUNT IS CORRECT - describe('When my record has been updated', () => { - beforeEach(() => { - ;(favoritesService as any).myFavoritesUuid$.next([ - component.record.uuid, - ]) - fixture.detectChanges() - }) - it('increase record favorite count by one', () => { - expect(favoriteCountEl.textContent).toEqual( - (component.record.favoriteCount + 1).toString() - ) - }) - }) - describe('When my record has not been updated', () => { - beforeEach(() => { - ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) - fixture.detectChanges() - }) - it('increase record favorite count by one', () => { - expect(favoriteCountEl.textContent).toEqual( - component.record.favoriteCount.toString() - ) - }) - }) + it('increase record favorite count by one', () => { + expect(favoriteCountHTMLEl.textContent).toEqual( + component.record.favoriteCount.toString() + ) }) + }) + }) - describe('two subsequent changes', () => { - beforeEach(() => { - ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) - starToggle.newValue.emit(false) - starToggle.newValue.emit(true) - fixture.detectChanges() - }) - it('removes and adds record to favorites', () => { - expect(favoritesService.removeFromFavorites).toHaveBeenCalledWith([ - component.record.uuid, - ]) - expect(favoritesService.addToFavorites).toHaveBeenCalledWith([ - component.record.uuid, - ]) - }) - it('record favorite count stays the same', () => { - expect(favoriteCountEl.textContent).toEqual( - component.record.favoriteCount.toString() - ) - }) - }) - describe('if favorite modification fails', () => { - beforeEach(() => { - favoritesService.addToFavorites = () => throwError('blargz') - starToggle.newValue.emit(true) - fixture.detectChanges() - }) - it('does not change record favorite count', () => { - expect(favoriteCountEl.textContent).toEqual( - component.record.favoriteCount.toString() - ) - }) - }) + describe('two subsequent changes', () => { + beforeEach(() => { + component.record = { ...RECORDS_SUMMARY_FIXTURE[0], favoriteCount: 42 } + ;(favoritesService as any).myFavoritesUuid$.next(['aaa']) + starToggle.newValue.emit(false) + starToggle.newValue.emit(true) + fixture.detectChanges() + }) + it('removes and adds record to favorites', () => { + expect(favoritesService.removeFromFavorites).toHaveBeenCalledWith([ + component.record.uuid, + ]) + expect(favoritesService.addToFavorites).toHaveBeenCalledWith([ + component.record.uuid, + ]) + }) + it('record favorite count stays the same', () => { + expect(favoriteCountHTMLEl.textContent).toEqual( + component.record.favoriteCount.toString() + ) }) }) - describe('when not authenticated', () => { + describe('if favorite modification fails', () => { beforeEach(() => { - ;(authService as any).isAnonymous$.next(true) + component.record = { ...RECORDS_SUMMARY_FIXTURE[0], favoriteCount: 42 } + favoritesService.addToFavorites = () => throwError('blargz') + starToggle.newValue.emit(true) fixture.detectChanges() }) - it('star toggle is disabled', () => { - expect(starToggle.disabled).toBe(true) - }) - it('creates tippy tooltip', () => { - expect(tippy).toHaveBeenCalledWith( - expect.any(Object), - expect.objectContaining({ - content: 'You can log in here', - allowHTML: true, - interactive: true, - zIndex: 40, - maxWidth: 250, - }) + it('does not change record favorite count', () => { + expect(favoriteCountHTMLEl.textContent).toEqual( + component.record.favoriteCount.toString() ) }) }) + describe('unsubscribe', () => { let unsubscribeSpy beforeEach(() => { From 60ed20f191a463b8de7490cd89a2ebf13745553c Mon Sep 17 00:00:00 2001 From: Florent gravin Date: Fri, 28 Jul 2023 11:51:10 +0200 Subject: [PATCH 9/9] chore(prettier): exclude /docs cache & lint --- .prettierignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.prettierignore b/.prettierignore index 141d5afa6c..3859ddbc13 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,6 +2,7 @@ /dist /coverage - +/docs/.vitepress/cache +/docs/.vitepress/dist # OpenAPI generated specs **/spec.yaml