This is an automated email from the ASF dual-hosted git repository.
poorejc pushed a commit to branch FLAGON-434
in repository https://gitbox.apache.org/repos/asf/incubator-flagon-useralejs.git
The following commit(s) were added to refs/heads/FLAGON-434 by this push:
new 094b872 [FLAGON-451] updated WIP mods to unit testing framework to
mock sessionStorage
094b872 is described below
commit 094b87238e048181f878c961e740747f75c84b25
Author: poorejc <[email protected]>
AuthorDate: Fri Sep 13 01:49:10 2019 -0400
[FLAGON-451] updated WIP mods to unit testing framework to mock
sessionStorage
---
build/UserAleWebExtension/background.js | 18 ++++++++++++++++++
build/UserAleWebExtension/content.js | 10 ++++++----
build/userale-2.0.2.js | 10 ++++++----
build/userale-2.0.2.min.js | 2 +-
example/index_form.html | 4 ++--
package-lock.json | 6 ++++++
package.json | 1 +
src/getInitialSettings.js | 10 ++++++----
test/getInitialSettings_spec.js | 3 +++
9 files changed, 49 insertions(+), 15 deletions(-)
diff --git a/build/UserAleWebExtension/background.js
b/build/UserAleWebExtension/background.js
index d39c251..ee9e91f 100644
--- a/build/UserAleWebExtension/background.js
+++ b/build/UserAleWebExtension/background.js
@@ -65,6 +65,24 @@ var ADD_LOG = prefix + 'ADD_LOG';
* limitations under the License.
*/
+var SessionId = getSessionId("userAleSessionId", 'session_' +
String(Date.now()));
+
+/**
+ * defines sessionId, stores it in sessionStorage, checks to see if there is a
sessionId in
+ * storage when script is started. This prevents events like 'submit', which
refresh page data
+ * from refreshing the current user session
+ *
+ */
+function getSessionId(sessionKey, value){
+ if (JSON.parse(window.sessionStorage.getItem(sessionKey) === null)) {
+ var storedSession = value;
+ window.sessionStorage.setItem(sessionKey, JSON.stringify(value));
+ } else {
+ storedSession = JSON.parse(window.sessionStorage.getItem(sessionKey));
+ }
+ return storedSession;
+}
+
/**
* Creates a function to normalize the timestamp of the provided event.
diff --git a/build/UserAleWebExtension/content.js
b/build/UserAleWebExtension/content.js
index 894e467..77ad5e3 100644
--- a/build/UserAleWebExtension/content.js
+++ b/build/UserAleWebExtension/content.js
@@ -67,6 +67,8 @@ var version = "2.0.2";
* limitations under the License.
*/
+var SessionId = getSessionId("userAleSessionId", 'session_' +
String(Date.now()));
+
/**
* Extracts the initial configuration settings from the
* currently executing script tag.
@@ -93,7 +95,7 @@ function getInitialSettings() {
settings.toolName = get('data-tool') || null;
settings.userFromParams = get('data-user-from-params') || null;
settings.time = timeStampScale(document.createEvent('CustomEvent'));
- settings.sessionID = get('data-session') || getSessionId("userAleSessionId",
'session_' + String(Date.now()));
+ settings.sessionID = get('data-session') || SessionId;
return settings;
}
@@ -105,11 +107,11 @@ function getInitialSettings() {
*
*/
function getSessionId(sessionKey, value){
- if (sessionStorage.getItem(sessionKey) === null) {
+ if (JSON.parse(window.sessionStorage.getItem(sessionKey) === null)) {
var storedSession = value;
- sessionStorage.setItem(sessionKey, value);
+ window.sessionStorage.setItem(sessionKey, JSON.stringify(value));
} else {
- storedSession = sessionStorage.getItem(sessionKey);
+ storedSession = JSON.parse(window.sessionStorage.getItem(sessionKey));
}
return storedSession;
}
diff --git a/build/userale-2.0.2.js b/build/userale-2.0.2.js
index eaa0d65..2da29db 100644
--- a/build/userale-2.0.2.js
+++ b/build/userale-2.0.2.js
@@ -38,6 +38,8 @@ var userale = (function (exports) {
* limitations under the License.
*/
+ var SessionId = getSessionId("userAleSessionId", 'session_' +
String(Date.now()));
+
/**
* Extracts the initial configuration settings from the
* currently executing script tag.
@@ -64,7 +66,7 @@ var userale = (function (exports) {
settings.toolName = get('data-tool') || null;
settings.userFromParams = get('data-user-from-params') || null;
settings.time = timeStampScale(document.createEvent('CustomEvent'));
- settings.sessionID = get('data-session') ||
getSessionId("userAleSessionId", 'session_' + String(Date.now()));
+ settings.sessionID = get('data-session') || SessionId;
return settings;
}
@@ -76,11 +78,11 @@ var userale = (function (exports) {
*
*/
function getSessionId(sessionKey, value){
- if (sessionStorage.getItem(sessionKey) === null) {
+ if (JSON.parse(window.sessionStorage.getItem(sessionKey) === null)) {
var storedSession = value;
- sessionStorage.setItem(sessionKey, value);
+ window.sessionStorage.setItem(sessionKey, JSON.stringify(value));
} else {
- storedSession = sessionStorage.getItem(sessionKey);
+ storedSession = JSON.parse(window.sessionStorage.getItem(sessionKey));
}
return storedSession;
}
diff --git a/build/userale-2.0.2.min.js b/build/userale-2.0.2.min.js
index 8fd2c91..2c46b63 100644
--- a/build/userale-2.0.2.min.js
+++ b/build/userale-2.0.2.min.js
@@ -15,4 +15,4 @@
* limitations under the License.
* @preserved
*/
-var userale=function(n){"use strict";var a,i,u,l,s,c,d,f,e="2.0.2";function
t(n,o){Object.keys(o).forEach(function(e){if("userFromParams"===e){var
t=function(e){var t=new
RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)"),n=window.location.href.match(t);return
n&&n[2]?decodeURIComponent(n[2].replace(/\+/g,"
")):null}(o[e]);t&&(n.userId=t)}n[e]=o[e]})}var m=null,p=null;function
o(e,t){if(!i.on)return!1;var n=null;t&&(n=t(e));var
o=function(e){return{milli:Math.floor(e),micro:Number((e%1).toFixed(3))}}( [...]
\ No newline at end of file
+var userale=function(n){"use strict";var
a,i,u,l,s,c,d,f,e="2.0.2",t=function(e,t){if(JSON.parse(null===window.sessionStorage.getItem(e))){var
n=t;window.sessionStorage.setItem(e,JSON.stringify(t))}else
n=JSON.parse(window.sessionStorage.getItem(e));return
n}("userAleSessionId","session_"+String(Date.now()));function
o(n,o){Object.keys(o).forEach(function(e){if("userFromParams"===e){var
t=function(e){var t=new
RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)"),n=window.location.href.match(t);return n&
[...]
\ No newline at end of file
diff --git a/example/index_form.html b/example/index_form.html
index c526287..55977fc 100644
--- a/example/index_form.html
+++ b/example/index_form.html
@@ -36,7 +36,7 @@ limitations under the License.
</head>
<body>
-
+<!---
<script type="text/javascript">
const changeMe = "me";
window.userale.options({
@@ -45,7 +45,7 @@ limitations under the License.
"sessionID": "4"
})
</script>
-
+-->
<br>
<br>
<br>
diff --git a/package-lock.json b/package-lock.json
index 6809086..ac1eb67 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -2126,6 +2126,12 @@
"esutils": "^2.0.2"
}
},
+ "dom-storage": {
+ "version": "2.0.2",
+ "resolved":
"https://registry.npmjs.org/dom-storage/-/dom-storage-2.0.2.tgz",
+ "integrity": "sha1-7RfL9oq9EOCu+BgnE+KXxeS1ALA=",
+ "dev": true
+ },
"dot-object": {
"version": "1.9.0",
"resolved":
"https://registry.npmjs.org/dot-object/-/dot-object-1.9.0.tgz",
diff --git a/package.json b/package.json
index 4e6377e..2335be3 100755
--- a/package.json
+++ b/package.json
@@ -47,6 +47,7 @@
"body-parser": "^1.15.0",
"chai": "^3.5.0",
"del": "^2.2.2",
+ "dom-storage": "2.0.2",
"express": "^4.13.4",
"gulp": "^4.0.2",
"gulplog": "^1.0.0",
diff --git a/src/getInitialSettings.js b/src/getInitialSettings.js
index 051cddc..d55806d 100644
--- a/src/getInitialSettings.js
+++ b/src/getInitialSettings.js
@@ -15,6 +15,8 @@
* limitations under the License.
*/
+var SessionId = getSessionId("userAleSessionId", 'session_' +
String(Date.now()));
+
/**
* Extracts the initial configuration settings from the
* currently executing script tag.
@@ -41,7 +43,7 @@ export function getInitialSettings() {
settings.toolName = get('data-tool') || null;
settings.userFromParams = get('data-user-from-params') || null;
settings.time = timeStampScale(document.createEvent('CustomEvent'));
- settings.sessionID = get('data-session') || getSessionId("userAleSessionId",
'session_' + String(Date.now()));
+ settings.sessionID = get('data-session') || SessionId;
return settings;
}
@@ -53,11 +55,11 @@ export function getInitialSettings() {
*
*/
export function getSessionId(sessionKey, value){
- if (sessionStorage.getItem(sessionKey) === null) {
+ if (JSON.parse(window.sessionStorage.getItem(sessionKey) === null)) {
var storedSession = value;
- sessionStorage.setItem(sessionKey, value);
+ window.sessionStorage.setItem(sessionKey, JSON.stringify(value));
} else {
- storedSession = sessionStorage.getItem(sessionKey);
+ storedSession = JSON.parse(window.sessionStorage.getItem(sessionKey));
}
return storedSession;
}
diff --git a/test/getInitialSettings_spec.js b/test/getInitialSettings_spec.js
index 2063332..3f8d834 100644
--- a/test/getInitialSettings_spec.js
+++ b/test/getInitialSettings_spec.js
@@ -16,6 +16,8 @@
*/
import { expect } from 'chai';
import jsdom from 'jsdom';
+import 'jsdom-global/register';
+import { Storage } from 'dom-storage';
import fs from 'fs';
import { createEnv } from './testUtils';
@@ -69,6 +71,7 @@ describe('getInitialSettings', () => {
const html = fs.readFileSync(__dirname +
'/getInitialSettings_fetchAll.html');
createEnv(html, (err, window) => {
+ window.sessionStorage = new Storage(null, { strict: true });
const config = window.userale.options();
expect(config).to.have.property('autostart', true);
expect(config).to.have.property('url', 'http://test.com');