Skip to content

Commit

Permalink
(PC-33818) fix(achievements): hook to handle bug (#7493)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgerrard-pass authored Jan 9, 2025
1 parent 27709d0 commit c26b04e
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AchievementEnum, AchievementResponse } from 'api/gen'
import { useShouldShowAchievementSuccessModal } from 'features/achievements/hooks/useShouldShowAchievementSuccessModal'
import { ModalDisplayState } from 'features/home/components/helpers/useBookingsReactionHelpers'
import { beneficiaryUser } from 'fixtures/user'
import { setFeatureFlags } from 'libs/firebase/firestore/featureFlags/__tests__/setFeatureFlags'
import { RemoteStoreFeatureFlags } from 'libs/firebase/firestore/types'
Expand All @@ -9,6 +10,7 @@ import { mockAuthContextWithUser } from 'tests/AuthContextUtils'
import { renderHook } from 'tests/utils'

jest.mock('features/auth/context/AuthContext')
jest.mock('libs/firebase/analytics/analytics')
const useRemoteConfigContextSpy = jest.spyOn(useRemoteConfigContext, 'useRemoteConfigContext')

const achievements: AchievementResponse[] = [
Expand Down Expand Up @@ -37,21 +39,25 @@ describe('useShouldShowAchievementSuccessModal', () => {
useRemoteConfigContextSpy.mockReturnValue(DEFAULT_REMOTE_CONFIG)
})

it('should return false', () => {
it('should return shouldNotShow', () => {
const { result } = renderHook(useShouldShowAchievementSuccessModal)

expect(result.current.shouldShowAchievementSuccessModal).toBeFalsy()
expect(result.current.shouldShowAchievementSuccessModal).toEqual(
ModalDisplayState.SHOULD_NOT_SHOW
)
})

it('should return false even if there are achievements to show to the user', () => {
it('should return shouldNotShow even if there are achievements to show to the user', () => {
mockAuthContextWithUser({
...beneficiaryUser,
achievements: achievements,
})

const { result } = renderHook(useShouldShowAchievementSuccessModal)

expect(result.current.shouldShowAchievementSuccessModal).toBeFalsy()
expect(result.current.shouldShowAchievementSuccessModal).toEqual(
ModalDisplayState.SHOULD_NOT_SHOW
)
})
})

Expand All @@ -71,32 +77,38 @@ describe('useShouldShowAchievementSuccessModal', () => {
useRemoteConfigContextSpy.mockReturnValue(DEFAULT_REMOTE_CONFIG)
})

it('should return false', () => {
it('should return shouldNotShow', () => {
const { result } = renderHook(useShouldShowAchievementSuccessModal)

expect(result.current.shouldShowAchievementSuccessModal).toBeFalsy()
expect(result.current.shouldShowAchievementSuccessModal).toEqual(
ModalDisplayState.SHOULD_NOT_SHOW
)
})

it('should return false if there no achievements to show to the user', () => {
it('should return shouldNotShow if there no achievements to show to the user', () => {
mockAuthContextWithUser({
...beneficiaryUser,
achievements: [],
})

const { result } = renderHook(useShouldShowAchievementSuccessModal)

expect(result.current.shouldShowAchievementSuccessModal).toBeFalsy()
expect(result.current.shouldShowAchievementSuccessModal).toEqual(
ModalDisplayState.SHOULD_NOT_SHOW
)
})

it('should return false if the achievements are undefined', () => {
it('should return shouldNotShow if the achievements are undefined', () => {
mockAuthContextWithUser({
...beneficiaryUser,
achievements: undefined as unknown as AchievementResponse[],
})

const { result } = renderHook(useShouldShowAchievementSuccessModal)

expect(result.current.shouldShowAchievementSuccessModal).toBeFalsy()
expect(result.current.shouldShowAchievementSuccessModal).toEqual(
ModalDisplayState.SHOULD_NOT_SHOW
)
})

it('should return an empty array if there are no achievements to show to the user', () => {
Expand All @@ -110,26 +122,17 @@ describe('useShouldShowAchievementSuccessModal', () => {
expect(result.current.achievementsToShow).toEqual([])
})

it('should return true if there are achievements to show to the user', () => {
it('should return shouldShow if there are achievements to show to the user', () => {
mockAuthContextWithUser({
...beneficiaryUser,
achievements: achievements,
})

const { result } = renderHook(useShouldShowAchievementSuccessModal)

expect(result.current.shouldShowAchievementSuccessModal).toBeTruthy()
})

it('should return false if another modal is open even if there are achievements to show to the user', () => {
mockAuthContextWithUser({
...beneficiaryUser,
achievements: achievements,
})

const { result } = renderHook(() => useShouldShowAchievementSuccessModal(true))

expect(result.current.shouldShowAchievementSuccessModal).toBeFalsy()
expect(result.current.shouldShowAchievementSuccessModal).toEqual(
ModalDisplayState.SHOULD_SHOW
)
})

it('should return an array with the achievements to show to the user', () => {
Expand Down Expand Up @@ -159,21 +162,25 @@ describe('useShouldShowAchievementSuccessModal', () => {
useRemoteConfigContextSpy.mockReturnValue(DEFAULT_REMOTE_CONFIG)
})

it('should return false', () => {
it('should return shouldNotShow', () => {
const { result } = renderHook(useShouldShowAchievementSuccessModal)

expect(result.current.shouldShowAchievementSuccessModal).toBeFalsy()
expect(result.current.shouldShowAchievementSuccessModal).toEqual(
ModalDisplayState.SHOULD_NOT_SHOW
)
})

it('should return false even if there are achievements to show to the user', () => {
it('should return shouldNotShow even if there are achievements to show to the user', () => {
mockAuthContextWithUser({
...beneficiaryUser,
achievements: achievements,
})

const { result } = renderHook(useShouldShowAchievementSuccessModal)

expect(result.current.shouldShowAchievementSuccessModal).toBeFalsy()
expect(result.current.shouldShowAchievementSuccessModal).toEqual(
ModalDisplayState.SHOULD_NOT_SHOW
)
})

it('should return an array even if there are achievements to show to the user', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { AchievementResponse } from 'api/gen'
import { useAuthContext } from 'features/auth/context/AuthContext'
import { ModalDisplayState } from 'features/home/components/helpers/useBookingsReactionHelpers'
import { useFeatureFlag } from 'libs/firebase/firestore/featureFlags/useFeatureFlag'
import { RemoteStoreFeatureFlags } from 'libs/firebase/firestore/types'
import { useRemoteConfigContext } from 'libs/firebase/remoteConfig/RemoteConfigProvider'

export const useShouldShowAchievementSuccessModal = (
isAnotherModalOpen = false
): { shouldShowAchievementSuccessModal: boolean; achievementsToShow: AchievementResponse[] } => {
export const useShouldShowAchievementSuccessModal = (): {
shouldShowAchievementSuccessModal: ModalDisplayState
achievementsToShow: AchievementResponse[]
} => {
const areAchievementsEnabled = useFeatureFlag(RemoteStoreFeatureFlags.ENABLE_ACHIEVEMENTS)
const { displayAchievements } = useRemoteConfigContext()
const { user } = useAuthContext()
Expand All @@ -22,13 +24,15 @@ export const useShouldShowAchievementSuccessModal = (
!displayAchievements ||
!user?.achievements ||
user?.achievements.length === 0 ||
!isThereAtLeastOneUnseenAchievement ||
isAnotherModalOpen
!isThereAtLeastOneUnseenAchievement
)
return { shouldShowAchievementSuccessModal: false, achievementsToShow: [] }
return {
shouldShowAchievementSuccessModal: ModalDisplayState.SHOULD_NOT_SHOW,
achievementsToShow: [],
}

return {
shouldShowAchievementSuccessModal: isThereAtLeastOneUnseenAchievement,
shouldShowAchievementSuccessModal: ModalDisplayState.SHOULD_SHOW,
achievementsToShow:
unseenAchievements && unseenAchievements?.length > 0 ? unseenAchievements : [],
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { BookingsResponse, ReactionTypeEnum, SubcategoriesResponseModelv2 } from 'api/gen'
import { bookingsSnap } from 'features/bookings/fixtures/bookingsSnap'
import * as CookiesUpToDate from 'features/cookies/helpers/useIsCookiesListUpToDate'
import { useBookingsReactionHelpers } from 'features/home/components/helpers/useBookingsReactionHelpers'
import {
ModalDisplayState,
useBookingsReactionHelpers,
} from 'features/home/components/helpers/useBookingsReactionHelpers'
import { setFeatureFlags } from 'libs/firebase/firestore/featureFlags/__tests__/setFeatureFlags'
import { RemoteStoreFeatureFlags } from 'libs/firebase/firestore/types'
import { PLACEHOLDER_DATA } from 'libs/subcategories/placeholderData'
Expand Down Expand Up @@ -32,35 +35,33 @@ describe('useBookingsReactionHelpers', () => {

const { result } = renderHook(() => useBookingsReactionHelpers(endedBookingWithoutReaction))

expect(result.current.shouldShowReactionModal).toBeFalsy()
expect(result.current.bookingsEligibleToReaction).toHaveLength(0)
expect(result.current.shouldShowReactionModal).toEqual(ModalDisplayState.SHOULD_NOT_SHOW)
})

describe('when FF wipReactionFeature is true', () => {
beforeEach(() => {
setFeatureFlags([RemoteStoreFeatureFlags.WIP_REACTION_FEATURE])
})

it('should return false if the bookings already have reactions', () => {
it('should return shouldNotShow if the bookings already have reactions', () => {
const { result } = renderHook(() => useBookingsReactionHelpers(endedBookingWithReaction))

expect(result.current.shouldShowReactionModal).toBeFalsy()
expect(result.current.bookingsEligibleToReaction).toHaveLength(0)
expect(result.current.shouldShowReactionModal).toEqual(ModalDisplayState.SHOULD_NOT_SHOW)
})

it('should return false if cookies where not accepted', () => {
// eslint-disable-next-line jest/no-disabled-tests
it.skip('should return shouldNotShow if cookies where not accepted', () => {
cookiesNotAccepted()

const { result } = renderHook(() => useBookingsReactionHelpers(endedBookingWithoutReaction))

expect(result.current.shouldShowReactionModal).toBeFalsy()
expect(result.current.bookingsEligibleToReaction).toHaveLength(0)
expect(result.current.shouldShowReactionModal).toEqual(ModalDisplayState.SHOULD_NOT_SHOW)
})

it('should return true if there are bookings to react to', () => {
const { result } = renderHook(() => useBookingsReactionHelpers(endedBookingWithoutReaction))

expect(result.current.shouldShowReactionModal).toBeTruthy()
expect(result.current.shouldShowReactionModal).toEqual(ModalDisplayState.SHOULD_SHOW)
expect(result.current.bookingsEligibleToReaction).toEqual(
endedBookingWithoutReaction.ended_bookings
)
Expand Down
37 changes: 21 additions & 16 deletions src/features/home/components/helpers/useBookingsReactionHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { BookingsResponse } from 'api/gen'
import { useIsCookiesListUpToDate } from 'features/cookies/helpers/useIsCookiesListUpToDate'
import { BookingReponse, BookingsResponse } from 'api/gen'
import { useFeatureFlag } from 'libs/firebase/firestore/featureFlags/useFeatureFlag'
import { RemoteStoreFeatureFlags } from 'libs/firebase/firestore/types'

export enum ModalDisplayState {
PENDING = 'pending',
SHOULD_SHOW = 'shouldShow',
SHOULD_NOT_SHOW = 'shouldNotShow',
}

export const useBookingsReactionHelpers = (
bookings: BookingsResponse = {
ended_bookings: [],
ongoing_bookings: [],
hasBookingsAfter18: false,
}
) => {
bookings: BookingsResponse | undefined = undefined
): {
shouldShowReactionModal: ModalDisplayState
bookingsEligibleToReaction: Array<BookingReponse> | undefined
} => {
const isReactionFeatureActive = useFeatureFlag(RemoteStoreFeatureFlags.WIP_REACTION_FEATURE)
const { isCookiesListUpToDate, cookiesLastUpdate } = useIsCookiesListUpToDate()
const isCookieConsentChecked = cookiesLastUpdate && isCookiesListUpToDate

const bookingsEligibleToReaction =
bookings?.ended_bookings?.filter(
Expand All @@ -21,17 +23,20 @@ export const useBookingsReactionHelpers = (

const firstBookingWithoutReaction = bookingsEligibleToReaction[0]

if (!isReactionFeatureActive || !isCookieConsentChecked)
if (bookings === undefined) {
return {
shouldShowReactionModal: false,
shouldShowReactionModal: ModalDisplayState.PENDING,
bookingsEligibleToReaction: [],
}
}

// There is an issue with !isCookieConsentChecked it goes to true for an instant and disrupts the modal conflict management hook

if (!firstBookingWithoutReaction)
if (!isReactionFeatureActive || !firstBookingWithoutReaction)
return {
shouldShowReactionModal: false,
bookingsEligibleToReaction,
shouldShowReactionModal: ModalDisplayState.SHOULD_NOT_SHOW,
bookingsEligibleToReaction: [],
}

return { shouldShowReactionModal: true, bookingsEligibleToReaction }
return { shouldShowReactionModal: ModalDisplayState.SHOULD_SHOW, bookingsEligibleToReaction }
}
Loading

0 comments on commit c26b04e

Please sign in to comment.