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

(PC-32525) fix(culturalSurvey): fix condition return #7052

Merged
merged 1 commit into from
Oct 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jest.mock('features/identityCheck/context/SubscriptionContextProvider', () => ({

jest.mock('libs/network/NetInfoWrapper')

jest.spyOn(useFeatureFlagAPI, 'useFeatureFlag').mockReturnValue(true)
const useFeatureFlagSpy = jest.spyOn(useFeatureFlagAPI, 'useFeatureFlag').mockReturnValue(false)
const apiPostGoogleAuthorize = jest.spyOn(API.api, 'postNativeV1OauthGoogleAuthorize')
const getModelSpy = jest.spyOn(DeviceInfo, 'getModel')
const getSystemNameSpy = jest.spyOn(DeviceInfo, 'getSystemName')
Expand Down Expand Up @@ -56,6 +56,7 @@ describe('<SSOButton />', () => {
})

it('should sign in with device info when sso button is clicked', async () => {
useFeatureFlagSpy.mockReturnValueOnce(true)
getModelSpy.mockReturnValueOnce('iPhone 13')
getSystemNameSpy.mockReturnValueOnce('iOS')
mockServer.postApi<SigninResponse>('/v1/oauth/google/authorize', {
Expand All @@ -81,6 +82,7 @@ describe('<SSOButton />', () => {
})

it('should call onSignInFailure when signin fails', async () => {
useFeatureFlagSpy.mockReturnValueOnce(true)
mockServer.postApi<SigninResponse>('/v1/oauth/google/authorize', {
responseOptions: { statusCode: 500 },
})
Expand All @@ -95,6 +97,7 @@ describe('<SSOButton />', () => {
})

it('should log analytics when logging in with sso from signup', async () => {
useFeatureFlagSpy.mockReturnValueOnce(true)
mockServer.postApi<SigninResponse>('/v1/oauth/google/authorize', {
accessToken: 'accessToken',
refreshToken: 'refreshToken',
Expand All @@ -110,6 +113,7 @@ describe('<SSOButton />', () => {
})

it('should log analytics when logging in with sso from login', async () => {
useFeatureFlagSpy.mockReturnValueOnce(true)
mockServer.postApi<SigninResponse>('/v1/oauth/google/authorize', {
accessToken: 'accessToken',
refreshToken: 'refreshToken',
Expand All @@ -133,6 +137,7 @@ describe('<SSOButton />', () => {
})

it('should not log to Sentry on SSO login error', async () => {
useFeatureFlagSpy.mockReturnValueOnce(true)
jest.spyOn(GoogleSignin, 'signIn').mockRejectedValueOnce('GoogleSignIn Error')

renderSSOButton()
Expand All @@ -155,6 +160,7 @@ describe('<SSOButton />', () => {
})

it('should log to Sentry on SSO login error', async () => {
useFeatureFlagSpy.mockReturnValueOnce(true)
jest.spyOn(GoogleSignin, 'signIn').mockRejectedValueOnce('GoogleSignIn Error')

renderSSOButton()
Expand Down
43 changes: 12 additions & 31 deletions src/features/auth/pages/login/Login.native.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { analytics } from 'libs/analytics'
// eslint-disable-next-line no-restricted-imports
import { firebaseAnalytics } from 'libs/firebase/analytics'
import * as useFeatureFlagAPI from 'libs/firebase/firestore/featureFlags/useFeatureFlag'
import { RemoteStoreFeatureFlags } from 'libs/firebase/firestore/types'
import { captureMonitoringError } from 'libs/monitoring'
import { NetworkErrorFixture, UnknownErrorFixture } from 'libs/recaptcha/fixtures'
import { storage } from 'libs/storage'
Expand All @@ -37,10 +36,6 @@ import { SUGGESTION_DELAY_IN_MS } from 'ui/components/inputs/EmailInputWithSpell
import { SNACK_BAR_TIME_OUT_LONG } from 'ui/components/snackBar/SnackBarContext'

import { Login } from './Login'

const useFeatureFlagSpy = jest.spyOn(useFeatureFlagAPI, 'useFeatureFlag')
jest.mock('libs/firebase/remoteConfig/remoteConfig.services')

jest.mock('libs/network/NetInfoWrapper')

jest.mock('libs/monitoring')
Expand Down Expand Up @@ -73,6 +68,7 @@ const apiSignInSpy = jest.spyOn(API.api, 'postNativeV1Signin')
const apiPostGoogleAuthorize = jest.spyOn(API.api, 'postNativeV1OauthGoogleAuthorize')
const getModelSpy = jest.spyOn(DeviceInfo, 'getModel')
const getSystemNameSpy = jest.spyOn(DeviceInfo, 'getSystemName')
const useFeatureFlagSpy = jest.spyOn(useFeatureFlagAPI, 'useFeatureFlag').mockReturnValue(false)

jest.useFakeTimers()

Expand All @@ -97,7 +93,6 @@ jest.mock('react-native/Libraries/Animated/createAnimatedComponent', () => {

describe('<Login/>', () => {
beforeEach(() => {
activateFeatureFlags([RemoteStoreFeatureFlags.WIP_ENABLE_GOOGLE_SSO])
mockServer.postApi<FavoriteResponse>('/v1/me/favorites', favoriteResponseSnap)
mockServer.getApi<OauthStateResponse>('/v1/oauth/state', {
oauthStateToken: 'oauth_state_token',
Expand All @@ -115,6 +110,10 @@ describe('<Login/>', () => {
})

it('should render correctly when feature flag is enabled', async () => {
// We use this hook twice but due to multiple rerender we have to mock the return value this way
// eslint-disable-next-line local-rules/independent-mocks
useFeatureFlagSpy.mockReturnValue(true)

renderLogin()

await screen.findByText('Connecte-toi')
Expand Down Expand Up @@ -146,6 +145,9 @@ describe('<Login/>', () => {
})

it('should sign in when SSO button is clicked with device info', async () => {
// We use this hook twice but due to multiple rerender we have to mock the return value this way
// eslint-disable-next-line local-rules/independent-mocks
useFeatureFlagSpy.mockReturnValue(true)
getModelSpy.mockReturnValueOnce('iPhone 13')
getSystemNameSpy.mockReturnValueOnce('iOS')
mockServer.postApi<SigninResponse>('/v1/oauth/google/authorize', {
Expand Down Expand Up @@ -254,9 +256,6 @@ describe('<Login/>', () => {
})

it('should redirect to NATIVE Cultural Survey WHEN signin is successful and user needs to fill cultural survey', async () => {
activateFeatureFlags([RemoteStoreFeatureFlags.WIP_ENABLE_GOOGLE_SSO])
activateFeatureFlags() // disabled ENABLE_CULTURAL_SURVEY_MANDATORY feature flag

mockMeApiCall({
needsToFillCulturalSurvey: true,
showEligibleCard: false,
Expand All @@ -266,23 +265,8 @@ describe('<Login/>', () => {
await fillInputs()
await act(() => fireEvent.press(screen.getByText('Se connecter')))

expect(navigate).toHaveBeenNthCalledWith(1, 'CulturalSurveyIntro')
})

it('should redirect to home WHEN signin is successful and ENABLE_CULTURAL_SURVEY_MANDATORY enabled', async () => {
activateFeatureFlags([RemoteStoreFeatureFlags.WIP_ENABLE_GOOGLE_SSO])
activateFeatureFlags([RemoteStoreFeatureFlags.ENABLE_CULTURAL_SURVEY_MANDATORY])

mockMeApiCall({
needsToFillCulturalSurvey: true,
showEligibleCard: false,
} as UserProfileResponse)
renderLogin()

await fillInputs()
await act(() => fireEvent.press(screen.getByText('Se connecter')))

expect(navigateToHome).toHaveBeenCalledTimes(1)
expect(navigate).toHaveBeenCalledTimes(1)
expect(navigate).toHaveBeenCalledWith('CulturalSurveyIntro')
})

it('should not redirect to EighteenBirthday WHEN signin is successful and user has already seen eligible card and needs to see it', async () => {
Expand Down Expand Up @@ -498,7 +482,8 @@ describe('<Login/>', () => {

await screen.findByText('Connecte-toi')

expect(analytics.logStepperDisplayed).toHaveBeenNthCalledWith(1, StepperOrigin.PROFILE, 'Login')
expect(analytics.logStepperDisplayed).toHaveBeenCalledTimes(1)
expect(analytics.logStepperDisplayed).toHaveBeenCalledWith(StepperOrigin.PROFILE, 'Login')
})

it('should log analytics when clicking on "Créer un compte" button', async () => {
Expand Down Expand Up @@ -901,7 +886,3 @@ function simulateSigninNetworkFailure() {
function simulateAddToFavorites() {
mockServer.postApi<FavoriteResponse>('/v1/me/favorites', favoriteResponseSnap)
}

const activateFeatureFlags = (activeFeatureFlags: RemoteStoreFeatureFlags[] = []) => {
useFeatureFlagSpy.mockImplementation((flag) => activeFeatureFlags.includes(flag))
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ import { ShareAppWrapper } from 'features/share/context/ShareAppWrapper'
import { ShareAppModalType } from 'features/share/types'
import { beneficiaryUser } from 'fixtures/user'
import { analytics } from 'libs/analytics'
import * as useFeatureFlagAPI from 'libs/firebase/firestore/featureFlags/useFeatureFlag'
import { RemoteStoreFeatureFlags } from 'libs/firebase/firestore/types'
import { BatchUser } from 'libs/react-native-batch'
import { mockAuthContextWithUser } from 'tests/AuthContextUtils'
import { fireEvent, render, screen, waitFor } from 'tests/utils'

import { AccountCreated } from './AccountCreated'

const useFeatureFlagSpy = jest.spyOn(useFeatureFlagAPI, 'useFeatureFlag')
jest.mock('libs/firebase/remoteConfig/remoteConfig.services')

jest.mock('features/profile/api/useResetRecreditAmountToShow')
jest.mock('features/navigation/helpers/navigateToHome')
jest.mock('features/navigation/navigationRef')
Expand All @@ -45,10 +40,6 @@ jest.mock('react-native/Libraries/Animated/createAnimatedComponent', () => {
})

describe('<AccountCreated />', () => {
beforeEach(() => {
activateFeatureFlags()
})

it('should render correctly', async () => {
renderAccountCreated()

Expand All @@ -64,22 +55,8 @@ describe('<AccountCreated />', () => {

await waitFor(() => {
expect(navigateFromRef).not.toHaveBeenCalled()
expect(navigate).toHaveBeenNthCalledWith(1, 'CulturalSurveyIntro', undefined)
})
})

it('should redirect to home page WHEN "On y va !" button is clicked BUT feature flag enableCulturalSurveyMandatory is enabled', async () => {
activateFeatureFlags([RemoteStoreFeatureFlags.ENABLE_CULTURAL_SURVEY_MANDATORY])
renderAccountCreated()

fireEvent.press(await screen.findByLabelText('On y va\u00a0!'))

await waitFor(() => {
expect(navigateFromRef).toHaveBeenCalledWith(
navigateToHomeConfig.screen,
navigateToHomeConfig.params
)
expect(navigate).not.toHaveBeenCalledWith('CulturalSurvey', undefined)
expect(navigate).toHaveBeenCalledTimes(1)
expect(navigate).toHaveBeenCalledWith('CulturalSurveyIntro', undefined)
})
})

Expand Down Expand Up @@ -128,7 +105,3 @@ const renderAccountCreated = () =>
render(<AccountCreated />, {
wrapper: ShareAppWrapper,
})

const activateFeatureFlags = (activeFeatureFlags: RemoteStoreFeatureFlags[] = []) => {
useFeatureFlagSpy.mockImplementation((flag) => activeFeatureFlags.includes(flag))
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import React from 'react'

import * as useFeatureFlagAPI from 'libs/firebase/firestore/featureFlags/useFeatureFlag'
import { checkAccessibilityFor, render, screen } from 'tests/utils/web'

import { AccountCreated } from './AccountCreated'

jest.spyOn(useFeatureFlagAPI, 'useFeatureFlag').mockReturnValue(false)
jest.mock('libs/firebase/remoteConfig/remoteConfig.services')

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

jest.mock('react-native-safe-area-context', () => ({
...(jest.requireActual('react-native-safe-area-context') as Record<string, unknown>),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ describe('<SetEmail />', () => {

useFeatureFlagSpy.mockReturnValueOnce(true) // first call in SetEmail
useFeatureFlagSpy.mockReturnValueOnce(true) // second call in useOAuthState
useFeatureFlagSpy.mockReturnValueOnce(true) // third call in CulturalSurvey

renderSetEmail()

Expand Down Expand Up @@ -324,7 +323,6 @@ describe('<SetEmail />', () => {

useFeatureFlagSpy.mockReturnValueOnce(true) // first call in SetEmail
useFeatureFlagSpy.mockReturnValueOnce(true) // second call in useOAuthState
useFeatureFlagSpy.mockReturnValueOnce(true) // third call in CulturalSurvey

renderSetEmail()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import React from 'react'

import { navigate } from '__mocks__/@react-navigation/native'
import { BeneficiaryAccountCreated } from 'features/identityCheck/pages/confirmation/BeneficiaryAccountCreated'
import * as ShareAppWrapperModule from 'features/share/context/ShareAppWrapper'
import { ShareAppWrapper } from 'features/share/context/ShareAppWrapper'
import { ShareAppModalType } from 'features/share/types'
import { beneficiaryUser, underageBeneficiaryUser } from 'fixtures/user'
import * as useFeatureFlagAPI from 'libs/firebase/firestore/featureFlags/useFeatureFlag'
import { RemoteStoreFeatureFlags } from 'libs/firebase/firestore/types'
import { BatchUser } from 'libs/react-native-batch'
import { mockAuthContextWithUser } from 'tests/AuthContextUtils'
import { fireEvent, render, screen, waitFor } from 'tests/utils'

const useFeatureFlagSpy = jest.spyOn(useFeatureFlagAPI, 'useFeatureFlag')
jest.mock('libs/firebase/remoteConfig/remoteConfig.services')
import { fireEvent, render, screen } from 'tests/utils'

jest.mock('features/auth/context/AuthContext')

Expand Down Expand Up @@ -41,7 +35,6 @@ jest.mock('react-native/Libraries/Animated/createAnimatedComponent', () => {

describe('<BeneficiaryAccountCreated/>', () => {
beforeEach(() => {
activateFeatureFlags()
mockAuthContextWithUser(underageBeneficiaryUser, { persist: true })
})

Expand All @@ -64,7 +57,6 @@ describe('<BeneficiaryAccountCreated/>', () => {

it('should track Batch event when button is clicked', async () => {
renderBeneficiaryAccountCreated()

fireEvent.press(await screen.findByLabelText('C’est parti !'))

expect(BatchUser.trackEvent).toHaveBeenCalledWith('has_validated_subscription')
Expand All @@ -77,46 +69,20 @@ describe('<BeneficiaryAccountCreated/>', () => {
{ ...beneficiaryUser, needsToFillCulturalSurvey: false },
{ persist: true }
)
renderBeneficiaryAccountCreated()

renderBeneficiaryAccountCreated()
fireEvent.press(await screen.findByLabelText('C’est parti !'))

expect(mockShowAppModal).toHaveBeenNthCalledWith(1, ShareAppModalType.BENEFICIARY)
})

it('should not show share app modal when user is supposed to see cultural survey', async () => {
renderBeneficiaryAccountCreated()

fireEvent.press(await screen.findByLabelText('C’est parti !'))

expect(mockShowAppModal).not.toHaveBeenCalled()
})

it('should redirect to native cultural survey page when "C’est parti !"button is clicked and user is supposed to see cultural survey', async () => {
renderBeneficiaryAccountCreated()

fireEvent.press(await screen.findByLabelText('C’est parti !'))

await waitFor(() => {
expect(navigate).toHaveBeenNthCalledWith(1, 'CulturalSurveyIntro', undefined)
})
})

it('should redirect to home page when "C’est parti !" button is clicked BUT feature flag enableCulturalSurveyMandatory is enabled', async () => {
activateFeatureFlags([RemoteStoreFeatureFlags.ENABLE_CULTURAL_SURVEY_MANDATORY])
renderBeneficiaryAccountCreated()

fireEvent.press(await screen.findByLabelText('C’est parti !'))

await waitFor(() => {
expect(navigate).toHaveBeenNthCalledWith(1, 'TabNavigator', { screen: 'Home' })
})
})
})

const renderBeneficiaryAccountCreated = () =>
render(<BeneficiaryAccountCreated />, { wrapper: ShareAppWrapper })

const activateFeatureFlags = (activeFeatureFlags: RemoteStoreFeatureFlags[] = []) => {
useFeatureFlagSpy.mockImplementation((flag) => activeFeatureFlags.includes(flag))
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import React from 'react'

import * as useFeatureFlagAPI from 'libs/firebase/firestore/featureFlags/useFeatureFlag'
import { render, checkAccessibilityFor, screen } from 'tests/utils/web'

import { BeneficiaryAccountCreated } from './BeneficiaryAccountCreated'

jest.spyOn(useFeatureFlagAPI, 'useFeatureFlag').mockReturnValue(false)
jest.mock('libs/firebase/remoteConfig/remoteConfig.services')

jest.mock('features/auth/context/AuthContext')

jest.mock('react-native-safe-area-context', () => ({
Expand Down
Loading
Loading