Files
iNaturalistReactNative/tests/unit/components/SharedComponents/ObservationsFlashList/ObsUploadStatus.test.js
Amanda Bullington aa167c669d Refactor upload functionality into hook/zustand (#1606)
* Refactor upload code into hook and zustand slice

* Create a single source of truth for toolbar and individual progress

* Fix unit tests

* Fix more tests

* Continue trying to simplify upload progress and status code

* Continued overhaul of upload code

* Fix exclamation point in Toolbar

* Fix total toolbar progress

* Fix tests

* Update snapshots

* Code cleanup

* Update MyObs test

* Fix most tests by not deleting realm

* Keep attempting to make toolbar text clear in subsequent tests

* Mock Zustand and reset state after all tests

In theory this should make testing with Zustand simpler. The real kicker is
that resetting state will fail if we're not careful about manually replacing
nested objects in Zustand state updates.

* Bugfix: removeAllFilesFromDirectory works with File objects, not raw paths

* Update status text when uploads canceled

* Only show red exclamation rotating icon after all uploads attempted

* Fix progress bar with zustand nested object

* Fix toolbar progress

---------

Co-authored-by: Ken-ichi Ueda <kenichi.ueda@gmail.com>
2024-05-30 20:36:50 -07:00

107 lines
2.6 KiB
JavaScript

import {
screen
} from "@testing-library/react-native";
import ObsUploadStatus from "components/SharedComponents/ObservationsFlashList/ObsUploadStatus";
import i18next from "i18next";
import React from "react";
import useStore from "stores/useStore";
import factory from "tests/factory";
import faker from "tests/helpers/faker";
import { renderComponent } from "tests/helpers/render";
const mockUnsyncedObservation = factory( "LocalObservation", {
_synced_at: null
} );
const mockEditedObservation = factory( "LocalObservation", {
_synced_at: faker.date.past( ),
_updated_at: faker.date.future( )
} );
const initialStoreState = useStore.getState( );
beforeAll( ( ) => {
useStore.setState( initialStoreState, true );
} );
describe( "ObsUploadStatus", () => {
it( "displays a pending upload for an unsynced observation", () => {
useStore.setState( {
totalUploadProgress: [
{
uuid: mockUnsyncedObservation.uuid,
totalProgress: 0
}
]
} );
renderComponent(
<ObsUploadStatus
observation={mockUnsyncedObservation}
showUploadStatus
/>
);
const icon = screen.getByLabelText( i18next.t( "Start-upload" ) );
expect( icon ).toBeVisible( );
} );
it( "displays a pending upload for a locally edited observation", () => {
useStore.setState( {
totalUploadProgress: [
{
uuid: mockEditedObservation.uuid,
totalProgress: 0
}
]
} );
renderComponent(
<ObsUploadStatus
observation={mockEditedObservation}
showUploadStatus
/>
);
const icon = screen.getByLabelText( i18next.t( "Start-upload" ) );
expect( icon ).toBeVisible( );
} );
it( "displays an upload in progress", async ( ) => {
useStore.setState( {
totalUploadProgress: [
{
uuid: mockUnsyncedObservation.uuid,
totalProgress: 0.05
}
]
} );
renderComponent(
<ObsUploadStatus
observation={mockUnsyncedObservation}
showUploadStatus
/>
);
const progressIcon = screen.getByLabelText( i18next.t( "Upload-in-progress" ) );
expect( progressIcon ).toBeVisible( );
} );
it( "displays a completed upload", async () => {
useStore.setState( {
totalUploadProgress: [
{
uuid: mockUnsyncedObservation.uuid,
totalProgress: 1
}
]
} );
renderComponent(
<ObsUploadStatus
observation={mockUnsyncedObservation}
showUploadStatus
/>
);
const a11yLabel = screen.getByLabelText( i18next.t( "Upload-Complete" ) );
expect( a11yLabel ).toBeVisible( );
} );
} );