-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6b42af
commit bb714bb
Showing
4 changed files
with
122 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ures/offer/components/MoviesScreeningCalendar/hook/useMoviesScreeningsList.native.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { SubcategoryIdEnum } from 'api/gen' | ||
import * as getStocksByOfferIdsModule from 'features/offer/api/getStocksByOfferIds' | ||
import * as useMovieCalendarModule from 'features/offer/components/MoviesScreeningCalendar/MovieCalendarContext' | ||
import { | ||
offerResponseBuilder, | ||
stockBuilder, | ||
} from 'features/offer/components/MoviesScreeningCalendar/offersStockResponse.builder' | ||
import { reactQueryProviderHOC } from 'tests/reactQueryProviderHOC' | ||
import { act, renderHook } from 'tests/utils' | ||
|
||
import { useMoviesScreeningsList } from './useMoviesScreeningsList' | ||
|
||
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter') | ||
jest.mock('libs/network/NetInfoWrapper') | ||
|
||
const getStocksByOfferIdsModuleSpy = jest.spyOn(getStocksByOfferIdsModule, 'getStocksByOfferIds') | ||
|
||
describe('useMoviesScreeningsList', () => { | ||
const mockSelectedDate = new Date('2023-05-15') | ||
const mockOfferIds = [1, 2, 3] | ||
|
||
beforeEach(() => { | ||
jest | ||
.spyOn(useMovieCalendarModule, 'useMovieCalendar') | ||
.mockReturnValue({ selectedDate: mockSelectedDate, goToDate: jest.fn }) | ||
}) | ||
|
||
it('should return filtered movie offers', async () => { | ||
const mockOffers = [ | ||
offerResponseBuilder() | ||
.withId(1) | ||
.withSubcategoryId(SubcategoryIdEnum.SEANCE_CINE) | ||
.withStocks([stockBuilder().withBeginningDatetime('2023-05-15T10:00:00Z').build()]) | ||
.build(), | ||
offerResponseBuilder() | ||
.withId(2) | ||
.withSubcategoryId(SubcategoryIdEnum.SEANCE_CINE) | ||
.withStocks([stockBuilder().withBeginningDatetime('2023-05-16T10:00:00Z').build()]) | ||
.build(), | ||
offerResponseBuilder() | ||
.withId(3) | ||
.withSubcategoryId(SubcategoryIdEnum.LIVRE_PAPIER) | ||
.withStocks([stockBuilder().withBeginningDatetime('2023-05-15T10:00:00Z').build()]) | ||
.build(), | ||
] | ||
|
||
getStocksByOfferIdsModuleSpy.mockResolvedValueOnce({ offers: mockOffers }) | ||
|
||
const { result } = renderHook(() => useMoviesScreeningsList(mockOfferIds), { | ||
wrapper: ({ children }) => reactQueryProviderHOC(children), | ||
}) | ||
|
||
await act(async () => {}) | ||
|
||
expect(result.current.moviesOffers).toStrictEqual([{ isUpcoming: false, offer: mockOffers[0] }]) | ||
}) | ||
|
||
it('should return an empty array when no offers match the criteria', async () => { | ||
getStocksByOfferIdsModuleSpy.mockResolvedValueOnce({ offers: [] }) | ||
|
||
const { result } = renderHook(() => useMoviesScreeningsList(mockOfferIds), { | ||
wrapper: ({ children }) => reactQueryProviderHOC(children), | ||
}) | ||
|
||
await act(async () => {}) | ||
|
||
expect(result.current.moviesOffers).toHaveLength(0) | ||
}) | ||
}) |
32 changes: 32 additions & 0 deletions
32
src/features/offer/components/MoviesScreeningCalendar/hook/useMoviesScreeningsList.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useMemo } from 'react' | ||
|
||
import { SubcategoryIdEnum } from 'api/gen' | ||
import { useOffersStocks } from 'features/offer/api/useOffersStocks' | ||
import { filterOffersStocksByDate } from 'features/offer/components/MoviesScreeningCalendar/filterOffersStocksByDate' | ||
import { | ||
MovieOffer, | ||
getNextMoviesByDate, | ||
} from 'features/offer/components/MoviesScreeningCalendar/getNextMoviesByDate' | ||
import { useMovieCalendar } from 'features/offer/components/MoviesScreeningCalendar/MovieCalendarContext' | ||
|
||
export const useMoviesScreeningsList = (offerIds: number[]) => { | ||
const { selectedDate } = useMovieCalendar() | ||
const { data: offersWithStocks } = useOffersStocks({ offerIds }) | ||
|
||
const moviesOffers: MovieOffer[] = useMemo(() => { | ||
const filteredOffersWithStocks = filterOffersStocksByDate( | ||
offersWithStocks?.offers || [], | ||
selectedDate | ||
) | ||
|
||
const nextScreeningOffers = getNextMoviesByDate(offersWithStocks?.offers || [], selectedDate) | ||
|
||
return [...filteredOffersWithStocks, ...nextScreeningOffers] | ||
}, [offersWithStocks?.offers, selectedDate]).filter( | ||
(offer) => offer.offer.subcategoryId === SubcategoryIdEnum.SEANCE_CINE | ||
) | ||
|
||
return { | ||
moviesOffers, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters