@@ -5,6 +5,7 @@ describe('AmplitudeClient', function() {
55 var CookieStorage = require ( '../src/cookiestorage.js' ) ;
66 var Base64 = require ( '../src/base64.js' ) ;
77 var cookie = require ( '../src/cookie.js' ) ;
8+ var utils = require ( '../src/utils.js' ) ;
89 var querystring = require ( 'querystring' ) ;
910 var JSON = require ( 'json' ) ;
1011 var Identify = require ( '../src/identify.js' ) ;
@@ -381,6 +382,21 @@ describe('AmplitudeClient', function() {
381382 assert . equal ( localStorage . getItem ( 'amplitude_unsent_identify_new_app' ) , existingIdentify ) ;
382383 } ) ;
383384
385+ it ( 'should validate event properties when loading saved events from localStorage' , function ( ) {
386+ var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
387+ '"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
388+ ',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
389+ '"event_properties":"{}","user_properties":{},"uuid":"3c508faa-a5c9-45fa-9da7-9f4f3b992fb0","library"' +
390+ ':{"name":"amplitude-js","version":"2.9.0"},"sequence_number":130}]' ;
391+ localStorage . setItem ( 'amplitude_unsent' , existingEvent ) ;
392+
393+ var amplitude2 = new AmplitudeClient ( '$default_Instance' ) ;
394+ amplitude2 . init ( apiKey , null , { batchEvents : true } ) ;
395+
396+ // check event loaded into memory
397+ assert . deepEqual ( amplitude2 . _unsentEvents [ 0 ] . event_properties , { } ) ;
398+ } ) ;
399+
384400 it ( 'should load saved events from localStorage new keys and send events' , function ( ) {
385401 var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
386402 '"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
@@ -1494,6 +1510,55 @@ describe('AmplitudeClient', function() {
14941510 'sequenceNumber' : 3
14951511 } ) ;
14961512 } ) ;
1513+
1514+ it ( 'should validate event properties' , function ( ) {
1515+ var e = new Error ( 'oops' ) ;
1516+ clock . tick ( 1 ) ;
1517+ amplitude . init ( apiKey , null , { batchEvents : true , eventUploadThreshold : 5 } ) ;
1518+ clock . tick ( 1 ) ;
1519+ amplitude . logEvent ( 'String event properties' , '{}' ) ;
1520+ clock . tick ( 1 ) ;
1521+ amplitude . logEvent ( 'Bool event properties' , true ) ;
1522+ clock . tick ( 1 ) ;
1523+ amplitude . logEvent ( 'Number event properties' , 15 ) ;
1524+ clock . tick ( 1 ) ;
1525+ amplitude . logEvent ( 'Array event properties' , [ 1 , 2 , 3 ] ) ;
1526+ clock . tick ( 1 ) ;
1527+ amplitude . logEvent ( 'Object event properties' , {
1528+ 10 : 'false' , // coerce key
1529+ 'bool' : true ,
1530+ 'null' : null , // should be ignored
1531+ 'function' : utils . log , // should be ignored
1532+ 'regex' : / a f d g / , // should be ignored
1533+ 'error' : e , // coerce value
1534+ 'string' : 'test' ,
1535+ 'array' : [ 0 , 1 , 2 , '3' ] ,
1536+ 'nested_array' : [ 'a' , { 'key' : 'value' } , [ 'b' ] ] ,
1537+ 'object' : { 'key' :'value' , 15 : e } ,
1538+ 'nested_object' : { 'k' :'v' , 'l' :[ 0 , 1 ] , 'o' :{ 'k2' :'v2' , 'l2' : [ 'e2' , { 'k3' : 'v3' } ] } }
1539+ } ) ;
1540+ clock . tick ( 1 ) ;
1541+
1542+ assert . lengthOf ( amplitude . _unsentEvents , 5 ) ;
1543+ assert . lengthOf ( server . requests , 1 ) ;
1544+ var events = JSON . parse ( querystring . parse ( server . requests [ 0 ] . requestBody ) . e ) ;
1545+ assert . lengthOf ( events , 5 ) ;
1546+
1547+ assert . deepEqual ( events [ 0 ] . event_properties , { } ) ;
1548+ assert . deepEqual ( events [ 1 ] . event_properties , { } ) ;
1549+ assert . deepEqual ( events [ 2 ] . event_properties , { } ) ;
1550+ assert . deepEqual ( events [ 3 ] . event_properties , { } ) ;
1551+ assert . deepEqual ( events [ 4 ] . event_properties , {
1552+ '10' : 'false' ,
1553+ 'bool' : true ,
1554+ 'error' : 'Error: oops' ,
1555+ 'string' : 'test' ,
1556+ 'array' : [ 0 , 1 , 2 , '3' ] ,
1557+ 'nested_array' : [ 'a' ] ,
1558+ 'object' : { 'key' :'value' , '15' :'Error: oops' } ,
1559+ 'nested_object' : { 'k' :'v' , 'l' :[ 0 , 1 ] , 'o' :{ 'k2' :'v2' , 'l2' : [ 'e2' ] } }
1560+ } ) ;
1561+ } ) ;
14971562 } ) ;
14981563
14991564 describe ( 'optOut' , function ( ) {
0 commit comments