Skip to content

Commit

Permalink
add ipc_bus_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Mar 17, 2020
1 parent a362758 commit 85abe2c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
41 changes: 41 additions & 0 deletions packages/desktop-gui/cypress/integration/ipc_bus_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import ipcBus from '../../src/lib/ipc-bus'

const RANDOM_NUMBER = 0.5

describe('IPC bus', () => {
let oldIpc

beforeEach(() => {
oldIpc = window.ipc

cy.stub(Math, 'random').returns(RANDOM_NUMBER)
cy.stub(window.ipc, 'on').returns()
cy.stub(window.ipc, 'send').returns()
})

afterEach(() => {
window.ipc = oldIpc
})

it('sends event as expected', () => {
ipcBus('foo:bar', 'baz', 'quux')

expect(window.ipc.send).to.be.calledWith('request', RANDOM_NUMBER, 'foo:bar', 'baz', 'quux')
})

it('removes functions & elements from the args', () => {
const obj = {
el: document.querySelector('div'),
fn: () => {},
str: 'foo',
}

ipcBus('bar', obj)

expect(window.ipc.send).to.be.calledWith('request', RANDOM_NUMBER, 'bar', {
el: null,
fn: null,
str: 'foo',
})
})
})
22 changes: 10 additions & 12 deletions packages/desktop-gui/src/lib/ipc-bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ const ipcBus = (...args) => {
// get the last argument
const lastArg = args.pop()

// remove nodes that cannot be cloned
// https://github.com/cypress-io/cypress/issues/6750
args = args.map((arg) => {
return _.cloneDeepWith(arg, (val) => {
if (_.isFunction(val) || _.isElement(val)) {
return null
}

return val
})
})

let fn

// enable the last arg to be a function
Expand Down Expand Up @@ -108,6 +96,16 @@ const ipcBus = (...args) => {
}
}

// remove nodes that cannot be cloned
// https://github.com/cypress-io/cypress/issues/6750
args = args.map((arg) => {
return _.cloneDeepWith(arg, (val) => {
if (_.isFunction(val) || _.isElement(val)) {
return null
}
})
})

// pass in request, id, and remaining args
ipc.send(...['request', id].concat(args))

Expand Down
2 changes: 0 additions & 2 deletions packages/server/lib/gui/events.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ removeUnserializableValues = (obj) =>
if _.isFunction(val)
return null

return val

handleEvent = (options, bus, event, id, type, arg) ->
debug("got request for event: %s, %o", type, arg)

Expand Down

0 comments on commit 85abe2c

Please sign in to comment.