Skip to content

Commit

Permalink
fix app shell discovery test
Browse files Browse the repository at this point in the history
  • Loading branch information
shlokamin committed Mar 5, 2024
1 parent dc19d3a commit 6008b86
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 33 deletions.
33 changes: 22 additions & 11 deletions __mocks__/electron-store.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
// mock electron-store
'use strict'
import { DEFAULTS_V12 } from '../app-shell-odd/src/config/migrate'
import { vi } from 'vitest'
import { DEFAULTS_V12, migrate } from '../app-shell-odd/src/config/migrate'

const Store = function () {
// this.get = () => ({
// DEFAULTS_V12,
// })
this.store = () => ({
DEFAULTS_V12,
// will by default mock the config dir. if you need other behaavior you can
// override this mock (see app-shell/src/__tests__/discovery.test.ts for an example)
const Store = vi.fn(function () {
this.store = vi.fn(() => {
return {}
})
this.get = property => {
return DEFAULTS_V12[property]
}
}
this.get = vi.fn(property => {
return {}
})
this.onDidChange = vi.fn()
})

// eslint-disable-next-line import/no-default-export
export default Store

// const Store = vi.fn(function () {
// this.store = vi.fn(() => migrate(DEFAULTS_V12))
// this.get = vi.fn(property => {
// return this.store()[property]
// })
// this.onDidChange = vi.fn()
// })
66 changes: 44 additions & 22 deletions app-shell/src/__tests__/discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@ import * as SysInfo from '../system-info'
import { getSerialPortHttpAgent } from '../usb'

vi.mock('electron')
// vi.mock('electron-store')
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 @@ -41,6 +54,20 @@ 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)
Expand All @@ -51,11 +78,6 @@ describe('app-shell/discovery', () => {
})
vi.mocked(DiscoveryClient.createDiscoveryClient).mockReturnValue(mockClient)
vi.mocked(getSerialPortHttpAgent).mockReturnValue({} as any)

when(vi.mocked(Store).prototype.get).calledWith('robots', []).thenReturn([])
when(vi.mocked(Store).prototype.get)
.calledWith('services', null)
.thenReturn(null)
})

afterEach(() => {
Expand Down Expand Up @@ -160,18 +182,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 @@ -255,13 +277,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 @@ -339,9 +361,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 @@ -364,9 +386,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 @@ -376,15 +398,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
3 changes: 3 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export default mergeConfig(
'@opentrons/discovery-client': path.resolve(
'./discovery-client/src/index.ts'
),
'@opentrons/usb-bridge/node-client': path.resolve(
'./usb-bridge/node-client/src/index.ts'
),
},
},
})
Expand Down

0 comments on commit 6008b86

Please sign in to comment.