-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Land user on Suggestions when one photo imported (#1824)
* Create navigation test for PhotoGallery * Code and test land user on Suggestions when one photo imported
- Loading branch information
1 parent
abd4bce
commit 92cafdd
Showing
4 changed files
with
121 additions
and
2 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
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,104 @@ | ||
import { | ||
screen, | ||
userEvent, | ||
waitFor, | ||
within | ||
} from "@testing-library/react-native"; | ||
import initI18next from "i18n/initI18next"; | ||
import * as rnImagePicker from "react-native-image-picker"; | ||
import useStore from "stores/useStore"; | ||
import faker from "tests/helpers/faker"; | ||
import { renderApp } from "tests/helpers/render"; | ||
import setupUniqueRealm from "tests/helpers/uniqueRealm"; | ||
|
||
// We're explicitly testing navigation here so we want react-navigation | ||
// working normally | ||
jest.unmock( "@react-navigation/native" ); | ||
|
||
// UNIQUE REALM SETUP | ||
const mockRealmIdentifier = __filename; | ||
const { mockRealmModelsIndex, uniqueRealmBeforeAll, uniqueRealmAfterAll } = setupUniqueRealm( | ||
mockRealmIdentifier | ||
); | ||
jest.mock( "realmModels/index", ( ) => mockRealmModelsIndex ); | ||
jest.mock( "providers/contexts", ( ) => { | ||
const originalModule = jest.requireActual( "providers/contexts" ); | ||
return { | ||
__esModule: true, | ||
...originalModule, | ||
RealmContext: { | ||
...originalModule.RealmContext, | ||
useRealm: ( ) => global.mockRealms[mockRealmIdentifier], | ||
useQuery: ( ) => [] | ||
} | ||
}; | ||
} ); | ||
beforeAll( uniqueRealmBeforeAll ); | ||
afterAll( uniqueRealmAfterAll ); | ||
// /UNIQUE REALM SETUP | ||
|
||
beforeAll( async () => { | ||
await initI18next(); | ||
jest.useFakeTimers( ); | ||
} ); | ||
|
||
const mockAsset = [{ | ||
uri: faker.image.url( ) | ||
}]; | ||
|
||
const mockMultipleAssets = [{ | ||
uri: faker.image.url( ) | ||
}, { | ||
uri: faker.image.url( ) | ||
}]; | ||
|
||
jest.mock( "react-native-image-picker", ( ) => ( { | ||
launchImageLibrary: jest.fn( ) | ||
} ) ); | ||
|
||
const actor = userEvent.setup( ); | ||
|
||
const navigateToPhotoImporter = async ( ) => { | ||
expect( await screen.findByText( /Log in to contribute/ ) ).toBeVisible( ); | ||
const tabBar = await screen.findByTestId( "CustomTabBar" ); | ||
const addObsButton = await within( tabBar ).findByLabelText( "Add observations" ); | ||
await actor.press( addObsButton ); | ||
const photoImporter = await screen.findByLabelText( "Photo importer" ); | ||
await actor.press( photoImporter ); | ||
}; | ||
|
||
describe( "PhotoGallery navigation", ( ) => { | ||
beforeEach( ( ) => { | ||
useStore.setState( { isAdvancedUser: true } ); | ||
} ); | ||
|
||
it( "advances to Suggestions when one photo is selected", async ( ) => { | ||
jest.spyOn( rnImagePicker, "launchImageLibrary" ).mockImplementation( | ||
( ) => ( { | ||
assets: mockAsset | ||
} ) | ||
); | ||
renderApp( ); | ||
await navigateToPhotoImporter( ); | ||
const suggestionsText = await screen.findByText( /Add an ID Later/ ); | ||
await waitFor( ( ) => { | ||
// user should land on Suggestions | ||
expect( suggestionsText ).toBeTruthy( ); | ||
} ); | ||
} ); | ||
|
||
it( "advances to GroupPhotos when multiple photos are selected", async ( ) => { | ||
jest.spyOn( rnImagePicker, "launchImageLibrary" ).mockImplementation( | ||
( ) => ( { | ||
assets: mockMultipleAssets | ||
} ) | ||
); | ||
renderApp( ); | ||
await navigateToPhotoImporter( ); | ||
const groupPhotosText = await screen.findByText( /Group Photos/ ); | ||
await waitFor( ( ) => { | ||
// user should land on GroupPhotos | ||
expect( groupPhotosText ).toBeTruthy( ); | ||
} ); | ||
} ); | ||
} ); |
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
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,8 @@ | ||
import { NativeModules } from "react-native"; | ||
|
||
// Mock the ImagePickerManager native module to allow us to unit test the JavaScript code | ||
NativeModules.ImagePickerManager = { | ||
showImagePicker: jest.fn(), | ||
launchCamera: jest.fn(), | ||
launchImageLibrary: jest.fn() | ||
}; |