Files
iNaturalistReactNative/tests/integration/PhotoSharing.test.js
Johannes Klein 020d46f9b9 Add conditions for which the Obswheel tooltip is shown (#2980)
* Update tooltip trigger logic in AddObsButton for logged in

Refines the tooltip display conditions for AddObsButton. Now shows the tooltip for logged-out users after their second observation, and for logged-in users with 50 or fewer observations upon landing on the My Observations screen. Adds comments for future handling of users with more than 50 observations.

* Refactor the state justFinishedSignup into the layout store slice

* Refactor trigger logic and add conditions for when the tooltip would overlap with pivot cards

* Move files

* Basic AddObsButton unit test

* Add mock for addListener

* Basic test for tooltip

* Add tests for two conditions that require a card dismissal

* Wrap setState in act in helper function
2025-06-27 13:19:28 +02:00

98 lines
2.5 KiB
JavaScript

import { act } from "@testing-library/react-native";
import { Platform } from "react-native";
import ShareMenu from "react-native-share-menu";
import { renderApp } from "tests/helpers/render";
const JPEG = "image/jpeg";
const mockNavigate = jest.fn( );
jest.mock( "@react-navigation/native", ( ) => {
const actualNav = jest.requireActual( "@react-navigation/native" );
return {
...actualNav,
useNavigation: ( ) => ( {
navigate: mockNavigate,
addListener: mockNavigate
} )
};
} );
const mockIOSPhoto = {
mimeType: JPEG,
data: [{ data: "file://photo.jpg", mimeType: "image/jpeg" }]
};
const mockAndroidPhoto = {
mimeType: "image/jpeg",
data: "file://photo.jpg"
};
const setupShareMocks = ( ) => {
const mockListeners = [];
ShareMenu.getInitialShare.mockImplementation( callback => {
ShareMenu.__initialShareCallback = callback;
} );
ShareMenu.addNewShareListener.mockImplementation( callback => {
const listener = { callback, remove: jest.fn( ) };
mockListeners.push( listener );
return listener;
} );
return {
simulateInitialShare: shareData => {
ShareMenu.__initialShareCallback?.( shareData );
},
simulateNewShare: shareData => {
mockListeners.forEach( listener => listener.callback( shareData ) );
},
reset: ( ) => {
mockListeners.length = 0;
ShareMenu.__initialShareCallback = null;
}
};
};
describe( "Sharing photos into the app", ( ) => {
let shareHelpers;
beforeEach( ( ) => {
shareHelpers = setupShareMocks( );
} );
afterEach( ( ) => {
shareHelpers.reset( );
// test iOS as default, but test Android in a few specific tests
Platform.OS = "ios";
} );
it( "should handle iOS photo share on app launch", async ( ) => {
renderApp( );
await act( async ( ) => {
shareHelpers.simulateInitialShare( mockIOSPhoto );
} );
expect( mockNavigate ).toHaveBeenCalledWith( "NoBottomTabStackNavigator", {
screen: "PhotoSharing",
params: { item: expect.objectContaining( { mimeType: "image/jpeg" } ) }
} );
} );
it( "should handle Android photo share on app launch", async ( ) => {
Platform.OS = "android";
renderApp( );
await act( async ( ) => {
shareHelpers.simulateInitialShare( mockAndroidPhoto );
} );
expect( mockNavigate ).toHaveBeenCalledWith( "NoBottomTabStackNavigator", {
screen: "PhotoSharing",
params: { item: expect.objectContaining( { mimeType: "image/jpeg" } ) }
} );
} );
} );