Skip to content

Commit

Permalink
fix: fixes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
mmeissonnier-pass committed Feb 10, 2025
1 parent d82c142 commit df62600
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/api/useSearchVenuesOffer/useSearchVenueOffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function getVenueList(hits: Offer[], userLocation: Position) {
}

export const filterVenueOfferHit = ({ hit, offerId, venueId }: FilterVenueOfferType): boolean =>
hit.objectID !== String(offerId) && hit.venue.id !== venueId
hit.offer.subcategoryId && hit.objectID !== String(offerId) && hit.venue.id !== venueId

export const useSearchVenueOffers = ({
allocineId,
Expand Down
83 changes: 81 additions & 2 deletions src/features/venue/api/useVenueOffers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { useVenueOffers } from 'features/venue/api/useVenueOffers'
import * as useVenueSearchParameters from 'features/venue/helpers/useVenueSearchParameters'
import mockVenueResponse from 'fixtures/venueResponse'
import { fetchMultipleOffers } from 'libs/algolia/fetchAlgolia/fetchMultipleOffers/fetchMultipleOffers'
import { transformOfferHit, filterOfferHit } from 'libs/algolia/fetchAlgolia/transformOfferHit'
import { AlgoliaOffer, HitOffer } from 'libs/algolia/types'
import { LocationMode, Position } from 'libs/location/types'
import * as useNetInfoContextDefault from 'libs/network/NetInfoWrapper'
import { reactQueryProviderHOC } from 'tests/reactQueryProviderHOC'
Expand Down Expand Up @@ -166,11 +168,88 @@ const EXPECTED_CALL_PARAM = {
}

describe('useVenueOffers', () => {
it('should call multiple fetch offers algolia request', async () => {
renderHook(() => useVenueOffers(mockVenueResponse), {
it('should call multiple fetch offers algolia request and return correct response object', async () => {
const FETCH_MULTIPLE_OFFERS_RESPONSE = [
{
hits: [
{
offer: {
dates: [],
isDigital: false,
isDuo: false,
name: 'I want something more',
prices: [28.0],
subcategoryId: SubcategoryIdEnum.CONCERT,
thumbUrl:
'https://storage.googleapis.com/passculture-metier-prod-production-assets-fine-grained/thumbs/mediations/CDZQ',
artist: 'Céline Dion',
},
_geoloc: { lat: 4.90339, lng: -52.31663 },
objectID: '102310',
venue: {
id: 4,
name: 'Lieu 4',
publicName: 'Lieu 4',
address: '4 rue de la paix',
postalCode: '75000',
city: 'Paris',
},
},
],
nbHits: 1,
},
{
hits: [],
nbHits: 0,
},
{
hits: [
{
offer: {
dates: [],
isDigital: false,
isDuo: false,
name: 'Naruto T3',
prices: [28.0],
subcategoryId: SubcategoryIdEnum.LIVRE_PAPIER,
thumbUrl:
'https://storage.googleapis.com/passculture-metier-prod-production-assets-fine-grained/thumbs/mediations/CDZQ',
artist: 'Masashi Kishimoto',
},
_geoloc: { lat: 4.90339, lng: -52.31663 },
objectID: '102310',
venue: {
id: 4,
name: 'Lieu 4',
publicName: 'Lieu 4',
address: '4 rue de la paix',
postalCode: '75000',
city: 'Paris',
},
},
],
nbHits: 1,
},
]
mockFetchMultipleOffers.mockResolvedValueOnce(FETCH_MULTIPLE_OFFERS_RESPONSE)

const { result } = renderHook(() => useVenueOffers(mockVenueResponse), {
wrapper: ({ children }) => reactQueryProviderHOC(children),
})
await waitFor(() => expect(mockFetchMultipleOffers).toHaveBeenCalledWith(EXPECTED_CALL_PARAM))

expect(result.current.data?.hits).toMatchObject(
FETCH_MULTIPLE_OFFERS_RESPONSE.slice(0, 2)
.flatMap((result) => result?.hits)
.filter(filterOfferHit)
.map(transformOfferHit())
)

const headlineOffer = FETCH_MULTIPLE_OFFERS_RESPONSE.at(-1)?.hits[0]

expect(result.current.data?.headlineOffer).toMatchObject(
transformOfferHit()(headlineOffer ?? ({} as AlgoliaOffer<HitOffer>))
)
})

it('should return empty artists when there are no offers', async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/features/venue/api/useVenueOffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export const useVenueOffers = (venue?: VenueResponse): UseQueryResult<VenueOffer
.map(transformHits)

const headlineOffer = headlineOfferResults?.hits[0]
? transformHits(headlineOfferResults?.hits[0])
: undefined

return {
hits: uniqBy(hits, 'objectID'),
Expand Down
2 changes: 1 addition & 1 deletion src/libs/algolia/fetchAlgolia/transformOfferHit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const parseGeoloc = (hit: AlgoliaOffer): AlgoliaOffer['_geoloc'] =>

// We don't want to display offers without image nor subcategoryId
export const filterOfferHit = (hit?: AlgoliaOffer): hit is AlgoliaOffer =>
!!hit && hit?.offer && !!hit.offer.thumbUrl && typeof hit.offer.subcategoryId !== 'undefined'
!!hit && hit.offer && !!hit.offer.thumbUrl && !!hit.offer.subcategoryId

export const transformOfferHit =
(urlPrefix?: string) =>
Expand Down

0 comments on commit df62600

Please sign in to comment.