diff --git a/src/features/offer/helpers/useGetVenueByDay/useGetVenuesByDay.native.test.ts b/src/features/offer/helpers/useGetVenueByDay/useGetVenuesByDay.native.test.ts index e40e7eac8c2..4e9e24e062d 100644 --- a/src/features/offer/helpers/useGetVenueByDay/useGetVenuesByDay.native.test.ts +++ b/src/features/offer/helpers/useGetVenueByDay/useGetVenuesByDay.native.test.ts @@ -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' @@ -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) @@ -50,9 +52,38 @@ 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: [] }) @@ -60,7 +91,7 @@ describe('useGetVenueByDay', () => { 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 () => { diff --git a/src/features/offer/helpers/useGetVenueByDay/useGetVenuesByDay.ts b/src/features/offer/helpers/useGetVenueByDay/useGetVenuesByDay.ts index 8da2cfea1c6..d2db2b3b011 100644 --- a/src/features/offer/helpers/useGetVenueByDay/useGetVenuesByDay.ts +++ b/src/features/offer/helpers/useGetVenueByDay/useGetVenuesByDay.ts @@ -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' @@ -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, diff --git a/src/libs/algolia/types.ts b/src/libs/algolia/types.ts index 2cc0072d6b8..d01003031ff 100644 --- a/src/libs/algolia/types.ts +++ b/src/libs/algolia/types.ts @@ -137,6 +137,7 @@ export type SearchQueryParameters = { venue?: Venue gtls?: GTL[] distinct?: boolean + objectIds?: string[] } export const transformHit = transformOfferHit