diff --git a/docs/api/app.md b/docs/api/app.md index c93d383fa7444..aa277b0578487 100755 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -987,7 +987,8 @@ let myWindow = null app.on('first-instance-ack', (event, additionalData) => { // Print out the ack received from the first instance. // Note this event handler must come before the requestSingleInstanceLock call. - console.log(additionalData) + // Expected output: '{"myAckKey":"myAckValue"}' + console.log(JSON.stringify(additionalData)) }) const additionalData = { myKey: 'myValue' } @@ -1000,7 +1001,8 @@ if (!gotTheLock) { // We must call preventDefault if we're sending back data. event.preventDefault() // Print out data received from the second instance. - console.log(additionalData) + // Expected output: '{"myKey":"myValue"}' + console.log(JSON.stringify(additionalData)) // Someone tried to run a second instance, we should focus our window. if (myWindow) { diff --git a/spec-main/api-app-spec.ts b/spec-main/api-app-spec.ts index b67bac74ad002..f774c97f7c29b 100644 --- a/spec-main/api-app-spec.ts +++ b/spec-main/api-app-spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'chai'; +import { assert, expect } from 'chai'; import * as cp from 'child_process'; import * as https from 'https'; import * as http from 'http'; @@ -315,13 +315,66 @@ describe('app module', () => { }); }); - it('sends and receives data', async () => { + it('sends and receives JSON object data', async () => { await testArgumentPassing({ args: ['--send-ack', '--prevent-default', '--send-data'], expectedAdditionalData, expectedAck }); }); + + it('sends and receives numerical data', async () => { + await testArgumentPassing({ + args: ['--send-ack', '--ack-content=1', '--prevent-default', '--send-data', '--data-content=2'], + expectedAdditionalData: 2, + expectedAck: 1 + }); + }); + + it('sends and receives string data', async () => { + await testArgumentPassing({ + args: ['--send-ack', '--ack-content="ack"', '--prevent-default', '--send-data', '--data-content="data"'], + expectedAdditionalData: 'data', + expectedAck: 'ack' + }); + }); + + it('sends and receives boolean data', async () => { + await testArgumentPassing({ + args: ['--send-ack', '--ack-content=true', '--prevent-default', '--send-data', '--data-content=false'], + expectedAdditionalData: false, + expectedAck: true + }); + }); + + it('sends and receives array data', async () => { + await testArgumentPassing({ + args: ['--send-ack', '--ack-content=[1, 2, 3]', '--prevent-default', '--send-data', '--data-content=[2, 3, 4]'], + expectedAdditionalData: [2, 3, 4], + expectedAck: [1, 2, 3] + }); + }); + + it('sends and receives null data', async () => { + await testArgumentPassing({ + args: ['--send-ack', '--ack-content=null', '--prevent-default', '--send-data', '--data-content=null'], + expectedAdditionalData: null, + expectedAck: null + }); + }); + + it('cannot send or receive undefined data', async () => { + try { + await testArgumentPassing({ + args: ['--send-ack', '--ack-content="undefined"', '--prevent-default', '--send-data', '--data-content="undefined"'], + expectedAdditionalData: undefined, + expectedAck: undefined + }); + assert(false); + } catch (e) { + // This is expected. + } + }); }); describe('app.relaunch', () => { diff --git a/spec/fixtures/api/singleton-data/main.js b/spec/fixtures/api/singleton-data/main.js index e1ba175f3669a..f79ed9ccc9bce 100644 --- a/spec/fixtures/api/singleton-data/main.js +++ b/spec/fixtures/api/singleton-data/main.js @@ -13,7 +13,7 @@ const preventDefault = app.commandLine.hasSwitch('prevent-default'); // Send an object back for the ack rather than undefined. const sendAck = app.commandLine.hasSwitch('send-ack'); -const obj = { +let obj = { level: 1, testkey: 'testvalue1', inner: { @@ -21,7 +21,7 @@ const obj = { testkey: 'testvalue2' } }; -const ackObj = { +let ackObj = { level: 1, testkey: 'acktestvalue1', inner: { @@ -30,6 +30,19 @@ const ackObj = { } }; +if (app.commandLine.hasSwitch('data-content')) { + obj = JSON.parse(app.commandLine.getSwitchValue('data-content')); + if (obj === 'undefined') { + obj = undefined; + } +} +if (app.commandLine.hasSwitch('ack-content')) { + ackObj = JSON.parse(app.commandLine.getSwitchValue('ack-content')); + if (ackObj === 'undefined') { + ackObj = undefined; + } +} + app.on('first-instance-ack', (event, additionalData) => { console.log(JSON.stringify(additionalData)); });