Skip to content

Commit

Permalink
Fix tests for Explore navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
albullington committed Mar 26, 2024
1 parent 72f0efa commit 5634b4a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 67 deletions.
22 changes: 15 additions & 7 deletions src/components/Explore/RootExploreContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,21 @@ const RootExploreContainerWithContext = ( ): Node => {
const onPermissionGranted = async ( ) => {
if ( state.place_guess ) { return; }
const location = await fetchUserLocation( );
dispatch( {
type: EXPLORE_ACTION.SET_PLACE,
placeName: t( "Nearby" ),
lat: location?.latitude,
lng: location?.longitude,
radius: 50
} );
console.log( location, "location in onPermissionGranted" );
if ( !location || !location.latitude ) {
dispatch( {
type: EXPLORE_ACTION.SET_PLACE,
placeName: t( "Worldwide" )
} );
} else {
dispatch( {
type: EXPLORE_ACTION.SET_PLACE,
placeName: t( "Nearby" ),
lat: location?.latitude,
lng: location?.longitude,
radius: 50
} );
}
};

const onPermissionDenied = ( ) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/MyObservations/MyObservationsEmpty.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const MyObservationsEmpty = ( { isFetchingNextPage }: Props ): Node => {
className="mb-2"
text={t( "EXPLORE-OBSERVATIONS" )}
level="focus"
onPress={( ) => navigation.navigate( "Explore" )}
accessibilityLabel={t( "Explore" )}
onPress={( ) => navigation.navigate( "RootExplore" )}
accessibilityLabel={t( "See-observations-in-explore" )}
accessibilityHint={t( "Navigates-to-explore" )}
/>
</View>
Expand Down
5 changes: 3 additions & 2 deletions src/components/SharedComponents/SpeciesSeenCheckmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ const SpeciesSeenCheckmark = ( {
["searchObservations", taxonId],
optsWithAuth => searchObservations(
{
user_id: currentUser.id,
user_id: currentUser?.id,
per_page: 0,
taxon_id: taxonId
},
optsWithAuth
),
{
keepPreviousData: false
keepPreviousData: false,
enabled: !!taxonId && !!currentUser?.id
}
);

Expand Down
3 changes: 3 additions & 0 deletions src/i18n/l10n/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -1861,3 +1861,6 @@ See-observations-by-this-user-in-Explore = See observations by this user in Expl
# Accessibility label for Explore button on TaxonDetails screen
See-observations-of-this-taxon-in-explore = See observations of this taxon in explore
# Accessibility label for Explore button in MyObservationsEmpty for logged out user
See-observations-in-explore = See observations in explore
4 changes: 4 additions & 0 deletions src/i18n/l10n/en.ftl.json
Original file line number Diff line number Diff line change
Expand Up @@ -1369,5 +1369,9 @@
"See-observations-of-this-taxon-in-explore": {
"comment": "Accessibility label for Explore button on TaxonDetails screen",
"val": "See observations of this taxon in explore"
},
"See-observations-in-explore": {
"comment": "Accessibility label for Explore button in MyObservationsEmpty for logged out user",
"val": "See observations in explore"
}
}
3 changes: 3 additions & 0 deletions src/i18n/strings.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -1861,3 +1861,6 @@ See-observations-by-this-user-in-Explore = See observations by this user in Expl
# Accessibility label for Explore button on TaxonDetails screen
See-observations-of-this-taxon-in-explore = See observations of this taxon in explore
# Accessibility label for Explore button in MyObservationsEmpty for logged out user
See-observations-in-explore = See observations in explore
96 changes: 40 additions & 56 deletions tests/integration/navigation/Explore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ afterAll( uniqueRealmAfterAll );
beforeAll( async () => {
await initI18next();
jest.useFakeTimers( );
inatjs.observations.speciesCounts.mockResolvedValue( makeResponse( [{
count: 1,
taxon: mockTaxon
}] ) );
inatjs.observations.search.mockResolvedValue( makeResponse( mockObservations ) );
inatjs.taxa.fetch.mockResolvedValue( makeResponse( [mockTaxon] ) );
} );

const actor = userEvent.setup( );
Expand All @@ -85,21 +91,36 @@ async function navigateToRootExplore( ) {
await actor.press( exploreButton );
}

describe( "Explore navigation", ( ) => {
describe( "from MyObs", ( ) => {
beforeEach( async ( ) => {
// Write mock observation to realm
safeRealmWrite( global.mockRealms[__filename], ( ) => {
global.mockRealms[__filename].create( "Observation", mockObservations[0] );
}, "write mock observation, navigation/Explore test from MyObs" );

await signIn( mockUser, { realm: global.mockRealms[__filename] } );
describe( "logged out", ( ) => {
describe( "from MyObservationsEmpty for logged out user", ( ) => {
it( "should display species view with no back button", async ( ) => {
renderApp( );
expect( await screen.findByText( /Log in to contribute/ ) ).toBeVisible( );
const exploreButton = await screen.findByLabelText( /See observations in explore/ );
await actor.press( exploreButton );
const speciesViewIcon = await screen.findByLabelText( /Species View/ );
expect( speciesViewIcon ).toBeVisible( );
const backButton = screen.queryByTestId( "Explore.BackButton" );
expect( backButton ).toBeFalsy( );
} );
} );
} );

afterEach( ( ) => {
signOut( { realm: global.mockRealms[__filename] } );
} );
describe( "logged in", ( ) => {
beforeEach( async ( ) => {
// Write mock observation to realm
safeRealmWrite( global.mockRealms[__filename], ( ) => {
global.mockRealms[__filename].create( "Observation", mockObservations[0] );
}, "write mock observation, navigation/Explore test" );

await signIn( mockUser, { realm: global.mockRealms[__filename] } );
} );

afterEach( ( ) => {
signOut( { realm: global.mockRealms[__filename] } );
} );

describe( "from MyObs", ( ) => {
describe( "from MyObs toolbar", ( ) => {
it( "should show observations view and navigate back to MyObs", async ( ) => {
renderApp( );
Expand All @@ -123,10 +144,6 @@ describe( "Explore navigation", ( ) => {
} );

describe( "from TaxonDetails", ( ) => {
beforeEach( ( ) => {
inatjs.taxa.fetch.mockResolvedValue( makeResponse( [mockTaxon] ) );
} );

it( "should show observations view and navigate back to TaxonDetails", async ( ) => {
renderApp( );
await navigateToObsDetails( );
Expand Down Expand Up @@ -189,10 +206,6 @@ describe( "Explore navigation", ( ) => {
inatjs.relationships.search.mockResolvedValue( makeResponse( {
results: []
} ) );
inatjs.observations.speciesCounts.mockResolvedValue( makeResponse( [{
count: 1,
taxon: factory( "RemoteTaxon" )
}] ) );
} );

it( "should show species view and navigate back to UserProfile", async ( ) => {
Expand Down Expand Up @@ -220,26 +233,7 @@ describe( "Explore navigation", ( ) => {
} );

describe( "from bottom tab navigator Explore button", ( ) => {
beforeEach( async ( ) => {
// Write mock observation to realm
safeRealmWrite( global.mockRealms[__filename], ( ) => {
global.mockRealms[__filename].create( "Observation", mockObservations[0] );
}, "write mock observation, navigation/Explore test from MyObs" );

await signIn( mockUser, { realm: global.mockRealms[__filename] } );
inatjs.observations.search.mockResolvedValue( makeResponse( mockObservations ) );
inatjs.observations.speciesCounts.mockResolvedValue( makeResponse( [{
count: 1,
taxon: mockTaxon
}] ) );
inatjs.taxa.fetch.mockResolvedValue( makeResponse( [mockTaxon] ) );
} );

afterEach( ( ) => {
signOut( { realm: global.mockRealms[__filename] } );
} );

describe( "from Explore -> TaxonDetails -> Explore -> TaxonDetails", ( ) => {
describe( "from RootExplore -> X -> Explore -> back", ( ) => {
it( "should navigate from TaxonDetails to Explore and back to TaxonDetails", async ( ) => {
renderApp( );
await navigateToRootExplore( );
Expand All @@ -249,12 +243,6 @@ describe( "Explore navigation", ( ) => {
await actor.press( firstTaxon );
const taxonDetailsExploreButton = await screen.findByLabelText( /See observations of this taxon in explore/ );
await actor.press( taxonDetailsExploreButton );
expect( inatjs.observations.search ).toHaveBeenCalledWith( expect.objectContaining( {
taxon_id: mockTaxon.id,
verifiable: true
} ), {
api_token: TEST_JWT
} );
const defaultGlobalLocation = await screen.findByText( /Worldwide/ );
expect( defaultGlobalLocation ).toBeVisible( );
const observationsViewIcon = await screen.findByLabelText( /Observations View/ );
Expand All @@ -263,17 +251,13 @@ describe( "Explore navigation", ( ) => {
await actor.press( backButton );
expect( taxonDetailsExploreButton ).toBeVisible( );
} );
} );

describe( "from Explore -> UserProfile -> Explore -> UserProfile", ( ) => {
beforeEach( ( ) => {
it( "should navigate from UserProfile to Explore and back to UserProfile", async ( ) => {
inatjs.users.fetch.mockResolvedValue( makeResponse( [mockUser] ) );
inatjs.relationships.search.mockResolvedValue( makeResponse( {
results: []
} ) );
} );

it( "should navigate from UserProfile to Explore and back to UserProfile", async ( ) => {
inatjs.observations.fetch.mockResolvedValue( makeResponse( mockObservations[0] ) );
renderApp( );
await navigateToRootExplore( );
const speciesViewIcon = await screen.findByLabelText( /Species View/ );
Expand All @@ -283,9 +267,11 @@ describe( "Explore navigation", ( ) => {
await actor.press( observationsRadioButton );
const confirmButton = await screen.findByText( /EXPLORE OBSERVATIONS/ );
await actor.press( confirmButton );
const gridView = await screen.findByTestId( "SegmentedButton.grid" );
await actor.press( gridView );
const firstObservation = await screen.findByTestId( `MyObservationsPressable.${obs.uuid}` );
await actor.press( firstObservation );
const userProfileButton = await screen.findByLabelText( `User @${obs.user.login}` );
const userProfileButton = await screen.findByLabelText( `User @${mockUser.login}` );
await actor.press( userProfileButton );
const observationsButton = await screen.findByLabelText( /See observations by this user in Explore/ );
await actor.press( observationsButton );
Expand All @@ -312,9 +298,7 @@ describe( "Explore navigation", ( ) => {
jest.spyOn( ReactNativePermissions, "checkMultiple" )
.mockResolvedValueOnce( mockedPermissions );
renderApp( );
expect( await screen.findByText( /Welcome back/ ) ).toBeVisible( );
const exploreButton = await screen.findByLabelText( /Navigate to explore screen/ );
await actor.press( exploreButton );
await navigateToRootExplore( );
const speciesViewIcon = await screen.findByLabelText( /Species View/ );
expect( speciesViewIcon ).toBeVisible( );
const nearbyText = await screen.findByText( /Nearby/ );
Expand Down

0 comments on commit 5634b4a

Please sign in to comment.