@@ -80,6 +80,7 @@ describe('Braze Forwarder', function() {
8080 this . monthOfBirth = null ;
8181 this . dayOfBirth = null ;
8282 this . customAttributes = { } ;
83+ this . subscriptionGroup = { } ;
8384
8485 this . customAttributeSet = false ;
8586
@@ -137,6 +138,14 @@ describe('Braze Forwarder', function() {
137138 self . customAttributeSet = true ;
138139 self . customAttributes [ key ] = value ;
139140 } ;
141+
142+ this . addToSubscriptionGroup = function ( key ) {
143+ self . subscriptionGroup [ key ] = true ;
144+ }
145+
146+ this . removeFromSubscriptionGroup = function ( key ) {
147+ self . subscriptionGroup [ key ] = false ;
148+ }
140149 } ,
141150 MockBraze = function ( ) {
142151 var self = this ;
@@ -175,6 +184,7 @@ describe('Braze Forwarder', function() {
175184 self . apiKey = apiKey ;
176185 self . baseUrl = options . baseUrl || null ;
177186 self . doNotLoadFontAwesome = options . doNotLoadFontAwesome ;
187+ self . parsedSubscriptionGroupMapping = { } ;
178188 return true ;
179189 } ;
180190
@@ -1107,6 +1117,95 @@ describe('Braze Forwarder', function() {
11071117 ( window . braze . getUser ( ) . yearOfBirth === null ) . should . equal ( true ) ;
11081118 } ) ;
11091119
1120+ it ( 'decodeSubscriptionGroupMappings should return parsed subscriptionGroupIds map when proper setting is given' , function ( ) {
1121+ // sample subscriptionGroupMapping from config
1122+ var subscriptionGroupMapping = '[{"jsmap":null,"map":"subscriptionGroupTest1","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000000"},{"jsmap":null,"map":"subscriptionGroupTest2","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000001"}]' ;
1123+
1124+ // get the decoded mapped subscriptionGroup
1125+ var parsedSubscriptionGroupMapping = mParticle . forwarder . decodeSubscriptionGroupMappings ( subscriptionGroupMapping ) ;
1126+ var expectedResult = {
1127+ 'subscriptionGroupTest1' : '00000000-0000-0000-0000-000000000000' ,
1128+ 'subscriptionGroupTest2' : '00000000-0000-0000-0000-000000000001' ,
1129+ } ;
1130+
1131+ parsedSubscriptionGroupMapping . should . deepEqual ( expectedResult ) ;
1132+ } ) ;
1133+
1134+ it ( 'should set subscription group for mapped attributes when value is true with type boolean' , function ( ) {
1135+ // sample subscriptionGroupMapping from config
1136+ var subscriptionGroupMapping = '[{"jsmap":null,"map":"subscriptionGroupTest1","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000000"},{"jsmap":null,"map":"subscriptionGroupTest2","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000001"}]' ;
1137+
1138+ // initialize Braze kit with subscriptionGroupMappings
1139+ mParticle . forwarder . init (
1140+ {
1141+ apiKey : '123456' ,
1142+ subscriptionGroupMapping : subscriptionGroupMapping ,
1143+ } ,
1144+ reportService . cb ,
1145+ true ,
1146+ null
1147+ ) ;
1148+
1149+ // get the decoded mapped subscriptionGroup
1150+ var parsedSubscriptionGroupMapping = mParticle . forwarder . decodeSubscriptionGroupMappings ( subscriptionGroupMapping ) ;
1151+
1152+ // set attribute subscriptionGroupTest1 with boolean value true should call Braze's addToSubscriptionGroup since it's mapped
1153+ mParticle . forwarder . setUserAttribute ( 'subscriptionGroupTest1' , true ) ;
1154+ var mappedSubscriptionGroupId = parsedSubscriptionGroupMapping [ 'subscriptionGroupTest1' ] ;
1155+ window . braze . getUser ( ) . subscriptionGroup [ mappedSubscriptionGroupId ] . should . equal ( true ) ;
1156+ } ) ;
1157+
1158+ it ( 'should set subscription group for mapped attributes when value is false with type boolean' , function ( ) {
1159+ // sample subscriptionGroupMapping from config
1160+ var subscriptionGroupMapping = '[{"jsmap":null,"map":"subscriptionGroupTest1","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000000"},{"jsmap":null,"map":"subscriptionGroupTest2","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000001"}]' ;
1161+
1162+ // initialize Braze kit with subscriptionGroupMappings
1163+ mParticle . forwarder . init (
1164+ {
1165+ apiKey : '123456' ,
1166+ subscriptionGroupMapping : subscriptionGroupMapping ,
1167+ } ,
1168+ reportService . cb ,
1169+ true ,
1170+ null
1171+ ) ;
1172+
1173+ // get the decoded mapped subscriptionGroup
1174+ var parsedSubscriptionGroupMapping = mParticle . forwarder . decodeSubscriptionGroupMappings ( subscriptionGroupMapping ) ;
1175+
1176+ // set attribute subscriptionGroupTest2 with boolean value false should call Braze's removeFromSubscriptionGroup since it's mapped
1177+ mParticle . forwarder . setUserAttribute ( 'subscriptionGroupTest2' , false ) ;
1178+ var mappedSubscriptionGroupId = parsedSubscriptionGroupMapping [ 'subscriptionGroupTest2' ] ;
1179+ window . braze . getUser ( ) . subscriptionGroup [ mappedSubscriptionGroupId ] . should . equal ( false ) ;
1180+ } ) ;
1181+
1182+ it ( 'should not set subscription group for mapped attributes when value type is not boolean' , function ( ) {
1183+ // sample subscriptionGroupMapping from config
1184+ var subscriptionGroupMapping = '[{"jsmap":null,"map":"subscriptionGroupTest1","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000000"},{"jsmap":null,"map":"subscriptionGroupTest2","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000001"}]' ;
1185+
1186+ // initialize Braze kit with subscriptionGroupMappings
1187+ mParticle . forwarder . init (
1188+ {
1189+ apiKey : '123456' ,
1190+ subscriptionGroupMapping : subscriptionGroupMapping ,
1191+ } ,
1192+ reportService . cb ,
1193+ true ,
1194+ null
1195+ ) ;
1196+
1197+ // should log error if mapped attribute value is not type boolean
1198+ mParticle . forwarder . logger = {
1199+ verbose : function ( msg ) {
1200+ mParticle . forwarder . msg = msg ;
1201+ } ,
1202+ } ;
1203+
1204+ mParticle . forwarder . setUserAttribute ( 'subscriptionGroupTest1' , 'testStringValue' ) ;
1205+ var expectedMessage = `mParticle - Braze Web Kit log:\nCan\'t call setSubscriptionGroups on forwarder Appboy, setSubscriptionGroups must set this value to a boolean:\n` ;
1206+ mParticle . forwarder . msg . should . equal ( expectedMessage )
1207+ } ) ;
1208+
11101209 it ( 'should not set default values if a string is not passed as the attribute' , function ( ) {
11111210 mParticle . forwarder . setUserAttribute ( 'first_name' , 'John' ) ;
11121211 mParticle . forwarder . setUserAttribute ( 'last_name' , 'Doe' ) ;
@@ -1119,7 +1218,7 @@ describe('Braze Forwarder', function() {
11191218 it ( 'should set a custom user attribute' , function ( ) {
11201219 mParticle . forwarder . setUserAttribute ( 'test' , 'result' ) ;
11211220 window . braze . getUser ( ) . should . have . property ( 'customAttributeSet' , true ) ;
1122- window . braze . getUser ( ) . customAttributes [ 'test' ] . should . equal ( 'result' ) ; ;
1221+ window . braze . getUser ( ) . customAttributes [ 'test' ] . should . equal ( 'result' ) ;
11231222 } ) ;
11241223
11251224 it ( 'should set a custom user attribute of diffferent types' , function ( ) {
0 commit comments