Files
iNaturalistReactNative/tests/unit/components/Projects/Projects.test.js
Ken-ichi f7dc08a704 Suggestions fixes (#972)
* Bugfix: TaxonDetails was crashing if it received a null taxon
* Send lat and lng instead of latitude and longitude to the score_image
  endpoint
* Show offline suggestions when you are offline
* Show notice when viewing offline suggestions
* Moved code unique to useOnlineSuggestions into that file
* Ensure we use a medium size image to get suggestions when dealing with
  remote URLs
* More logging around React Query retries
* Use default retry logic for useAuthenticatedQuery
* Made a module-resolver shortcut for tests
* Move offline notice above top suggestion; hide when offlines exist but onlines do too
2023-12-15 19:58:12 -08:00

79 lines
2.5 KiB
JavaScript

import { faker } from "@faker-js/faker";
import { fireEvent, screen } from "@testing-library/react-native";
import ProjectsContainer from "components/Projects/ProjectsContainer";
import initI18next from "i18n/initI18next";
import React from "react";
import factory from "tests/factory";
import { renderComponent } from "tests/helpers/render";
const mockedNavigate = jest.fn( );
const mockProject = factory( "RemoteProject", {
icon: faker.image.url( ),
title: faker.lorem.sentence( )
} );
jest.mock( "sharedHooks/useAuthenticatedQuery", ( ) => ( {
__esModule: true,
default: ( ) => ( {
data: [mockProject]
} )
} ) );
jest.mock( "@react-navigation/native", ( ) => {
const actualNav = jest.requireActual( "@react-navigation/native" );
return {
...actualNav,
useNavigation: () => ( {
navigate: mockedNavigate,
setOptions: jest.fn( )
} ),
useRoute: () => ( {} )
};
} );
// react-native-paper's TextInput does a bunch of async stuff that's hard to
// control in a test, so we're just mocking it here.
jest.mock( "react-native-paper", () => {
const RealModule = jest.requireActual( "react-native-paper" );
const MockTextInput = props => {
const MockName = "mock-text-input";
// eslint-disable-next-line react/jsx-props-no-spreading
return <MockName {...props}>{props.children}</MockName>;
};
MockTextInput.Icon = RealModule.TextInput.Icon;
const MockedModule = {
...RealModule,
// eslint-disable-next-line react/jsx-props-no-spreading
// TextInput: props => <View {...props}>{props.children}</View>
TextInput: MockTextInput
};
return MockedModule;
} );
describe( "Projects", ( ) => {
beforeAll( async ( ) => {
await initI18next( );
jest.useFakeTimers( );
} );
it( "should not have accessibility errors", async ( ) => {
const projects = <ProjectsContainer />;
expect( projects ).toBeAccessible( );
} );
it( "should display project search results", ( ) => {
renderComponent( <ProjectsContainer /> );
const input = screen.getByTestId( "ProjectSearch.input" );
fireEvent.changeText( input, "butterflies" );
expect( screen.getByText( mockProject.title ) ).toBeTruthy( );
expect( screen.getByTestId( `Project.${mockProject.id}.photo` ).props.source )
.toStrictEqual( { uri: mockProject.icon } );
fireEvent.press( screen.getByTestId( `Project.${mockProject.id}` ) );
expect( mockedNavigate ).toHaveBeenCalledWith( "ProjectDetails", {
id: mockProject.id
} );
} );
} );