Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(app): Reconfigure app-shell remote access #3683

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions __mocks__/electron.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
// mock electron module
'use strict'

const path = require('path')

jest.mock('electron-updater')

jest.mock(
'../app-shell/src/log',
() => require('../app/src/__mocks__/logger').default
)

const __mockRemotes = {}

const __clearMock = () => {
Object.keys(__mockRemotes).forEach(remoteName => {
const remote = __mockRemotes[remoteName]

Object.keys(remote).forEach(property => {
const value = remote[property]
value && value.mockClear && value.mockClear()
})
})
}

module.exports = {
__mockRemotes,
__clearMock,
// return jest mocked versions of remote modules
remote: {
require: jest.fn(name => {
if (__mockRemotes[name]) return __mockRemotes[name]

const remote = jest.genMockFromModule(
path.join(__dirname, '../app-shell/src', name)
)

__mockRemotes[name] = remote
return remote
}),
},

app: {
getPath: () => '__mock-app-path__',
once: jest.fn(),
Expand Down
16 changes: 0 additions & 16 deletions __mocks__/setup-mock-globals.js

This file was deleted.

2 changes: 2 additions & 0 deletions app-shell/src/__mocks__/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// mock logger
module.exports = require('../../../app/src/__mocks__/logger').default
2 changes: 1 addition & 1 deletion app-shell/src/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// for security reasons
import { ipcRenderer, remote } from 'electron'

global.APP_SHELL = {
global.APP_SHELL_REMOTE = {
ipcRenderer,
apiUpdate: remote.require('./api-update'),
config: remote.require('./config'),
Expand Down
2 changes: 1 addition & 1 deletion app/src/discovery/__tests__/selectors.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// discovery selectors tests
import * as discovery from '..'
import * as discovery from '../selectors'

const makeFullyUp = (
name,
Expand Down
6 changes: 4 additions & 2 deletions app/src/http-api-client/__tests__/robot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ describe('robot/*', () => {
let store

beforeEach(() => {
client.__clearMock()

robot = { name: NAME, ip: '1.2.3.4', port: '1234' }
state = { api: { robot: {} } }
store = mockStore(state)
})

afterEach(() => {
client.__clearMock()
})

describe('moveRobotTo action creator', () => {
const path = 'robot/move'
const request = { position: 'change_pipette', mount: 'left' }
Expand Down
8 changes: 4 additions & 4 deletions app/src/http-api-client/__tests__/server.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// server api tests
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import electron from 'electron'
import { setter } from '@thi.ng/paths'

import client from '../client'
Expand All @@ -17,7 +16,6 @@ import {
reducer,
} from '..'

jest.mock('electron')
jest.mock('../client')

const REQUESTS_TO_TEST = [
Expand All @@ -34,8 +32,6 @@ describe('server API client', () => {
let robot

beforeEach(() => {
client.__clearMock()
electron.__clearMock()
robot = {
name: 'opentrons',
ip: '1.2.3.4',
Expand All @@ -48,6 +44,10 @@ describe('server API client', () => {
}
})

afterEach(() => {
jest.clearAllMocks()
})

describe('selectors', () => {
const setCurrent = setter(`health.api_version`)
let state
Expand Down
6 changes: 3 additions & 3 deletions app/src/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
// logger
const { ipcRenderer } = global.APP_SHELL
import remote from './shell/remote'

// TODO(mc, 2018-05-17): put this type somewhere common to app and app-shell
export type LogLevel =
Expand Down Expand Up @@ -54,6 +54,6 @@ function log(level: LogLevel, message: string, label: string, meta?: {}) {
console.dir(meta)
}

// send to main process for logfile collection
ipcRenderer.send('log', { level, message, label, ...meta })
// send to main process for log file collection
remote.ipcRenderer.send('log', { level, message, label, ...meta })
}
28 changes: 28 additions & 0 deletions app/src/shell/__mocks__/remote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// mock remote object
// keep in sync with app-shell/src/preload.js
'use strict'

module.exports = {
ipcRenderer: {
on: jest.fn(),
send: jest.fn(),
},
apiUpdate: {
getUpdateInfo: jest.fn(),
getUpdateFileContents: jest.fn(),
},
config: {
getConfig: jest.fn(),
},
discovery: {
getRobots: jest.fn(),
},
update: {
CURRENT_VERSION: '0.0.0',
CURRENT_RELEASE_NOTES: 'Release notes for 0.0.0',
},
buildroot: {
getBuildrootUpdateInfo: jest.fn(),
getUpdateFileContents: jest.fn(),
},
}
3 changes: 2 additions & 1 deletion app/src/shell/__tests__/api-update.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { mockResolvedValue } from '../../../__util__/mock-promise'
import mockRemote from '../remote'
import * as apiUpdate from '../api-update'

const { apiUpdate: mockApiUpdate } = global.APP_SHELL
const { apiUpdate: mockApiUpdate } = mockRemote

describe('shell/api-update', () => {
let _Blob
Expand Down
3 changes: 2 additions & 1 deletion app/src/shell/__tests__/buildroot.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { mockResolvedValue } from '../../../__util__/mock-promise'
import mockRemote from '../remote'
import * as buildroot from '../buildroot'

const { buildroot: mockBuildrootUpdate } = global.APP_SHELL
const { buildroot: mockBuildrootUpdate } = mockRemote

const reducer = buildroot.buildrootReducer

Expand Down
5 changes: 2 additions & 3 deletions app/src/shell/__tests__/shell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import mockRemote from '../remote'
import { shellMiddleware, getShellConfig } from '..'

jest.mock('../../logger')

const middlewares = [thunk, shellMiddleware]
const mockStore = configureMockStore(middlewares)
const { ipcRenderer: mockIpc, config: mockConfig } = global.APP_SHELL
const { ipcRenderer: mockIpc, config: mockConfig } = mockRemote

describe('app shell module', () => {
afterEach(() => {
Expand Down
3 changes: 2 additions & 1 deletion app/src/shell/api-update.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow
import remote from './remote'
import type { State } from '../types'

export type ApiUpdateInfo = {
Expand All @@ -8,7 +9,7 @@ export type ApiUpdateInfo = {

const {
apiUpdate: { getUpdateInfo, getUpdateFileContents },
} = global.APP_SHELL
} = remote

export function apiUpdateReducer(state: ?ApiUpdateInfo) {
if (!state) return getUpdateInfo()
Expand Down
3 changes: 2 additions & 1 deletion app/src/shell/buildroot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import semver from 'semver'
import remote from './remote'
import type { State, Action } from '../types'

export type BuildrootUpdateInfo = {|
Expand All @@ -15,7 +16,7 @@ export type BuildrootState = {

const {
buildroot: { getUpdateFileContents },
} = global.APP_SHELL
} = remote

export type BuildrootAction =
| {| type: 'buildroot:UPDATE_INFO', payload: BuildrootUpdateInfo | null |}
Expand Down
3 changes: 2 additions & 1 deletion app/src/shell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { combineReducers } from 'redux'

import createLogger from '../logger'
import remote from './remote'
import { updateReducer } from './update'
import { apiUpdateReducer } from './api-update'
import { buildrootReducer } from './buildroot'
Expand Down Expand Up @@ -36,7 +37,7 @@ const {
update: { CURRENT_VERSION, CURRENT_RELEASE_NOTES },
config: { getConfig },
discovery: { getRobots },
} = global.APP_SHELL
} = remote

const log = createLogger(__filename)

Expand Down
21 changes: 21 additions & 0 deletions app/src/shell/remote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @flow
// access main process remote modules via attachments to `global`
import assert from 'assert'

assert(
global.APP_SHELL_REMOTE,
'Expected APP_SHELL_REMOTE to be attached to global scope; is app-shell/src/preload.js properly configured?'
)

const remote = new Proxy(global.APP_SHELL_REMOTE, {
get(target, propName) {
assert(
propName in target,
`Expected APP_SHELL_REMOTE.${propName} to exist, is app-shell/src/preload.js properly configured?`
)

return target[propName]
},
})

export default remote
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module.exports = {
setupFiles: [
'<rootDir>/__mocks__/setup-mock-globals.js',
'<rootDir>/scripts/setup-enzyme.js',
'<rootDir>/scripts/setup-global-mocks.js',
],
Expand Down
8 changes: 8 additions & 0 deletions scripts/setup-global-mocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
'use strict'

jest.mock('electron')
jest.mock('electron-updater')
jest.mock('electron-store')

jest.mock('../components/src/deck/getDeckDefinitions')
jest.mock('../app/src/getLabware')
jest.mock('../app/src/logger')
jest.mock('../app/src/shell/remote')
jest.mock('../app-shell/src/config')
jest.mock('../app-shell/src/log')