From 59231269aa131e9f63b878ae4e9dc3ae60323506 Mon Sep 17 00:00:00 2001 From: gromdimon Date: Wed, 13 Sep 2023 16:50:41 +0200 Subject: [PATCH] tests --- frontend/src/api/__tests__/utils.spec.ts | 20 +++-- .../__tests__/variantAcmgRating.spec.ts | 86 +++++++++++++++++++ frontend/src/stores/variantAcmgRating.ts | 4 +- 3 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 frontend/src/stores/__tests__/variantAcmgRating.spec.ts diff --git a/frontend/src/api/__tests__/utils.spec.ts b/frontend/src/api/__tests__/utils.spec.ts index 75d7d6bc..3a1c3ff0 100644 --- a/frontend/src/api/__tests__/utils.spec.ts +++ b/frontend/src/api/__tests__/utils.spec.ts @@ -6,7 +6,8 @@ import { isVariantMt, isVariantMtHomopolymer, search, - infoFromQuery + infoFromQuery, + copy } from '../utils' describe.concurrent('roundIt method', () => { @@ -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 ') @@ -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' }) @@ -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) @@ -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({ @@ -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({ @@ -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' }) + }) +}) diff --git a/frontend/src/stores/__tests__/variantAcmgRating.spec.ts b/frontend/src/stores/__tests__/variantAcmgRating.spec.ts new file mode 100644 index 00000000..f421300f --- /dev/null +++ b/frontend/src/stores/__tests__/variantAcmgRating.spec.ts @@ -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) + }) +}) diff --git a/frontend/src/stores/variantAcmgRating.ts b/frontend/src/stores/variantAcmgRating.ts index f8090bee..0dcdf2e4 100644 --- a/frontend/src/stores/variantAcmgRating.ts +++ b/frontend/src/stores/variantAcmgRating.ts @@ -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 @@ -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