Files
iNaturalistReactNative/tests/unit/components/ObsDetails/DataQualityAssessment.test.js
Johannes Klein 5553719a2f Community Taxon improvement DQA (#1291)
* Restyle community taxon section

* Use fave/unfave mutation in case of need_id metric

* Add votes field to Observation

* Change useIsConnected to TS

* Refactor fetching of remote observation into hook

Also includes the code to update local copy of a user's own observation

* Use const as key instead of string

* Remove unused prop

* Only send obs uuid to DQA

* Get obs in DQA container

* Rebuild object that is to check as it was send in nav params

* Refactor qualityGrade

* Refactor set to not loading state

* Update test to reflect fetching of obs

* Refactor too long lines

* Refetch remote observation after success in adding/removing vote

* Change qualitcMetrics to make use of observation.votes as well

Also refactored the object structure to use more efficient and performant filtering and finding in child components.

* Only use data slice for DQA buttons

* Refactor DQAButtons to only work with data slice relevant to that metric

* Special case for needs_id metric

* Also show loading indicator when fetching remote obs

* Add TODO

* Check only for vote of this user

* Optional chaining for user id

* Update test

* Enable fetching of remote if local is falsy

* Invalidate query on mutation

* Only set loading to false after refetch is finished

* Split up DQA test into unit tests and integration tests

* Use factory for mock user

* Add needs_id DQAVoteButtons unit test

* Revert hook to js

* Check for observation before using it

* Undo code style changes

* Remove unused value

* Refactor faves to be derived from votes

* Refactor needs_id interaction into standalone fcts

* Code style
2024-03-22 21:57:28 +01:00

146 lines
4.5 KiB
JavaScript

import { useRoute } from "@react-navigation/native";
import { screen } from "@testing-library/react-native";
import DQAContainer from "components/ObsDetails/DQAContainer";
import { t } from "i18next";
import React from "react";
import factory from "tests/factory";
import faker from "tests/helpers/faker";
import { renderComponent } from "tests/helpers/render";
const mockObservation = factory( "LocalObservation", {
observed_on: "2023-12-14T21:07:41-09:30",
observationPhotos: [factory( "LocalObservationPhoto" )],
latitude: Number( faker.location.latitude( ) ),
longitude: Number( faker.location.longitude( ) ),
taxon: {
id: undefined,
rank_level: undefined
},
identifications: [],
// casual is the default, so using needs_id here ensures test
// is using our mock observation, not just showing the default screen
quality_grade: "needs_id",
votes: []
} );
const mockUserID = "some_user_id";
// Mock useCurrentUser hook
jest.mock( "sharedHooks/useCurrentUser", ( ) => ( {
__esModule: true,
default: jest.fn( ( ) => ( {
id: mockUserID
} ) )
} ) );
const mockMutate = jest.fn();
jest.mock( "sharedHooks/useAuthenticatedMutation", () => ( {
__esModule: true,
default: () => ( {
mutate: mockMutate
} )
} ) );
jest.mock( "sharedHooks/useLocalObservation", () => ( {
__esModule: true,
default: jest.fn( ( ) => mockObservation )
} ) );
useRoute.mockImplementation( ( ) => ( {
params: {
observationUUID: mockObservation.uuid
}
} ) );
describe( "Data Quality Assessment", ( ) => {
test( "renders correct quality grade status", async ( ) => {
renderComponent( <DQAContainer /> );
const qualityGrade = await screen.findByTestId(
`QualityGrade.${mockObservation.quality_grade}`
);
expect( qualityGrade ).toBeTruthy( );
} );
test( "renders correct quality grade status title", async ( ) => {
renderComponent( <DQAContainer /> );
const qualityGrade = await screen.findByText(
t( "Data-quality-assessment-title-needs-id" )
);
expect( qualityGrade ).toBeTruthy( );
} );
test( "renders correct quality grade status description", async ( ) => {
renderComponent( <DQAContainer /> );
const qualityGrade = await screen.findByText(
t( "Data-quality-assessment-description-needs-id" )
);
expect( qualityGrade ).toBeTruthy( );
} );
test( "renders correct metric titles", async ( ) => {
renderComponent( <DQAContainer /> );
const dateSpecified = await screen.findByText(
t( "Data-quality-assessment-date-specified" )
);
const locationSpecified = await screen.findByText(
t( "Data-quality-assessment-location-specified" )
);
const photosAndSounds = await screen.findByText(
t( "Data-quality-assessment-has-photos-or-sounds" )
);
const idSupportedTwoOrMore = await screen.findByText(
t( "Data-quality-assessment-id-supported-by-two-or-more" )
);
const communityTaxonSpeciesLevel = await screen.findByText(
t( "Data-quality-assessment-community-taxon-species-level-or-lower" )
);
expect( dateSpecified ).toBeTruthy( );
expect( locationSpecified ).toBeTruthy( );
expect( photosAndSounds ).toBeTruthy( );
expect( idSupportedTwoOrMore ).toBeTruthy( );
expect( communityTaxonSpeciesLevel ).toBeTruthy( );
} );
test( "renders correct metric vote titles", async ( ) => {
renderComponent( <DQAContainer /> );
const dateAccurate = await screen.findByText(
t( "Data-quality-assessment-date-is-accurate" )
);
const locationAccurate = await screen.findByText(
t( "Data-quality-assessment-location-is-accurate" )
);
const organismWild = await screen.findByText(
t( "Data-quality-assessment-organism-is-wild" )
);
const organismEvidence = await screen.findByText(
t( "Data-quality-assessment-evidence-of-organism" )
);
const recentEvidence = await screen.findByText(
t( "Data-quality-assessment-recent-evidence-of-organism" )
);
expect( dateAccurate ).toBeTruthy( );
expect( locationAccurate ).toBeTruthy( );
expect( organismWild ).toBeTruthy( );
expect( organismEvidence ).toBeTruthy( );
expect( recentEvidence ).toBeTruthy( );
} );
test( "renders about section", async ( ) => {
renderComponent( <DQAContainer /> );
const title = await screen.findByText(
t( "ABOUT-THE-DQA" )
);
const description = await screen.findByText(
t( "About-the-DQA-description" )
);
expect( title ).toBeTruthy( );
expect( description ).toBeTruthy( );
} );
} );