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

(BSR) fix(test): change date mock #7297

Merged
merged 1 commit into from
Nov 28, 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
58 changes: 44 additions & 14 deletions src/ui/components/tiles/HorizontalOfferTile.native.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mockdate from 'mockdate'
import React from 'react'

import { navigate } from '__mocks__/@react-navigation/native'
Expand All @@ -11,7 +12,7 @@ import { subcategoriesDataTest } from 'libs/subcategories/fixtures/subcategories
import { Offer } from 'shared/offer/types'
import { mockServer } from 'tests/mswServer'
import { reactQueryProviderHOC } from 'tests/reactQueryProviderHOC'
import { fireEvent, render, screen } from 'tests/utils'
import { userEvent, render, screen } from 'tests/utils'

import { HorizontalOfferTile } from './HorizontalOfferTile'

Expand All @@ -38,7 +39,11 @@ jest.mock('libs/algolia/analytics/SearchAnalyticsWrapper', () => ({

jest.mock('libs/firebase/analytics/analytics')

const user = userEvent.setup()

describe('HorizontalOfferTile component', () => {
jest.useFakeTimers()

beforeEach(() => {
mockServer.getApi<SubcategoriesResponseModelv2>(`/v1/subcategories/v2`, subcategoriesDataTest)
})
Expand All @@ -49,7 +54,7 @@ describe('HorizontalOfferTile component', () => {
analyticsParams: mockAnalyticsParams,
})

fireEvent.press(screen.getByRole('link'))
user.press(screen.getByRole('link'))

await screen.findByText(mockOffer.offer.name)

Expand All @@ -66,7 +71,7 @@ describe('HorizontalOfferTile component', () => {
<HorizontalOfferTile offer={mockOffer} analyticsParams={mockAnalyticsParams} />
)
)
fireEvent.press(screen.getByRole('link'))
user.press(screen.getByRole('link'))

await screen.findByText(mockOffer.offer.name)

Expand All @@ -87,7 +92,7 @@ describe('HorizontalOfferTile component', () => {
})

const hitComponent = screen.getByRole('link')
fireEvent.press(hitComponent)
user.press(hitComponent)

await screen.findByText(mockOffer.offer.name)

Expand Down Expand Up @@ -129,7 +134,7 @@ describe('HorizontalOfferTile component', () => {
analyticsParams: mockAnalyticsParams,
})

fireEvent.press(screen.getByRole('link'))
user.press(screen.getByRole('link'))

await screen.findByText(mockOffer.offer.name)

Expand All @@ -142,7 +147,7 @@ describe('HorizontalOfferTile component', () => {
analyticsParams: mockAnalyticsParams,
})

fireEvent.press(screen.getByRole('link'))
user.press(screen.getByRole('link'))

await screen.findByText(mockOffer.offer.name)

Expand All @@ -151,21 +156,46 @@ describe('HorizontalOfferTile component', () => {
})

describe('When offer is a `SEANCE_CINE`', () => {
const OCTOBER_5_2020 = 1601856000
const NOVEMBER_1_2020 = new Date(2020, 10, 1) // This date is used as now
const NOVEMBER_12_2020 = 1605139200
imouandjolobe-pass marked this conversation as resolved.
Show resolved Hide resolved

const defaultMovieName = 'La petite sirène'
const defaultMovieScreeningOffer = {
...mockOffer.offer,
subcategoryId: SubcategoryIdEnum.SEANCE_CINE,
name: defaultMovieName,
}

it('should format releaseDate when a valid one is given', async () => {
const validReleaseDate = 1722988800 // 7 août 2024
beforeEach(() => {
mockdate.set(NOVEMBER_1_2020)
})

it('should format releaseDate when release date is before now', async () => {
imouandjolobe-pass marked this conversation as resolved.
Show resolved Hide resolved
const movieScreeningOfferWithValidReleaseDate = {
...mockOffer,
offer: {
...defaultMovieScreeningOffer,
releaseDate: OCTOBER_5_2020,
},
}

renderHorizontalOfferTile({
offer: movieScreeningOfferWithValidReleaseDate,
analyticsParams: mockAnalyticsParams,
})

await screen.findByText(defaultMovieName)

expect(await screen.findByText('Sorti le 5 octobre 2020')).toBeOnTheScreen()
})

it('should format releaseDate when release date is after now', async () => {
const movieScreeningOfferWithValidReleaseDate = {
...mockOffer,
offer: {
...defaultMovieScreeningOffer,
releaseDate: validReleaseDate,
releaseDate: NOVEMBER_12_2020,
},
}

Expand All @@ -176,11 +206,11 @@ describe('HorizontalOfferTile component', () => {

await screen.findByText(defaultMovieName)

expect(await screen.findByText('Sorti le 7 août 2024')).toBeOnTheScreen()
expect(await screen.findByText('Dès le 12 novembre 2020')).toBeOnTheScreen()
})

it('should not format releaseDate when an invalid one is given', async () => {
const invalidReleaseDate = '1722988800'
const invalidReleaseDate = '1601856000'
const movieScreeningOfferWithInvalidReleaseDate = {
...mockOffer,
offer: {
Expand All @@ -196,15 +226,15 @@ describe('HorizontalOfferTile component', () => {

await screen.findByText(defaultMovieName)

expect(screen.queryByText('Sorti le 7 août 2024')).not.toBeOnTheScreen()
expect(screen.queryByText('Sorti le 5 octobre 2020')).not.toBeOnTheScreen()
})

it('should format dates when no releaseDate is given', async () => {
const movieScreeningOfferWithoutReleaseDate = {
...mockOffer,
offer: {
...defaultMovieScreeningOffer,
dates: [1732721400], // 27 novembre 2024
dates: [NOVEMBER_12_2020],
},
}

Expand All @@ -215,7 +245,7 @@ describe('HorizontalOfferTile component', () => {

await screen.findByText(defaultMovieName)

expect(await screen.findByText('27 novembre 2024')).toBeOnTheScreen()
expect(await screen.findByText('12 novembre 2020')).toBeOnTheScreen()
})
})
})
Expand Down
4 changes: 1 addition & 3 deletions src/ui/components/tiles/HorizontalOfferTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ export const HorizontalOfferTile = ({
const { logClickOnOffer } = useLogClickOnOffer()

const isOfferAMovieScreeningWithAReleaseDate =
subcategoryId === SubcategoryIdEnum.SEANCE_CINE &&
releaseDate &&
typeof releaseDate === 'number' // we do this because for now, some offers' releaseDate attribute have the wrong type
subcategoryId === SubcategoryIdEnum.SEANCE_CINE && typeof releaseDate === 'number' // we do this because for now, some offers' releaseDate attribute have the wrong type

const timestampsInMillis = dates?.map((timestampInSec) => timestampInSec * 1000)
const offerId = Number(objectID)
Expand Down