add some tests for locality -> species tab and fix issue with null subclass, suborder and subfamily when selecting species

This commit is contained in:
juhanikat
2025-09-08 10:57:46 +03:00
parent 8cc818ea1d
commit dcca0aefcd
3 changed files with 80 additions and 5 deletions
+67
View File
@@ -1,3 +1,70 @@
describe('Adding species in Locality -> Species tab', () => {
beforeEach('Login as admin', () => {
cy.login('testSu')
})
it('does not work if the species has already been added', () => {
cy.visit(`/locality/20920?tab=2`)
cy.contains('Lantian-Shuijiazui')
cy.get('[id=edit-button]').click()
cy.contains('Select Species').click()
cy.get('[data-cy=add-button-21052]').first().click()
cy.get('[data-cy=add-button-21426]').first().click()
cy.contains('Close').click()
cy.contains('Rodentia')
cy.contains('Eulipotyphla')
// adding species that already exists in the database
cy.contains('Add new Species').click()
cy.get('[name=order_name]').type('Rodentia')
cy.get('[name=family_name]').type('Gliridae')
cy.get('[name=genus_name]').type('Simplomys')
cy.get('[name=species_name]').type('Simplicidens')
cy.get('[name=unique_identifier]').type('-')
cy.contains('Save').click()
cy.contains('The taxon already exists in the database.')
// add new, unique species
cy.contains('Add new Species').click()
cy.get('[name=order_name]').clear()
cy.get('[name=order_name]').type('new order')
cy.get('[name=family_name]').clear()
cy.get('[name=family_name]').type('new family')
cy.get('[name=genus_name]').clear()
cy.get('[name=genus_name]').type('new genus')
cy.get('[name=species_name]').clear()
cy.get('[name=species_name]').type('new species')
cy.get('[name=unique_identifier]').clear()
cy.get('[name=unique_identifier]').type('-')
cy.contains('Save').click()
cy.get('[id=write-button]').click()
cy.get('[id=write-button]').should('be.disabled')
cy.contains('button', 'Add existing reference').click()
cy.get('button[data-cy^="add-button"]').first().click()
cy.contains('button', 'Close').click()
cy.get('[id=write-button]').should('not.be.disabled')
cy.get('[id=write-button]').click()
cy.contains('Edited item successfully.')
cy.visit(`/locality/20920?tab=2`)
cy.contains('Lantian-Shuijiazui')
cy.get('[id=edit-button]').click()
cy.contains('Rodentia')
cy.contains('Eulipotyphla')
cy.contains('New order')
// adding species that has been added earlier through "add new species" button
cy.contains('Add new Species').click()
cy.get('[name=order_name]').type('anything')
cy.get('[name=family_name]').type('anything')
cy.get('[name=genus_name]').type('new genus')
cy.get('[name=species_name]').type('new species')
cy.get('[name=unique_identifier]').type('-')
cy.contains('Save').click()
cy.contains('The taxon already exists in the database.')
})
})
describe('Creating a new locality', () => {
beforeEach('Login as admin', () => {
cy.login('testSu')
@@ -8,7 +8,7 @@ import { useGetAllSpeciesQuery } from '@/redux/speciesReducer'
import { Box, CircularProgress } from '@mui/material'
import { skipToken } from '@reduxjs/toolkit/query'
import { MRT_ColumnDef } from 'material-react-table'
import { checkTaxonomy } from '@/util/taxonomyFunctions'
import { checkTaxonomy, convertTaxonomyFields } from '@/util/taxonomyFunctions'
import { useNotify } from '@/hooks/notification'
export const SpeciesTab = () => {
@@ -105,9 +105,11 @@ export const SpeciesTab = () => {
buttonText="Add new Species"
formFields={formFields}
editAction={(newSpecies: Species) => {
const convertedSpecies = convertTaxonomyFields(newSpecies)
const allSpecies = editData.now_ls.map(ls => ls.com_species) as unknown as Editable<Species>[]
const filteredSpecies = allSpecies.filter(species => species.rowState === 'new')
const taxonomyErrors = checkTaxonomy(newSpecies, speciesData!.concat(filteredSpecies), [])
const taxonomyErrors = checkTaxonomy(convertedSpecies, speciesData!.concat(filteredSpecies), [])
if (taxonomyErrors.size > 0) {
const errorMessage = [...taxonomyErrors].reduce((acc, currentError) => acc + `\n${currentError}`)
notify(errorMessage, 'error', null)
@@ -120,7 +122,7 @@ export const SpeciesTab = () => {
{
lid: editData.lid,
species_id: newSpecies.species_id,
com_species: { ...(newSpecies as unknown as SpeciesDetailsType) },
com_species: { ...(convertedSpecies as unknown as SpeciesDetailsType) },
rowState: 'new',
},
],
@@ -142,7 +144,14 @@ export const SpeciesTab = () => {
{
lid: editData.lid,
species_id: newSpecies.species_id,
com_species: { ...(newSpecies as unknown as SpeciesDetailsType) },
com_species: {
...(newSpecies as unknown as SpeciesDetailsType),
// changes subclass, suborder, and subfamily from null to empty string to make validators work
// this should be removed if validators are improved in the future
subclass_or_superorder_name: newSpecies.subclass_or_superorder_name ?? '',
suborder_or_superfamily_name: newSpecies.suborder_or_superfamily_name ?? '',
subfamily_name: newSpecies.subfamily_name ?? '',
},
rowState: 'new',
},
],
-1
View File
@@ -159,7 +159,6 @@ export const checkTaxonomy = (
let speciesWithSameSubfamily = undefined
for (const existingSpecies of existingSpeciesArray) {
if (existingSpecies.unique_identifier === newSpecies.unique_identifier) console.log('should throw error')
if (isDuplicateTaxon(newSpecies, existingSpecies)) {
errors.add('The taxon already exists in the database.')
return errors