Skip to content

Commit

Permalink
Merge pull request #3195 from LiskHQ/3194-launch-protocol-fix
Browse files Browse the repository at this point in the history
Adapt launch protocol handling - Closes #3194
  • Loading branch information
reyraa committed Nov 23, 2020
2 parents 068da8c + 997cc54 commit cf467e0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
26 changes: 18 additions & 8 deletions src/utils/externalLinks.js
@@ -1,20 +1,30 @@
import history from '../history';

const sendRegex = /^\/(wallet|wallet\/send|main\/transactions\/send)$/;
const sendRedirect = '/wallet?modal=send';

const voteRegex = /^\/(main\/voting\/vote|delegates\/vote|vote)$/;
const voteRedirect = '/wallet?modal=votingQueue';

export default {
init: () => {
const { ipc } = window;

if (ipc) {
ipc.on('openUrl', (action, url) => {
const protocol = url.split(':/')[0];
let normalizedUrl = url.split(':/')[1];
const [protocol, rest] = url.split(':/');
const [normalizedUrl, searchParams] = rest.split('?');

if (protocol && protocol.toLowerCase() === 'lisk' && normalizedUrl) {
normalizedUrl = normalizedUrl
.replace('/main/transactions/send', '/wallet/send')
.replace('/wallet', '/wallet/send')
.replace('/main/voting/vote', '/delegates/vote');
history.push(normalizedUrl);
history.replace(normalizedUrl);
let redirectUrl = normalizedUrl;
if (normalizedUrl.match(sendRegex)) {
redirectUrl = sendRedirect + (searchParams ? `&${searchParams}` : '');
} else if (normalizedUrl.match(voteRegex)) {
redirectUrl = voteRedirect + (searchParams ? `&${searchParams}` : '');
}

history.push(redirectUrl);
history.replace(redirectUrl);
}
});
}
Expand Down
43 changes: 36 additions & 7 deletions src/utils/externalLinks.test.js
@@ -1,25 +1,32 @@
import { expect } from 'chai';
import { spy, mock } from 'sinon';
import externalLinks from './externalLinks';
import history from '../history';
import routes from '../constants/routes';

jest.mock('../history', () => ({
push: jest.fn(), replace: jest.fn(),
}));

describe('externalLinks', () => {
const historyMock = mock(history);
const ipc = {
on: spy(),
on: jest.fn(),
};

beforeEach(() => {
ipc.on.mockClear();
history.replace.mockReset();
history.push.mockReset();
});

it('calling init when ipc is not on window should do nothing', () => {
window.ipc = null;
externalLinks.init();
expect(ipc.on).to.not.have.been.calledWith();
expect(ipc.on).not.toHaveBeenCalled();
});

it('calling init when ipc is available on window should bind listeners', () => {
window.ipc = ipc;
externalLinks.init();
expect(ipc.on).to.have.been.calledWith();
expect(ipc.on).toHaveBeenCalled();
});

it('opens url', () => {
Expand All @@ -30,6 +37,28 @@ describe('externalLinks', () => {

externalLinks.init();
callbacks.openUrl({}, 'lisk://register');
historyMock.expects('replace').once().withArgs(routes.register.path);
expect(history.replace).toHaveBeenCalledWith(routes.register.path);
});

it('opens send modal', () => {
const callbacks = {};
window.ipc = {
on: (event, callback) => { callbacks[event] = callback; },
};

externalLinks.init();
callbacks.openUrl({}, 'lisk://wallet?recipient=1L&amount=100');
expect(history.replace).toHaveBeenCalledWith('/wallet?modal=send&recipient=1L&amount=100');
});

it('opens voting queue modal', () => {
const callbacks = {};
window.ipc = {
on: (event, callback) => { callbacks[event] = callback; },
};

externalLinks.init();
callbacks.openUrl({}, 'lisk://vote?votes=delegate');
expect(history.replace).toHaveBeenCalledWith('/wallet?modal=votingQueue&votes=delegate');
});
});

0 comments on commit cf467e0

Please sign in to comment.