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 + test storedevicesettings.js #1079

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions www/__mocks__/cordovaMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const mockBEMUserCache = () => {
return new Promise<void>((rs, rj) =>
setTimeout(() => {
for (let p in _cache) delete _cache[p];
for (let doc in _storage) delete _storage[doc];
rs();
}, 100)
);
Expand Down Expand Up @@ -129,3 +130,21 @@ export const mockBEMDataCollection = () => {
window['cordova'] ||= {};
window['cordova'].plugins.BEMDataCollection = mockBEMDataCollection;
}

export const mockBEMServerCom = () => {
const mockBEMServerCom = {
postUserPersonalData: (actionString, typeString, updateDoc, rs, rj) => {
setTimeout(() => {
_storage["user_data"] = updateDoc;
rs();
}, 100)
},

getUserPersonalData: (actionString, rs, rj) => {
setTimeout(() => {
rs( _storage["user_data"] );
}, 100)
}
}
window['cordova'].plugins.BEMServerComm = mockBEMServerCom;
}
76 changes: 76 additions & 0 deletions www/__tests__/storeDeviceSettings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { initDeviceSettings, afterConsentStore, afterIntroStore } from '../js/splash/storedevicesettings';
import { mockBEMUserCache, mockDevice, mockGetAppVersion, mockBEMDataCollection, mockBEMServerCom, mockCordova } from '../__mocks__/cordovaMocks';
import { mockLogger } from '../__mocks__/globalMocks';
import { storageClear } from '../js/plugin/storage';
import { getUser } from '../js/commHelper';
import { markConsented, readConsentState } from '../js/splash/startprefs';
import { markIntroDone } from '../js/onboarding/onboardingHelper';

mockBEMUserCache();
mockDevice();
mockCordova();
mockLogger();
mockGetAppVersion();
mockBEMDataCollection();
mockBEMServerCom();

global.fetch = (url: string) => new Promise((rs, rj) => {
setTimeout(() => rs({
json: () => new Promise((rs, rj) => {
let myJSON = { "emSensorDataCollectionProtocol": { "protocol_id": "2014-04-6267", "approval_date": "2016-07-14" } };
setTimeout(() => rs(myJSON), 100);
})
}));
}) as any;

it('runs after consent is marked', async () => {
await storageClear({ local: true, native: true });
await readConsentState();
await markConsented();
await initDeviceSettings();
let user = await getUser();
expect(user).toMatchObject({
client_os_version: '14.0.0',
client_app_version: '1.2.3'
})
});

it('does not run if consent is not marked', async () => {
await storageClear({ local: true, native: true });
await readConsentState();
await initDeviceSettings();
let user = await getUser();
expect(user).toBeUndefined();
});

it('does not run after consent, if intro not done', async () => {
await storageClear({ local: true, native: true });
await afterConsentStore();
let user = await getUser();
expect(user).toBeUndefined();
})

it('runs after consent, if intro done', async () => {
await storageClear({ local: true, native: true });
await markIntroDone();
await afterConsentStore();
setTimeout(async () => {
console.log("hello world");
getUser().then((user) => {
expect(user).toMatchObject({
client_os_version: '14.0.0',
client_app_version: '1.2.3'
})
});
}, 5000);
});

it('runs after intro is done', async () => {
await storageClear({ local: true, native: true });
await afterIntroStore();
let user = await getUser();
expect(user).toMatchObject({
client_os_version: '14.0.0',
client_app_version: '1.2.3'
})
});
1 change: 0 additions & 1 deletion www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'leaflet/dist/leaflet.css';
import './js/ngApp.js';
import './js/splash/referral.js';
import './js/splash/pushnotify.js';
import './js/splash/storedevicesettings.js';
import './js/splash/localnotify.js';
import './js/splash/remotenotify.js';
import './js/splash/notifScheduler.js';
Expand Down
2 changes: 2 additions & 0 deletions www/js/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { OnboardingRoute, OnboardingState, getPendingOnboardingState } from './o
import { setServerConnSettings } from './config/serverConn';
import AppStatusModal from './control/AppStatusModal';
import usePermissionStatus from './usePermissionStatus';
import { initDeviceSettings } from './splash/storeDeviceSettings';

const defaultRoutes = (t) => [
{ key: 'label', title: t('diary.label-tab'), focusedIcon: 'check-bold', unfocusedIcon: 'check-outline' },
Expand Down Expand Up @@ -48,6 +49,7 @@ const App = () => {
useEffect(() => {
if (!appConfig) return;
setServerConnSettings(appConfig).then(() => {
initDeviceSettings();
refreshOnboardingState();
});
}, [appConfig]);
Expand Down
3 changes: 1 addition & 2 deletions www/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { addStatError, addStatReading, statKeys } from './plugin/clientStats';
import { getPendingOnboardingState } from './onboarding/onboardingHelper';

angular.module('emission.controllers', ['emission.splash.pushnotify',
'emission.splash.storedevicesettings',
'emission.splash.localnotify',
'emission.splash.remotenotify'])

Expand All @@ -14,7 +13,7 @@ angular.module('emission.controllers', ['emission.splash.pushnotify',
.controller('DashCtrl', function($scope) {})

.controller('SplashCtrl', function($scope, $state, $interval, $rootScope,
PushNotify, StoreDeviceSettings,
PushNotify,
LocalNotify, RemoteNotify) {
console.log('SplashCtrl invoked');
// alert("attach debugger!");
Expand Down
1 change: 1 addition & 0 deletions www/js/onboarding/SaveQrPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { preloadDemoSurveyResponse } from "./SurveyPage";
import { storageSet } from "../plugin/storage";
import { registerUser } from "../commHelper";
import { resetDataAndRefresh } from "../config/dynamicConfig";
import { afterConsentStore } from "../splash/storeDeviceSettings";
import { markConsented } from "../splash/startprefs";
import i18next from "i18next";

Expand Down
3 changes: 3 additions & 0 deletions www/js/onboarding/SurveyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useTranslation } from "react-i18next";
import { DateTime } from "luxon";
import { onboardingStyles } from "./OnboardingStack";
import { displayErrorMsg } from "../plugin/logger";
import { afterIntroStore } from "../splash/storeDeviceSettings";
import i18next from "i18next";

let preloadedResponsePromise: Promise<any> = null;
Expand Down Expand Up @@ -58,6 +59,8 @@ const SurveyPage = () => {
setSurveyModalVisible(false);
markIntroDone();
refreshOnboardingState();

afterIntroStore();
}

return (<>
Expand Down
48 changes: 0 additions & 48 deletions www/js/splash/storedevicesettings.js

This file was deleted.

59 changes: 59 additions & 0 deletions www/js/splash/storedevicesettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { updateUser } from '../commHelper';
import { displayError, logDebug, logInfo } from '../plugin/logger';
import { readConsentState, isConsented } from './startprefs';
import { readIntroDone } from '../onboarding/onboardingHelper';
import i18next from 'i18next';

let _datacollect;

const storeDeviceSettings = function () {
var lang = i18next.resolvedLanguage;
var manufacturer = window['device'].manufacturer;
var osver = window['device'].version;
return window['cordova'].getAppVersion.getVersionNumber().then(function (appver) {
var updateJSON = {
phone_lang: lang,
curr_platform: window['cordova'].platformId,
manufacturer: manufacturer,
client_os_version: osver,
client_app_version: appver
};
logDebug("About to update profile with settings = " + JSON.stringify(updateJSON));
return updateUser(updateJSON);
}).then(function (updateJSON) {
// alert("Finished saving token = "+JSON.stringify(t.token));
}).catch(function (error) {
displayError(error, "Error in updating profile to store device settings");
});
}

export const initDeviceSettings = function () {
_datacollect = window['cordova'].plugins.BEMDataCollection;
return readConsentState()
.then(isConsented)
.then(function (consentState) {
if (consentState == true) {
return storeDeviceSettings();
} else {
logInfo("no consent yet, waiting to store device settings in profile");
}
logInfo("storedevicesettings startup done");
});
}

export const afterConsentStore = function () {
console.log("in storedevicesettings, executing after consent is received");
readIntroDone().then((introDone) => {
console.log("intro is done?", introDone);
if (introDone) {
console.log("intro is done -> reconsent situation, we already have a token -> register");
return storeDeviceSettings();
}
});
};

export const afterIntroStore = function () {
console.log("intro is done -> original consent situation, we should have a token by now -> register");
return storeDeviceSettings();
};

Loading