Skip to content

Commit

Permalink
Add more tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhao271 committed Oct 29, 2021
1 parent 503ea48 commit e5efbc2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
6 changes: 4 additions & 2 deletions docs/api/app.md
Expand Up @@ -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' }
Expand All @@ -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) {
Expand Down
57 changes: 55 additions & 2 deletions 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';
Expand Down Expand Up @@ -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', () => {
Expand Down
17 changes: 15 additions & 2 deletions spec/fixtures/api/singleton-data/main.js
Expand Up @@ -13,15 +13,15 @@ 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: {
level: 2,
testkey: 'testvalue2'
}
};
const ackObj = {
let ackObj = {
level: 1,
testkey: 'acktestvalue1',
inner: {
Expand All @@ -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));
});
Expand Down

0 comments on commit e5efbc2

Please sign in to comment.