migrated api species tests to jest

This commit is contained in:
Aaro Koivukangas
2024-09-12 13:36:26 +03:00
committed by Aaro Koivukangas
parent 92acd21920
commit 227d4fb0ec
4 changed files with 25 additions and 30 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
"scripts": {
"dev": "npx tsx watch --clear-screen=false src/index.ts",
"build": "tsc",
"test": "jest src/api-tests/locality",
"test": "jest src/api-tests/locality src/api-tests/species",
"test:with_reset": "cd .. && npm run anon:down && npm run start:anon -- -d && sleep 60 && cd backend && npm run test; cd .. && npm run anon:down",
"start": "node build/index.js",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
+7 -8
View File
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import assert from 'node:assert/strict'
import { before, describe, it } from 'node:test'
import { beforeEach, describe, it, expect } from '@jest/globals'
import { LocalityDetailsType, SpeciesDetailsType } from '../../../../frontend/src/backendTypes'
import { LogRow } from '../../services/write/writeOperations/types'
import { newSpeciesBasis } from './data'
@@ -9,7 +8,7 @@ import { login, send, testLogRows } from '../utils'
let createdSpecies: SpeciesDetailsType | null = null
describe('Creating new species works', () => {
before(async () => {
beforeEach(async () => {
await login()
})
@@ -19,7 +18,7 @@ describe('Creating new species works', () => {
})
const { id: createdId } = resultBody
assert(typeof createdId === 'number', `Invalid result returned on write: ${createdId}`)
expect(typeof createdId).toEqual('number') // `Invalid result returned on write: ${createdId}`
const { body } = await send<SpeciesDetailsType>(`species/${createdId}`, 'GET')
createdSpecies = body
@@ -27,17 +26,17 @@ describe('Creating new species works', () => {
it('Contains correct data', () => {
const { species_name, now_ls } = createdSpecies!
assert(species_name === newSpeciesBasis.species_name, 'Name is different')
expect(species_name).toEqual(newSpeciesBasis.species_name) // 'Name is different'
const locality = now_ls.find(ls => ls.now_loc.lid === 24750)
assert(!!locality, 'Locality in locality-species not found')
expect(!!locality).toEqual(true) // 'Locality in locality-species not found'
})
it('Locality-species change was updated also to locality', async () => {
const localityFound = createdSpecies!.now_ls.find(ls => ls.now_loc.loc_name.startsWith('Romany'))
assert(localityFound, 'Locality was not found in now_ls')
if (!localityFound) throw new Error('Locality was not found in now_ls')
const speciesResult = await send<LocalityDetailsType>(`locality/24750`, 'GET')
const update = speciesResult.body.now_lau.find(lau => lau.lid === 24750 && lau.lau_comment === 'species test')
assert(update, 'Update not found')
if(!update) throw new Error('Update not found')
const logRows = update.updates
const expectedLogRows: Partial<LogRow>[] = [
{
+6 -7
View File
@@ -1,16 +1,15 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import assert from 'node:assert/strict'
import { describe, it, before } from 'node:test'
import { beforeEach, describe, it, expect } from '@jest/globals'
import { login, send } from '../utils'
describe('Deleting a species works', async () => {
before(async () => {
describe('Deleting a species works', () => {
beforeEach(async () => {
await login()
})
await it('Deleting returns correct status and after that is not found on request', async () => {
it('Deleting returns correct status and after that is not found on request', async () => {
const deleteResult = await send<{ id: number }>('species/84357', 'DELETE')
assert(deleteResult.status === 200)
expect(deleteResult.status).toEqual(200)
const getResult = await send('species/84357', 'GET')
assert(getResult.status === 404, 'Species response status was not 404 after deletion')
expect(getResult.status).toEqual(404) // 'Species response status was not 404 after deletion'
})
})
+11 -14
View File
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import assert from 'node:assert/strict'
import { before, describe, it } from 'node:test'
import { beforeEach, describe, it, expect } from '@jest/globals'
import { EditMetaData, SpeciesDetailsType } from '../../../../frontend/src/backendTypes'
import { LogRow } from '../../services/write/writeOperations/types'
import { login, send, testLogRows } from '../utils'
@@ -9,41 +8,39 @@ import { editedSpecies } from './data'
let editedSpeciesResult: (SpeciesDetailsType & EditMetaData) | null = null
describe('Updating species works', () => {
before(async () => {
beforeEach(async () => {
await login()
})
it('Edits name, comment and locality-species correctly', async () => {
const writeResult = await send<{ id: number }>('species', 'PUT', { species: editedSpecies })
assert(writeResult.status === 200, 'Response status was OK')
assert(writeResult.body.id === editedSpecies.species_id, `Invalid result returned on write: ${writeResult.body.id}`)
expect(writeResult.status).toEqual(200) // 'Response status was OK'
expect(writeResult.body.id).toEqual(editedSpecies.species_id) // `Invalid result returned on write: ${writeResult.body.id}`
const { body, status } = await send<SpeciesDetailsType>(`species/${writeResult.body.id}`, 'GET')
assert(status === 200, 'Status on response to GET added species request was OK')
expect(status).toEqual(200) // 'Status on response to GET added species request was OK'
editedSpeciesResult = body
})
it('Name changed correctly', () => {
assert(editedSpeciesResult!.species_name === editedSpecies.species_name, 'Name was not changed correctly')
expect(editedSpeciesResult!.species_name).toEqual(editedSpecies.species_name) // 'Name was not changed correctly'
})
it('Added locality species is found', () => {
assert(
editedSpeciesResult!.now_ls.find(ls => ls.species_id === editedSpecies.species_id && ls.lid === 20920),
'Added locality species not found'
)
editedSpeciesResult!.now_ls.find(ls => ls.species_id === editedSpecies.species_id && ls.lid === 20920) //'Added locality species not found'
expect(!!editedSpeciesResult).toEqual(true)
})
it('Locality species include correct amount of entries', () => {
assert(editedSpeciesResult!.now_ls.length === 2, `Unexpected now_ls length: ${editedSpeciesResult!.now_ls.length}`)
expect(editedSpeciesResult!.now_ls.length).toEqual(2) // `Unexpected now_ls length: ${editedSpeciesResult!.now_ls.length}`)
})
it('Changes were logged correctly', () => {
const update = editedSpeciesResult!.now_sau
const lastUpdate = update[update.length - 1]
assert(lastUpdate.sau_comment === editedSpecies.comment, 'Comment wrong')
assert(lastUpdate.now_sr[lastUpdate.now_sr.length - 1].rid === editedSpecies.references![0].rid)
expect(lastUpdate.sau_comment).toEqual(editedSpecies.comment) // 'Comment wrong'
expect(lastUpdate.now_sr[lastUpdate.now_sr.length - 1].rid).toEqual(editedSpecies.references![0].rid)
const logRows = lastUpdate.updates