Skip to content

Commit

Permalink
fix app shell odd tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shlokamin committed Mar 5, 2024
1 parent 6008b86 commit bccfdee
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 24 deletions.
72 changes: 50 additions & 22 deletions app-shell-odd/src/__tests__/discovery.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// tests for the app-shell's discovery module
import { app } from 'electron'
import { vi, it, expect, describe, beforeEach } from 'vitest'
import Store from 'electron-store'
import { when } from 'vitest-when'
import noop from 'lodash/noop'
import { vi, it, expect, describe, beforeEach, afterEach } from 'vitest'

import * as DiscoveryClient from '@opentrons/discovery-client'
import {
Expand All @@ -14,9 +14,24 @@ import * as Cfg from '../config'

vi.mock('electron')
vi.mock('electron-store')
vi.mock('../usb')
vi.mock('@opentrons/discovery-client')
vi.mock('../config')
vi.mock('../system-info')
vi.mock('../log', () => {
return {
createLogger: () => {
return { debug: () => null }
},
}
})

let mockGet = vi.fn(property => {
return []
})
let mockOnDidChange = vi.fn()
let mockDelete = vi.fn()
let mockSet = vi.fn()
describe('app-shell/discovery', () => {
const dispatch = vi.fn()
const mockClient = {
Expand All @@ -36,17 +51,30 @@ describe('app-shell/discovery', () => {
}

beforeEach(() => {
mockGet = vi.fn(property => {
return []
})
mockDelete = vi.fn()
mockOnDidChange = vi.fn()
mockSet = vi.fn()
vi.mocked(Store).mockImplementation(() => {
return {
get: mockGet,
set: mockSet,
delete: mockDelete,
onDidAnyChange: mockOnDidChange,
} as any
})
vi.mocked(Cfg.getFullConfig).mockReturnValue(({
discovery: { disableCache: false, candidates: [] },
} as unknown) as Cfg.Config)

vi.mocked(Cfg.getOverrides).mockReturnValue({})
vi.mocked(DiscoveryClient.createDiscoveryClient).mockReturnValue(mockClient)
})

when(vi.mocked(Store).prototype.get).calledWith('robots', []).thenReturn([])
when(vi.mocked(Store).prototype.get)
.calledWith('services', null)
.thenReturn(null)
afterEach(() => {
vi.resetAllMocks()
})

it('registerDiscovery creates a DiscoveryClient', () => {
Expand Down Expand Up @@ -147,18 +175,18 @@ describe('app-shell/discovery', () => {
mockClient.getRobots.mockReturnValue([{ name: 'foo' }, { name: 'bar' }])
emitListChange()

expect(vi.mocked(Store).prototype.set).toHaveBeenLastCalledWith(
'robots',
[{ name: 'foo' }, { name: 'bar' }]
)
expect(vi.mocked(mockSet)).toHaveBeenLastCalledWith('robots', [
{ name: 'foo' },
{ name: 'bar' },
])
})

it('loads robots from cache on client initialization', () => {
const mockRobot = { name: 'foo' }

vi.mocked(Store).prototype.get.mockImplementation((key: string) => {
vi.mocked(mockGet).mockImplementation((key: string) => {
if (key === 'robots') return [mockRobot]
return null
return null as any
})

registerDiscovery(dispatch)
Expand Down Expand Up @@ -242,13 +270,13 @@ describe('app-shell/discovery', () => {
},
]

vi.mocked(Store).prototype.get.mockImplementation((key: string) => {
vi.mocked(mockGet).mockImplementation((key: string) => {
if (key === 'services') return services
return null
return null as any
})

registerDiscovery(dispatch)
expect(vi.mocked(Store).prototype.delete).toHaveBeenCalledWith('services')
expect(mockDelete).toHaveBeenCalledWith('services')
expect(mockClient.start).toHaveBeenCalledWith(
expect.objectContaining({
initialRobots: [
Expand Down Expand Up @@ -326,9 +354,9 @@ describe('app-shell/discovery', () => {
} as unknown) as Cfg.Config)

// discovery.json contains 1 entry
vi.mocked(Store).prototype.get.mockImplementation((key: string) => {
mockGet.mockImplementation((key: string) => {
if (key === 'robots') return [{ name: 'foo' }]
return null
return null as any
})

registerDiscovery(dispatch)
Expand All @@ -351,9 +379,9 @@ describe('app-shell/discovery', () => {
} as unknown) as Cfg.Config)

// discovery.json contains 1 entry
vi.mocked(Store).prototype.get.mockImplementation((key: string) => {
mockGet.mockImplementation((key: string) => {
if (key === 'robots') return [{ name: 'foo' }]
return null
return null as any
})

registerDiscovery(dispatch)
Expand All @@ -363,15 +391,15 @@ describe('app-shell/discovery', () => {
const disableCache = true
changeHandler(disableCache, false)

expect(vi.mocked(Store).prototype.set).toHaveBeenCalledWith('robots', [])
expect(mockSet).toHaveBeenCalledWith('robots', [])

// new services discovered
vi.mocked(Store).prototype.set.mockClear()
mockSet.mockClear()
mockClient.getRobots.mockReturnValue([{ name: 'foo' }, { name: 'bar' }])
emitListChange()

// but discovery.json should not update
expect(vi.mocked(Store).prototype.set).toHaveBeenCalledTimes(0)
expect(mockSet).toHaveBeenCalledTimes(0)
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import tempy from 'tempy'

import { cleanupReleaseFiles } from '../release-files'
vi.mock('electron-store')
vi.mock('../../log')

describe('system release files utilities', () => {
const tempDirs: string[] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { downloadAndCacheReleaseManifest } from '../release-manifest'

vi.mock('../../http')
vi.mock('../directories')
vi.mock('../../log')
vi.mock('electron-store')
const fetchJson = Http.fetchJson
const getManifestCacheDir = Dirs.getManifestCacheDir
Expand Down
3 changes: 2 additions & 1 deletion app-shell/src/protocol-storage/__tests__/file-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import { analyzeProtocolSource } from '../../protocol-analysis'

vi.mock('uuid/v4')
vi.mock('electron')
vi.mock('../../protocol-analysis')
vi.mock('electron-store')
vi.mock('../../protocol-analysis')
vi.mock('../../log')

const trashItem = Electron.shell.trashItem

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '../'

vi.mock('electron-store')
vi.mock('../../log')

describe('protocol storage directory utilities', () => {
let protocolsDir: string
Expand Down
1 change: 1 addition & 0 deletions app-shell/src/robot-update/__tests__/release-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { cleanupReleaseFiles } from '../release-files'

vi.mock('electron-updater')
vi.mock('electron-store')
vi.mock('../../log')

describe('robot update release files utilities', () => {
const tempDirs: string[] = []
Expand Down
13 changes: 12 additions & 1 deletion app-shell/src/system-info/__tests__/dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as Fixtures from '@opentrons/app/src/redux/system-info/__fixtures__'
import * as SystemInfo from '@opentrons/app/src/redux/system-info'
import { uiInitialized } from '@opentrons/app/src/redux/shell/actions'
import * as OS from '../../os'
import { createLogger } from '../../log'
import { createUsbDeviceMonitor, getWindowsDriverVersion } from '../usb-devices'
import {
getActiveInterfaces,
Expand All @@ -16,11 +17,21 @@ import type { Dispatch } from '../../types'
import type { UsbDeviceMonitor } from '../usb-devices'
import type { NetworkInterfaceMonitor } from '../network-interfaces'


vi.mock('../../os')
vi.mock('../usb-devices')
vi.mock('../network-interfaces')
vi.mock('electron-store')

vi.mock('../../log', async importOriginal => {
const actual = await importOriginal<typeof createLogger>()
return {
...actual,
createLogger: () => ({
debug: vi.fn(),
error: vi.fn(),
}),
}
})
const flush = (): Promise<void> =>
new Promise(resolve => setTimeout(resolve, 0))

Expand Down
13 changes: 13 additions & 0 deletions app-shell/src/system-info/__tests__/usb-devices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@ import { usb } from 'usb'
import { vi, it, expect, describe, afterEach } from 'vitest'

import * as Fixtures from '@opentrons/app/src/redux/system-info/__fixtures__'
import { createLogger } from '../../log'
import { createUsbDeviceMonitor, getWindowsDriverVersion } from '../usb-devices'
import { isWindows } from '../../os'


vi.mock('execa')
vi.mock('usb')
vi.mock('electron-store')
vi.mock('../../log', async importOriginal => {
const actual = await importOriginal<typeof createLogger>()
return {
...actual,
createLogger: () => ({
debug: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
}),
}
})

const mockFixtureDevice = {
...Fixtures.mockUsbDevice,
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default mergeConfig(
allowOnly: true,
exclude: [...configDefaults.exclude, '**/node_modules/**', '**/dist/**'],
setupFiles: ['./setup-vitest.js'],
poolOptions: { threads: { singleThread: true } },
},
resolve: {
alias: {
Expand Down

0 comments on commit bccfdee

Please sign in to comment.