Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clesausse-pass committed Jul 11, 2024
1 parent 4a18641 commit 2eb7f91
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 41 deletions.
12 changes: 7 additions & 5 deletions src/features/auth/pages/login/Login.native.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import React from 'react'
import DeviceInfo from 'react-native-device-info'

import { navigate, useRoute } from '__mocks__/@react-navigation/native'
import { BatchUser } from '__mocks__/libs/react-native-batch'
import * as API from 'api/api'
import {
AccountState,
Expand All @@ -23,6 +21,8 @@ import { usePreviousRoute } from 'features/navigation/helpers/usePreviousRoute'
import { StepperOrigin } from 'features/navigation/RootNavigator/types'
import { FAKE_USER_ID } from 'fixtures/fakeUserId'
import { analytics } from 'libs/analytics'
import { navigate, useRoute } from '__mocks__/@react-navigation/native'
import { BatchUser } from '__mocks__/libs/react-native-batch'
// eslint-disable-next-line no-restricted-imports
import { firebaseAnalytics } from 'libs/firebase/analytics'
import * as useFeatureFlagAPI from 'libs/firebase/firestore/featureFlags/useFeatureFlag'
Expand All @@ -31,7 +31,7 @@ import { NetworkErrorFixture, UnknownErrorFixture } from 'libs/recaptcha/fixture
import { storage } from 'libs/storage'
import { mockServer } from 'tests/mswServer'
import { reactQueryProviderHOC } from 'tests/reactQueryProviderHOC'
import { fireEvent, render, act, screen, simulateWebviewMessage } from 'tests/utils'
import { act, fireEvent, render, screen, simulateWebviewMessage } from 'tests/utils'
import { SUGGESTION_DELAY_IN_MS } from 'ui/components/inputs/EmailInputWithSpellingHelp/useEmailSpellingHelp'
import { SNACK_BAR_TIME_OUT_LONG } from 'ui/components/snackBar/SnackBarContext'

Expand Down Expand Up @@ -804,8 +804,10 @@ function simulateSigninEmailNotValidated() {
function simulateSigninNetworkFailure() {
mockServer.postApi('/v1/signin', {
responseOptions: {
statusCode: 'network-error',
data: 'Network request failed',
data: {
code: 'NETWORK_REQUEST_FAILED',
general: ['Erreur réseau. Tu peux réessayer une fois la connexion réétablie'],
},
},
})
}
Expand Down
22 changes: 12 additions & 10 deletions src/features/bookings/api/useOngoingOrEndedBooking.native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { bookingsSnap } from 'features/bookings/fixtures/bookingsSnap'
import * as useNetInfoContextDefault from 'libs/network/NetInfoWrapper'
import { mockServer } from 'tests/mswServer'
import { reactQueryProviderHOC } from 'tests/reactQueryProviderHOC'
import { act, renderHook } from 'tests/utils'
import { renderHook, waitFor } from 'tests/utils'

jest.mock('libs/react-query/usePersistQuery', () => ({
usePersistQuery: jest.requireActual('react-query').useQuery,
Expand Down Expand Up @@ -38,21 +38,23 @@ describe('useOngoingOrEndedBooking', () => {
const { result } = renderHook(() => useOngoingOrEndedBooking(booking.id), {
wrapper: ({ children }) => reactQueryProviderHOC(children),
})
await act(async () => {})

expect(result.current?.data?.id).toEqual(booking.id)
expect(result.current?.data?.stock.id).toEqual(booking.stock.id)
await waitFor(() => {
expect(result.current?.data?.id).toEqual(booking.id)
expect(result.current?.data?.stock.id).toEqual(booking.stock.id)
})
})

it('should return ended_bookings when there is one', async () => {
const booking = bookingsSnap.ended_bookings[0]
const { result } = renderHook(() => useOngoingOrEndedBooking(booking.id), {
wrapper: ({ children }) => reactQueryProviderHOC(children),
})
await act(async () => {})

expect(result.current?.data?.id).toEqual(booking.id)
expect(result.current?.data?.stock.id).toEqual(booking.stock.id)
await waitFor(() => {
expect(result.current?.data?.id).toEqual(booking.id)
expect(result.current?.data?.stock.id).toEqual(booking.stock.id)
})
})

it('should return null if no ongoing nor ended booking can be found', async () => {
Expand All @@ -61,8 +63,8 @@ describe('useOngoingOrEndedBooking', () => {
wrapper: ({ children }) => reactQueryProviderHOC(children),
})

await act(async () => {})

expect(result.current.data).toBeNull()
await waitFor(() => {
expect(result.current.data).toBeNull()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { PLACEHOLDER_DATA } from 'libs/subcategories/placeholderData'
import { mockAuthContextWithoutUser, mockAuthContextWithUser } from 'tests/AuthContextUtils'
import { mockServer } from 'tests/mswServer'
import { reactQueryProviderHOC } from 'tests/reactQueryProviderHOC'
import { act, fireEvent, render, screen } from 'tests/utils'
import { act, fireEvent, render, screen, waitFor } from 'tests/utils'
import { SNACK_BAR_TIME_OUT } from 'ui/components/snackBar/SnackBarContext'

jest.mock('libs/network/NetInfoWrapper')
Expand Down Expand Up @@ -97,7 +97,9 @@ describe('<OfferCTAButton />', () => {

await act(async () => {})

expect(await screen.findByText('Valider la date')).toBeOnTheScreen()
await waitFor(() => {
expect(screen.getByText('Valider la date')).toBeOnTheScreen()
})
})

it('should display authentication modal when clicking on "Réserver l’offre"', async () => {
Expand Down Expand Up @@ -195,14 +197,14 @@ describe('<OfferCTAButton />', () => {
subcategory: mockSubcategoryNotEvent,
})

await act(async () => {
fireEvent.press(screen.getByText('Accéder à l’offre en ligne'))
})
fireEvent.press(screen.getByText('Accéder à l’offre en ligne'))

expect(mockedOpenUrl).toHaveBeenNthCalledWith(1, 'https://www.google.fr/')
await waitFor(() => {
expect(mockedOpenUrl).toHaveBeenNthCalledWith(1, 'https://www.google.fr/')
})
})

it('should open when pressing button to book the offer if offer is Event', async () => {
it('should open when pressing button to book the offer if offer is Event', async () => {
mockAuthContextWithUser(beneficiaryUser, { persist: true })

renderOfferCTAButton({
Expand All @@ -211,11 +213,11 @@ describe('<OfferCTAButton />', () => {
subcategory: mockSubcategoryNotEvent,
})

await act(async () => {
fireEvent.press(screen.getByText('Accéder à l’offre en ligne'))
})
fireEvent.press(screen.getByText('Accéder à l’offre en ligne'))

expect(mockedOpenUrl).toHaveBeenNthCalledWith(1, 'https://www.google.fr/')
await waitFor(() => {
expect(mockedOpenUrl).toHaveBeenNthCalledWith(1, 'https://www.google.fr/')
})
})

it('should log BookingConfirmation when pressing button to book the offer', async () => {
Expand All @@ -226,13 +228,13 @@ describe('<OfferCTAButton />', () => {
offer: { ...offerResponseSnap, ...offerDigitalAndFree },
})

await act(async () => {
fireEvent.press(screen.getByText('Accéder à l’offre en ligne'))
})
fireEvent.press(screen.getByText('Accéder à l’offre en ligne'))

expect(analytics.logBookingConfirmation).toHaveBeenNthCalledWith(1, {
bookingId: 123,
offerId: 116656,
await waitFor(() => {
expect(analytics.logBookingConfirmation).toHaveBeenNthCalledWith(1, {
bookingId: 123,
offerId: 116656,
})
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,16 @@ describe('ChangePassword', () => {
fireEvent.press(screen.getByTestId('Enregistrer les modifications'))
})

expect(mockShowSuccessSnackBar).toHaveBeenCalledWith({
message: 'Ton mot de passe est modifié',
timeout: SNACK_BAR_TIME_OUT,
})
expect(navigate).toHaveBeenCalledWith('TabNavigator', { screen: 'Profile' })
expect(analytics.logHasChangedPassword).toHaveBeenCalledWith({
from: 'personaldata',
reason: 'changePassword',
await waitFor(() => {
expect(mockShowSuccessSnackBar).toHaveBeenCalledWith({
message: 'Ton mot de passe est modifié',
timeout: SNACK_BAR_TIME_OUT,
})
expect(navigate).toHaveBeenCalledWith('TabNavigator', { screen: 'Profile' })
expect(analytics.logHasChangedPassword).toHaveBeenCalledWith({
from: 'personaldata',
reason: 'changePassword',
})
})
})

Expand Down Expand Up @@ -149,6 +151,8 @@ describe('ChangePassword', () => {
fireEvent.press(continueButton)
})

expect(screen.getByText('Mot de passe incorrect')).toBeOnTheScreen()
await waitFor(() => {
expect(screen.getByText('Mot de passe incorrect')).toBeOnTheScreen()
})
})
})

0 comments on commit 2eb7f91

Please sign in to comment.