Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gromdimon committed Sep 13, 2023
1 parent 2cf691e commit 5923126
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
20 changes: 14 additions & 6 deletions frontend/src/api/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
isVariantMt,
isVariantMtHomopolymer,
search,
infoFromQuery
infoFromQuery,
copy
} from '../utils'

describe.concurrent('roundIt method', () => {
Expand Down Expand Up @@ -41,7 +42,7 @@ describe.concurrent('roundIt method', () => {
})
})

describe('separateIt method', () => {
describe.concurrent('separateIt method', () => {
it('should separate a positive value with default separator', () => {
const result = separateIt(123456789)
expect(result).toBe(' 123 456 789 ')
Expand All @@ -68,7 +69,7 @@ describe('separateIt method', () => {
})
})

describe('isVariantMt method', () => {
describe.concurrent('isVariantMt method', () => {
it('should return true if mitochondrial chromosome', () => {
const result_MT = isVariantMt({ chromosome: 'MT' })
const result_M = isVariantMt({ chromosome: 'M' })
Expand All @@ -86,7 +87,7 @@ describe('isVariantMt method', () => {
})
})

describe('isVariantMtHomopolymer method', () => {
describe.concurrent('isVariantMtHomopolymer method', () => {
it('should return true if mitochondrial homopolymer', () => {
const result = isVariantMtHomopolymer({ chromosome: 'MT', start: 70 })
expect(result).toBe(true)
Expand All @@ -108,7 +109,7 @@ describe('isVariantMtHomopolymer method', () => {
})
})

describe('search method', () => {
describe.concurrent('search method', () => {
it('should return "gene" route location for HGNC queries', () => {
const result = search('HGNC:1100', 'ghcr37')
expect(result).toEqual({
Expand Down Expand Up @@ -143,7 +144,7 @@ describe('search method', () => {
})
})

describe('infoFromQuery method', () => {
describe.concurrent('infoFromQuery method', () => {
it('should return info from query', () => {
const result = infoFromQuery('chr37:12345:A:G')
expect(result).toEqual({
Expand Down Expand Up @@ -182,3 +183,10 @@ describe('infoFromQuery method', () => {
})
})
})

describe.concurrent('copy method', () => {
it('should return a JSON object for given dict', () => {
const result = copy({ foo: 'bar' })
expect(result).toEqual({ foo: 'bar' })
})
})
86 changes: 86 additions & 0 deletions frontend/src/stores/__tests__/variantAcmgRating.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { beforeEach, describe, it, expect, vi } from 'vitest'
import createFetchMock from 'vitest-fetch-mock'

import { setActivePinia, createPinia } from 'pinia'

import { StoreState } from '../misc'
import { useVariantAcmgRatingStore } from '../variantAcmgRating'

const fetchMocker = createFetchMock(vi)

const smallVariantInfo = {
release: 'grch37',
chromosome: 'chr17',
start: '43044295',
end: '43044295',
reference: 'G',
alternative: 'A',
hgnc_id: 'HGNC:1100'
}

describe.concurrent('geneInfo Store', () => {
beforeEach(() => {
setActivePinia(createPinia())
fetchMocker.enableMocks()
fetchMocker.resetMocks()
})

it('should have initial state', () => {
const store = useVariantAcmgRatingStore()

expect(store.storeState).toBe(StoreState.Initial)
expect(store.acmgRating).toBe(null)
expect(store.smallVariant).toBe(null)
})

it('should clear state', () => {
const store = useVariantAcmgRatingStore()
store.storeState = StoreState.Active
store.acmgRating = JSON.parse(JSON.stringify({ acmg: 'rating' }))
store.smallVariant = JSON.parse(JSON.stringify({ gene: 'info' }))

store.clearData()

expect(store.storeState).toBe(StoreState.Initial)
expect(store.acmgRating).toBe(null)
expect(store.smallVariant).toBe(null)
})

it('should correctly retrieve data', async () => {
const store = useVariantAcmgRatingStore()
fetchMocker.mockResponseOnce(JSON.stringify({ acmg: 'rating' }))

await store.retrieveAcmgRating(smallVariantInfo)

expect(store.storeState).toBe(StoreState.Active)
expect(store.acmgRating).toStrictEqual(JSON.parse(JSON.stringify({ acmg: 'rating' })))
expect(store.smallVariant).toStrictEqual(JSON.parse(JSON.stringify(smallVariantInfo)))
})

it('should fail to load data with invalid request', async () => {
// Disable error logging
vi.spyOn(console, 'error').mockImplementation(() => {})
const store = useVariantAcmgRatingStore()
fetchMocker.mockResponseOnce(JSON.stringify({ foo: 'bar' }), { status: 400 })

await store.retrieveAcmgRating(smallVariantInfo)

expect(store.storeState).toBe(StoreState.Error)
expect(store.acmgRating).toBe(null)
expect(store.smallVariant).toBe(null)
})

it('should not load data if small variant is the same', async () => {
const store = useVariantAcmgRatingStore()
fetchMocker.mockResponse(JSON.stringify({ acmg: 'rating' }))
await store.retrieveAcmgRating(smallVariantInfo)

expect(store.storeState).toBe(StoreState.Active)
expect(store.acmgRating).toStrictEqual(JSON.parse(JSON.stringify({ acmg: 'rating' })))
expect(store.smallVariant).toStrictEqual(JSON.parse(JSON.stringify(smallVariantInfo)))

await store.retrieveAcmgRating(store.smallVariant)

expect(fetchMocker.mock.calls.length).toBe(1)
})
})
4 changes: 3 additions & 1 deletion frontend/src/stores/variantAcmgRating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export const useVariantAcmgRatingStore = defineStore('variantAcmgRating', () =>
// Load data via API
storeState.value = StoreState.Loading
try {
console.log(smallVar)
const release = smallVar.release === 'grch37' ? 'hg19' : 'hg38'
const chromosome = smallVar.chromosome.replace('chr', '')
const pos = smallVar.start
Expand All @@ -53,6 +52,9 @@ export const useVariantAcmgRatingStore = defineStore('variantAcmgRating', () =>
`&position=${pos}&reference=${ref}&alternative=${alt}`,
{ method: 'GET' }
)
if (!response.ok) {
throw new Error('There was an error loading the ACMG data.')
}
acmgRating.value = await response.json()
smallVariant.value = smallVar
storeState.value = StoreState.Active
Expand Down

0 comments on commit 5923126

Please sign in to comment.