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

(PC-32766) fix(Offer): fix xp cine offers when offer has no allocineId #7125

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
stockBuilder,
} from 'features/offer/components/MoviesScreeningCalendar/offersStockResponse.builder'
import { useGetVenuesByDay } from 'features/offer/helpers/useGetVenueByDay/useGetVenuesByDay'
import * as fetchAlgoliaOffer from 'libs/algolia/fetchAlgolia/fetchOffers'
import { LocationMode, Position } from 'libs/location/types'
import { reactQueryProviderHOC } from 'tests/reactQueryProviderHOC'
import { act, renderHook } from 'tests/utils'
Expand All @@ -28,6 +29,7 @@ const OFFER_WITH_STOCKS_AFTER_15_DAYS = offerResponseBuilder()
.withStocks([STOCK_AFTER_15_DAYS])
.build()
const OFFER_WITHOUT_STOCKS = offerResponseBuilder().withStocks([]).build()
const OFFER_WITHOUT_ALLOCINE_ID = offerResponseBuilder().withExtraData({}).build()

mockdate.set(TODAY_DATE)

Expand All @@ -50,17 +52,46 @@ jest.mock('@batch.com/react-native-plugin', () =>
)

const mockedGetStocksByOfferIds = jest.spyOn(getStocksByOfferIdsModule, 'getStocksByOfferIds')
const fetchOffersSpy = jest.spyOn(fetchAlgoliaOffer, 'fetchOffers')

describe('useGetVenueByDay', () => {
describe('items', () => {
it('should call fetchOffers with allocineId when provided', async () => {
renderUseGetVenueByDay(TODAY_DATE, OFFER_WITH_STOCKS_TODAY)

const allocineId = OFFER_WITH_STOCKS_TODAY.extraData?.allocineId

await act(() => {})

expect(fetchOffersSpy).toHaveBeenCalledWith(
expect.objectContaining({
parameters: expect.objectContaining({ allocineId }),
})
)
})

it('should call fetchOffers with offerId when no allocineId is provided', async () => {
renderUseGetVenueByDay(TODAY_DATE, OFFER_WITHOUT_ALLOCINE_ID)

const offerId = OFFER_WITHOUT_ALLOCINE_ID.id.toString()

await act(() => {})

expect(fetchOffersSpy).toHaveBeenCalledWith(
expect.objectContaining({
parameters: expect.objectContaining({ objectIds: [offerId] }),
})
)
})

it('should return an empty list when the offer is not available in any cinema', async () => {
mockedGetStocksByOfferIds.mockResolvedValueOnce({ offers: [] })

const { result } = renderUseGetVenueByDay(TODAY_DATE, offerResponseBuilder().build())

await act(() => {})

await expect(result.current.items).toStrictEqual([])
expect(result.current.items).toStrictEqual([])
})

it('should return all the results of the indicated venue and cinema today', async () => {
Expand Down
19 changes: 14 additions & 5 deletions src/features/offer/helpers/useGetVenueByDay/useGetVenuesByDay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useOffersStocks } from 'features/offer/api/useOffersStocks'
import { moviesOfferBuilder } from 'features/offer/components/MoviesScreeningCalendar/moviesOffer.builder'
import { useIsUserUnderage } from 'features/profile/helpers/useIsUserUnderage'
import { initialSearchState } from 'features/search/context/reducer'
import { SearchQueryParameters } from 'libs/algolia/types'
import { useLocation } from 'libs/location'
import { LocationMode } from 'libs/location/types'
import { Offer } from 'shared/offer/types'
Expand Down Expand Up @@ -34,12 +35,20 @@ export const useGetVenuesByDay = (date: Date, offer?: OfferResponseV2, options?:
[offer?.venue.coordinates.latitude, offer?.venue.coordinates.longitude, userLocation]
)

let searchQueryParameters: SearchQueryParameters = {
...initialSearchState,
distinct: false,
}

const allocineId = offer?.extraData?.allocineId ?? undefined
if (allocineId) {
searchQueryParameters = { ...searchQueryParameters, allocineId }
} else if (offer?.id) {
searchQueryParameters = { ...searchQueryParameters, objectIds: [offer.id.toString()] }
}

const { data } = useFetchOffers({
parameters: {
...initialSearchState,
allocineId: offer?.extraData?.allocineId ?? undefined,
distinct: false,
},
parameters: searchQueryParameters,
buildLocationParameterParams: {
userLocation: location,
selectedLocationMode: LocationMode.AROUND_ME,
Expand Down
1 change: 1 addition & 0 deletions src/libs/algolia/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export type SearchQueryParameters = {
venue?: Venue
gtls?: GTL[]
distinct?: boolean
objectIds?: string[]
}

export const transformHit = transformOfferHit
Expand Down
Loading