Skip to content

Commit

Permalink
fix a few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jerader committed Mar 5, 2024
1 parent 87aba6e commit fcb3bc2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// TODO(mc, 2020-06-11): test all release-files functions
import path from 'path'
import { describe, afterAll, it } from 'vitest'
import { promises as fs } from 'fs'
import fse from 'fs-extra'
import tempy from 'tempy'
Expand Down
38 changes: 15 additions & 23 deletions app-shell-odd/src/system-update/__tests__/release-manifest.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
import { when, resetAllWhenMocks } from 'jest-when'
import { describe, it, vi, beforeEach, afterEach } from 'vitest'
import fse from 'fs-extra'
import * as Http from '../../http'
import * as Dirs from '../directories'
import { downloadAndCacheReleaseManifest } from '../release-manifest'

jest.mock('fs-extra')
jest.mock('../../http')
jest.mock('../directories')
vi.mock('fs-extra')
vi.mock('../../http')
vi.mock('../directories')

const fetchJson = Http.fetchJson
const outputJson = fse.outputJson
const readJson = fse.readJson
const getManifestCacheDir = Dirs.getManifestCacheDir

const fetchJson = Http.fetchJson as jest.MockedFunction<typeof Http.fetchJson>
const outputJson = fse.outputJson as jest.MockedFunction<typeof fse.outputJson>
const readJson = fse.readJson as jest.MockedFunction<typeof fse.readJson>
const getManifestCacheDir = Dirs.getManifestCacheDir as jest.MockedFunction<
typeof Dirs.getManifestCacheDir
>
const MOCK_DIR = 'mock_dir'
const MANIFEST_URL = 'http://example.com/releases.json'
const MOCK_MANIFEST = {}

