Files
iNaturalistReactNative/tests/unit/components/ObsEdit/DeleteObservationDialog.test.js
Amanda Bullington 7a98b6faf1 Timeless dates (#457)
* Add DateDisplay to ObsCard and make first pass at translation strings

* Add failing tests (due to lack of localization) for timeless dates

* WIP: trying to ensure i18next gets initialized before tests run

The remaining test failures might be legit. This probably breaks the actual
app, though.

* Got the rest of the tests working

* Updated tests to assume UTC
* Updated README to advise against using `npx jest` so test runs always have
  the env vars we specify in our `npm test` script
* Moved i18next initialization to an explicitly-named file
* Use i18next init function in app
* Fixed up remaining tests

* Added test for non-English localization of date format

* Cleanup

* Made DateDisplay explicitly handle strings not Dates

* Restore skipped localization tests for MyObservations

* Remove duplicative tests from DateDisplay unit test

* Added note to the README about initializing i18next

* Updated change to DateDisplay in main

---------

Co-authored-by: Ken-ichi Ueda <kenichi.ueda@gmail.com>
2023-02-14 22:14:38 +01:00

137 lines
4.7 KiB
JavaScript

import { fireEvent, screen, waitFor } from "@testing-library/react-native";
import DeleteObservationDialog from "components/ObsEdit/DeleteObservationDialog";
import initI18next from "i18n/initI18next";
import i18next from "i18next";
import inatjs from "inaturalistjs";
import { ObsEditContext } from "providers/contexts";
import ObsEditProvider from "providers/ObsEditProvider";
import React from "react";
import factory from "../../../factory";
import { renderComponent } from "../../../helpers/render";
beforeEach( async ( ) => {
global.realm.write( ( ) => {
global.realm.deleteAll( );
} );
} );
afterEach( ( ) => {
jest.clearAllMocks( );
} );
jest.mock( "providers/ObsEditProvider" );
jest.mock( "@react-navigation/native", ( ) => {
const actualNav = jest.requireActual( "@react-navigation/native" );
return {
...actualNav,
useNavigation: ( ) => ( {
navigate: jest.fn( )
} )
};
} );
// Mock ObservationProvider so it provides a specific array of observations
// without any current observation or ability to update or fetch
// observations
const mockObsEditProviderWithObs = obs => ObsEditProvider.mockImplementation( ( { children } ) => (
// eslint-disable-next-line react/jsx-no-constructed-context-values
<ObsEditContext.Provider value={{
currentObservation: obs[0],
deleteLocalObservation: ( ) => {
global.realm.write( ( ) => {
const observation = global.realm.objectForPrimaryKey( "Observation", obs[0].uuid );
if ( observation ) {
global.realm.delete( observation );
}
} );
}
}}
>
{children}
</ObsEditContext.Provider>
) );
const renderDeleteDialog = ( ) => renderComponent(
<ObsEditProvider>
<DeleteObservationDialog deleteDialogVisible hideDialog={( ) => jest.fn( )} />
</ObsEditProvider>
);
const getLocalObservation = uuid => global.realm
.objectForPrimaryKey( "Observation", uuid );
describe( "delete observation", ( ) => {
beforeAll( async ( ) => {
await initI18next( );
} );
describe( "delete an unsynced observation", ( ) => {
it( "should delete an observation from realm", async ( ) => {
const observations = [factory( "LocalObservation", {
_synced_at: null
} )];
global.realm.write( ( ) => {
global.realm.create( "Observation", observations[0] );
} );
const localObservation = getLocalObservation( observations[0].uuid );
expect( localObservation ).toBeTruthy( );
mockObsEditProviderWithObs( observations );
renderDeleteDialog( );
const deleteButtonText = i18next.t( "Yes-delete-observation" );
const deleteButton = screen.queryByText( deleteButtonText );
expect( deleteButton ).toBeTruthy( );
fireEvent.press( deleteButton );
expect( getLocalObservation( observations[0].uuid ) ).toBeFalsy( );
} );
it( "should not make a request to observations/delete", async ( ) => {
await waitFor( ( ) => {
expect( inatjs.observations.delete ).not.toHaveBeenCalled( );
} );
} );
} );
describe( "delete a previously synced observation", ( ) => {
it( "should make a request to observations/delete", async ( ) => {
const observations = [factory( "LocalObservation" )];
global.realm.write( ( ) => {
global.realm.create( "Observation", observations[0] );
} );
const localObservation = getLocalObservation( observations[0].uuid );
expect( localObservation ).toBeTruthy( );
mockObsEditProviderWithObs( observations );
renderDeleteDialog( );
// TODO: figure out why this needs English text and why the one above needs
// the generic text. Probably has to do with User object still being stored in global realm
// between tests
const deleteButton = screen.queryByText( /delete/ );
expect( deleteButton ).toBeTruthy( );
fireEvent.press( deleteButton );
await waitFor( ( ) => {
expect( inatjs.observations.delete ).toHaveBeenCalledTimes( 1 );
} );
expect( getLocalObservation( observations[0].uuid ) ).toBeFalsy( );
} );
} );
describe( "cancel deletion", ( ) => {
it( "should not delete the observation from realm", ( ) => {
const observations = [factory( "LocalObservation" )];
global.realm.write( ( ) => {
global.realm.create( "Observation", observations[0] );
} );
const localObservation = getLocalObservation( observations[0].uuid );
expect( localObservation ).toBeTruthy( );
mockObsEditProviderWithObs( observations );
renderDeleteDialog( );
const cancelButton = screen.queryByText( /Cancel/ );
expect( cancelButton ).toBeTruthy( );
fireEvent.press( cancelButton );
expect( getLocalObservation( observations[0].uuid ) ).toBeTruthy( );
} );
} );
} );