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

Rewrite angular service - customURL #1042

Merged
38 changes: 38 additions & 0 deletions www/__tests__/customURL.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { onLaunchCustomURL } from '../js/splash/customURL';

describe('onLaunchCustomURL', () => {
let mockHandler;

beforeEach(() => {
// create a new mock handler before each test case.
mockHandler = jest.fn();
});

it('tests valid url 1 - should call handler callback with valid URL and the handler should be called with correct parameters', () => {
const validURL = 'emission://login_token?token=nrelop_dev-emulator-program';
const expectedURL = 'login_token?token=nrelop_dev-emulator-program';
const expectedComponents = { route: 'login_token', token: 'nrelop_dev-emulator-program' };
onLaunchCustomURL(validURL, mockHandler);
expect(mockHandler).toHaveBeenCalledWith(expectedURL, expectedComponents);
});

it('tests valid url 2 - should call handler callback with valid URL and the handler should be called with correct parameters', () => {
const validURL = 'emission://test?param1=first&param2=second';
const expectedURL = 'test?param1=first&param2=second';
const expectedComponents = { route: 'test', param1: 'first', param2: 'second' };
onLaunchCustomURL(validURL, mockHandler);
expect(mockHandler).toHaveBeenCalledWith(expectedURL, expectedComponents);
});

it('test invalid url 1 - should not call handler callback with invalid URL', () => {
const invalidURL = 'invalid_url';
onLaunchCustomURL(invalidURL, mockHandler);
expect(mockHandler).not.toHaveBeenCalled();
});

it('tests invalid url 2 - should not call handler callback with invalid URL', () => {
const invalidURL = '';
onLaunchCustomURL(invalidURL, mockHandler);
expect(mockHandler).not.toHaveBeenCalled();
});
})
1 change: 0 additions & 1 deletion www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'leaflet/dist/leaflet.css';

import './js/ngApp.js';
import './js/splash/referral.js';
import './js/splash/customURL.js';
import './js/splash/startprefs.js';
import './js/splash/pushnotify.js';
import './js/splash/storedevicesettings.js';
Expand Down
22 changes: 2 additions & 20 deletions www/js/ngApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,18 @@ import { Provider as PaperProvider } from 'react-native-paper';
import App from './App';
import { getTheme } from './appTheme';
import { SafeAreaView } from 'react-native-safe-area-context';
import { initByUser } from './config/dynamicConfig';

angular.module('emission', ['ionic', 'jm.i18next',
'emission.controllers','emission.services', 'emission.plugin.logger',
'emission.splash.customURLScheme', 'emission.splash.referral',
'emission.services.email',
'emission.splash.referral', 'emission.services.email',
'emission.main', 'pascalprecht.translate', 'LocalStorageModule'])

.run(function($ionicPlatform, $rootScope, $http, Logger,
CustomURLScheme, ReferralHandler, localStorageService) {
.run(function($ionicPlatform, $rootScope, $http, Logger, localStorageService) {
console.log("Starting run");
// ensure that plugin events are delivered after the ionicPlatform is ready
// https://github.com/katzer/cordova-plugin-local-notifications#launch-details
window.skipLocalNotificationReady = true;
// alert("Starting run");
// BEGIN: Global listeners, no need to wait for the platform
// TODO: Although the onLaunch call doesn't need to wait for the platform the
// handlers do. Can we rely on the fact that the event is generated from
// native code, so will only be launched after the platform is ready?
CustomURLScheme.onLaunch(function(event, url, urlComponents){
console.log("GOT URL:"+url);
// alert("GOT URL:"+url);

if (urlComponents.route == 'join') {
ReferralHandler.setupGroupReferral(urlComponents);
} else if (urlComponents.route == 'login_token') {
initByUser(urlComponents);
}
});
// END: Global listeners
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
Expand Down
40 changes: 0 additions & 40 deletions www/js/splash/customURL.js

This file was deleted.

22 changes: 22 additions & 0 deletions www/js/splash/customURL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type UrlComponents = {
[key : string] : string
}

type OnLaunchCustomURL = (rawUrl: string, callback: (url: string, urlComponents: UrlComponents) => void ) => void;


export const onLaunchCustomURL: OnLaunchCustomURL = (rawUrl, handler) => {
shankari marked this conversation as resolved.
Show resolved Hide resolved
try {
const url = rawUrl.split('//')[1];
const [ route, paramString ] = url.split('?');
const paramsList = paramString.split('&');
const urlComponents: UrlComponents = { route : route };
for (let i = 0; i < paramsList.length; i++) {
const [key, value] = paramsList[i].split('=');
urlComponents[key] = value;
}
handler(url, urlComponents);
}catch {
console.log('not a valid url');
}
};