describe('release manifest utilities', () => {
beforeEach(() => {
getManifestCacheDir.mockReturnValue(MOCK_DIR)
when(fetchJson).calledWith(MANIFEST_URL).mockResolvedValue(MOCK_MANIFEST)
when(outputJson)
// @ts-expect-error outputJson takes additional optional arguments which is tweaking jest-when
.calledWith(MOCK_DIR, MOCK_MANIFEST)
// @ts-expect-error outputJson takes additional optional arguments which is tweaking jest-when
.mockResolvedValue()
when(readJson)
// @ts-expect-error readJson takes additional optional arguments which is tweaking jest-when
.calledWith(MOCK_DIR)
vi.mocked(getManifestCacheDir).mockReturnValue(MOCK_DIR)
vi.mocked(fetchJson).mockResolvedValue(MOCK_MANIFEST)
vi.mocked(outputJson).mockResolvedValue()
vi.mocked(readJson)
// @ts-expect-error readJson takes additional optional arguments which is tweaking jest-when
.mockResolvedValue(MOCK_MANIFEST)
})

afterEach(() => {
resetAllWhenMocks()
jest.resetAllMocks()
vi.resetAllMocks()
})

it('should download and save the manifest from a url', () => {
Expand All @@ -47,7 +39,7 @@ describe('release manifest utilities', () => {
})

it('should pull the manifest from the file if the manifest download fails', () => {
when(fetchJson).calledWith(MANIFEST_URL).mockRejectedValue('oh no!')
vi.mocked(fetchJson).mockRejectedValue('oh no!')
return downloadAndCacheReleaseManifest(MANIFEST_URL).then(manifest =>
expect(manifest).toBe(MOCK_MANIFEST)
)
Expand Down
42 changes: 17 additions & 25 deletions app-shell/src/protocol-storage/__tests__/file-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import path from 'path'
import fs from 'fs-extra'
import tempy from 'tempy'
import Electron from 'electron'
import { vi, describe, beforeEach, it, afterAll } from 'vitest'
import uuid from 'uuid/v4'
import { when } from 'jest-when'

import {
readDirectoriesWithinDirectory,
Expand All @@ -19,19 +19,13 @@ import {
import { getConfig } from '../../config'
import { analyzeProtocolSource } from '../../protocol-analysis'

jest.mock('uuid/v4')
jest.mock('electron')
jest.mock('../../config')
jest.mock('../../protocol-analysis')

const trashItem = Electron.shell.trashItem as jest.MockedFunction<
typeof Electron.shell.trashItem
>
const mockUuid = uuid as jest.MockedFunction<typeof uuid>
const mockGetConfig = getConfig as jest.MockedFunction<typeof getConfig>
const mockRunFileWithPython = analyzeProtocolSource as jest.MockedFunction<
typeof analyzeProtocolSource
>
vi.mock('uuid/v4')
vi.mock('electron')
vi.mock('../../config')
vi.mock('../../protocol-analysis')

const trashItem = Electron.shell.trashItem

describe('protocol storage directory utilities', () => {
let protocolsDir: string
Expand All @@ -43,14 +37,14 @@ describe('protocol storage directory utilities', () => {
}
beforeEach(() => {
protocolsDir = makeEmptyDir()
mockGetConfig.mockReturnValue({
vi.mocked(getConfig).mockReturnValue({
python: { pathToPythonOverride: null },
} as any)
mockRunFileWithPython.mockReturnValue(Promise.resolve())
vi.mocked(analyzeProtocolSource).mockReturnValue(Promise.resolve())
})

afterAll(() => {
jest.resetAllMocks()
vi.resetAllMocks()
return Promise.all(tempDirs.map(d => fs.remove(d)))
})

Expand Down Expand Up @@ -185,13 +179,11 @@ describe('protocol storage directory utilities', () => {
describe('addProtocolFile', () => {
it('writes a protocol file to a new directory', () => {
let count = 0
when(mockUuid)
.calledWith()
.mockImplementation(() => {
const nextId = `${count}abc123`
count = count + 1
return nextId
})
vi.mocked(uuid).mockImplementation(() => {
const nextId = `${count}abc123`
count = count + 1
return nextId
})
const sourceDir = makeEmptyDir()
const destDir = makeEmptyDir()
const sourceName = path.join(sourceDir, 'source.py')
Expand Down Expand Up @@ -223,7 +215,7 @@ describe('protocol storage directory utilities', () => {
const protocolId = 'def456'
const setup = fs.mkdir(path.join(protocolsDir, protocolId))

trashItem.mockResolvedValue()
vi.mocked(trashItem).mockResolvedValue()

return setup
.then(() => removeProtocolByKey('def456', protocolsDir))
Expand All @@ -239,7 +231,7 @@ describe('protocol storage directory utilities', () => {
const protocolId = 'def456'
const setup = fs.mkdir(path.join(protocolsDir, protocolId))

trashItem.mockRejectedValue(Error('something went wrong'))
vi.mocked(trashItem).mockRejectedValue(Error('something went wrong'))

return setup
.then(() => removeProtocolByKey('def456', protocolsDir))
Expand Down
16 changes: 3 additions & 13 deletions app/src/pages/NameRobot/__tests__/NameRobot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ describe('NameRobot', () => {
vi.mocked(useIsUnboxingFlowOngoing).mockReturnValue(true)
})

afterEach(() => {
vi.resetAllMocks()
})

it('should render text, button and keyboard', () => {
render()
screen.getByText('Name your robot')
Expand Down Expand Up @@ -109,9 +105,10 @@ describe('NameRobot', () => {
fireEvent.click(screen.getByRole('button', { name: 'c' }))
fireEvent.click(screen.getByRole('button', { name: 't' }))
expect(input).toHaveValue('connect')

fireEvent.click(screen.getByRole('button', { name: 'Confirm' }))
await waitFor(() =>
screen.findByText(
screen.queryByText(
'Oops! Name is already in use. Choose a different name.'
)
)
Expand All @@ -128,7 +125,7 @@ describe('NameRobot', () => {
expect(input).toHaveValue('reach')
fireEvent.click(screen.getByRole('button', { name: 'Confirm' }))
await waitFor(() =>
screen.findByText(
screen.queryByText(
'Oops! Name is already in use. Choose a different name.'
)
)
Expand All @@ -146,11 +143,4 @@ describe('NameRobot', () => {
screen.getByText('Enter up to 17 characters (letters and numbers only)')
screen.getByText('Confirm')
})

it('should call a mock function when tapping back button', () => {
vi.mocked(useIsUnboxingFlowOngoing).mockReturnValue(false)
render()
fireEvent.click(screen.getByTestId('name_back_button'))
expect(mockPush).toHaveBeenCalledWith('/robot-settings')
})
})

0 comments on commit fcb3bc2

Please sign in to comment.