Skip to content

Commit

Permalink
better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe committed Oct 4, 2023
1 parent 851b5fd commit 1509ddf
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 25 deletions.
2 changes: 1 addition & 1 deletion frontend/src/api/__tests__/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AuthClient } from '@/api/auth'

const fetchMocker = createFetchMock(vi)

describe.concurrent('Auth Client', () => {
describe.concurrent('AuthClient', () => {
beforeEach(() => {
fetchMocker.enableMocks()
fetchMocker.resetMocks()
Expand Down
31 changes: 31 additions & 0 deletions frontend/src/api/__tests__/dotty.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import createFetchMock from 'vitest-fetch-mock'

import { DottyClient, type DottyResponse } from '@/api/dotty'

const fetchMocker = createFetchMock(vi)

describe.concurrent('DottyClient', () => {
beforeEach(() => {
fetchMocker.enableMocks()
fetchMocker.resetMocks()
})

it('should resolve to SPDI successfully', async () => {
const mockData: DottyResponse = {
spdi: {
assembly: 'GRCh38',
contig: '13',
pos: 32319283,
reference_deleted: 'C',
alternate_inserted: 'A'
}
}
fetchMocker.mockResponseOnce(JSON.stringify(mockData), { status: 200 })

const client = new DottyClient()
const result = await client.toSpdi('NM_000059.3:c.274G>A')

expect(result).toEqual(mockData)
})
})
10 changes: 9 additions & 1 deletion frontend/src/components/__tests__/HeaderDetailPage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { nextTick } from 'vue'

import { DottyClient } from '@/api/dotty'
import SearchBar from '@/components/SearchBar.vue'
import { setupMountedComponents } from '@/lib/test-utils'
import { useGeneInfoStore } from '@/stores/geneInfo'
Expand All @@ -21,6 +22,10 @@ const geneData = {
}

describe.concurrent('HeaderDetailPage', async () => {
afterEach(() => {
vi.restoreAllMocks()
})

it('renders the gene symbol and nav links', () => {
const { wrapper } = setupMountedComponents(
{ component: HeaderDetailPage, template: true },
Expand Down Expand Up @@ -60,6 +65,9 @@ describe.concurrent('HeaderDetailPage', async () => {
})

it('correctly emits search', async () => {
// we make `DottyClient.toSpdi` return null / fail
vi.spyOn(DottyClient.prototype, 'toSpdi').mockResolvedValue(null)

const { wrapper, router } = setupMountedComponents(
{ component: HeaderDetailPage, template: true },
{
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/components/__tests__/SearchBar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { describe, expect, it } from 'vitest'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { nextTick } from 'vue'

import { DottyClient } from '@/api/dotty'
import { setupMountedComponents } from '@/lib/test-utils'

import SearchBar from '../SearchBar.vue'

describe.concurrent('SearchBar.vue', () => {
afterEach(() => {
vi.restoreAllMocks()
})

it('renders the search bar with the correct default props', () => {
const { wrapper } = setupMountedComponents(
{ component: SearchBar, template: false },
Expand Down Expand Up @@ -61,6 +66,9 @@ describe.concurrent('SearchBar.vue', () => {
})

it('correctly emits search', async () => {
// we make `DottyClient.toSpdi` return null / fail
vi.spyOn(DottyClient.prototype, 'toSpdi').mockResolvedValue(null)

const { wrapper } = setupMountedComponents(
{ component: SearchBar, template: false },
{
Expand Down
46 changes: 28 additions & 18 deletions frontend/src/lib/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { describe, expect, it } from 'vitest'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { DottyClient } from '@/api/dotty'

import {
copy,
Expand Down Expand Up @@ -109,7 +111,15 @@ describe.concurrent('isVariantMtHomopolymer method', () => {
})
})

describe.concurrent('search method', () => {
describe.concurrent('search method', async () => {
beforeEach(() => {
// we make `DottyClient.toSpdi` return null / fail every time
vi.spyOn(DottyClient.prototype, 'toSpdi').mockResolvedValue(null)
})
afterEach(() => {
vi.restoreAllMocks()
})

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

it('should return null if no entry', async () => {
const result = await search('', 'foo37')
expect(result).toBe(null)
})

it('should remove whitespace', async () => {
const result = await search(' HGNC:1100 ', 'ghcr37')
expect(result).toEqual({
name: 'gene',
params: {
searchTerm: 'HGNC:1100',
genomeRelease: 'ghcr37'
}
})
})
})

describe.concurrent('infoFromQuery method', () => {
Expand All @@ -166,22 +192,6 @@ describe.concurrent('infoFromQuery method', () => {
hgnc_id: undefined
})
})

it('should return null if no entry', () => {
const result = search('', 'foo37')
expect(result).toBe(null)
})

it('should remove whitespace', () => {
const result = search(' HGNC:1100 ', 'ghcr37')
expect(result).toEqual({
name: 'gene',
params: {
searchTerm: 'HGNC:1100',
genomeRelease: 'ghcr37'
}
})
})
})

describe.concurrent('copy method', () => {
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const search = async (searchTerm: string, genomeRelease: string) => {
// Remove leading/trailing whitespace.
searchTerm = searchTerm.trim()
if (!searchTerm) {
return // no query ;-)
return null // no query ;-)
}

// First, attempt to resolve using dotty.
Expand All @@ -105,7 +105,6 @@ export const search = async (searchTerm: string, genomeRelease: string) => {
if (!searchTerm.startsWith('chr')) {
searchTerm = `chr${searchTerm}`
}
console.log(searchTerm)
genomeRelease = spdi.assembly.toLowerCase()
}

Expand Down
11 changes: 9 additions & 2 deletions frontend/src/views/__tests__/HomeView.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { nextTick } from 'vue'

import { DottyClient } from '@/api/dotty'
import FooterDefault from '@/components/FooterDefault.vue'
import HeaderDefault from '@/components/HeaderDefault.vue'
import SearchBar from '@/components/SearchBar.vue'
Expand All @@ -23,6 +24,10 @@ const geneData = {
}

describe.concurrent('HomeView with mocked router', async () => {
afterEach(() => {
vi.restoreAllMocks()
})

it('renders the header and the footer', () => {
const { wrapper } = setupMountedComponents(
{ component: HomeView, template: true },
Expand Down Expand Up @@ -97,7 +102,7 @@ describe.concurrent('HomeView with mocked router', async () => {
const subtitle = wrapper.find('h2')
const exampleTerms = wrapper.findAll('.example')
expect(subtitle.exists()).toBe(true)
expect(exampleTerms.length).toBe(9)
expect(exampleTerms.length).toBe(8)
})

it('uses example by click', async () => {
Expand Down Expand Up @@ -127,6 +132,8 @@ describe.concurrent('HomeView with mocked router', async () => {
})

it('correctly uses the router', async () => {
vi.spyOn(DottyClient.prototype, 'toSpdi').mockResolvedValue(null)

const { wrapper, router } = setupMountedComponents(
{ component: HomeView, template: true },
{
Expand Down

0 comments on commit 1509ddf

Please sign in to comment.