From 6008b8644556cc87b1ae817f82250df2236bc398 Mon Sep 17 00:00:00 2001 From: Shlok Amin Date: Tue, 5 Mar 2024 17:33:17 -0500 Subject: [PATCH] fix app shell discovery test --- __mocks__/electron-store.js | 33 ++++++++---- app-shell/src/__tests__/discovery.test.ts | 66 +++++++++++++++-------- vitest.config.ts | 3 ++ 3 files changed, 69 insertions(+), 33 deletions(-) diff --git a/__mocks__/electron-store.js b/__mocks__/electron-store.js index 7155319b831..49444bba1f5 100644 --- a/__mocks__/electron-store.js +++ b/__mocks__/electron-store.js @@ -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() +// }) diff --git a/app-shell/src/__tests__/discovery.test.ts b/app-shell/src/__tests__/discovery.test.ts index 2b65d7cb285..134167a6f3e 100644 --- a/app-shell/src/__tests__/discovery.test.ts +++ b/app-shell/src/__tests__/discovery.test.ts @@ -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 = { @@ -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) @@ -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(() => { @@ -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) @@ -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: [ @@ -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) @@ -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) @@ -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) }) }) diff --git a/vitest.config.ts b/vitest.config.ts index b3881ac4c0e..4caf89b4fec 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -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' + ), }, }, })