11const home = SERVER_URL + '/index.html'
22const alt = SERVER_URL + '/alt.html'
3+ const templatePage = SERVER_URL + '/template-fns.html'
34
45describe ( 'integration' , ( ) => {
56 let page
@@ -29,8 +30,8 @@ describe('integration', () => {
2930 if ( postData ) {
3031 postData = JSON . parse ( postData )
3132 }
32- expect ( postData . streamName ) . toMatch ( 'clickstream' )
33- expect ( postData . eventTimestamp ) . toBeGreaterThan ( 15e9 )
33+ expect ( postData . stream ) . toMatch ( 'clickstream' )
34+ expect ( postData . data . timestamp ) . toBeGreaterThan ( 15e9 )
3435 } , 10000 )
3536
3637 it ( 'sends page context with url, title, and referrer' , async ( ) => {
@@ -41,7 +42,7 @@ describe('integration', () => {
4142 await page . goto ( home )
4243 await page . click ( 'button#count-button' )
4344 postData = postData && JSON . parse ( postData )
44- expect ( postData . context ) . toMatchObject ( {
45+ expect ( postData . data . context ) . toMatchObject ( {
4546 url : await page . url ( ) ,
4647 title : await page . title ( ) ,
4748 referrer : '' ,
@@ -50,7 +51,7 @@ describe('integration', () => {
5051 await Promise . all ( [ page . waitForNavigation ( ) , page . click ( 'a' ) ] )
5152 await page . click ( 'button#send-button' )
5253 postData = postData && JSON . parse ( postData )
53- expect ( postData . context ) . toMatchObject ( {
54+ expect ( postData . data . context ) . toMatchObject ( {
5455 url : await page . url ( ) ,
5556 title : await page . title ( ) ,
5657 referrer : expect . stringMatching ( SERVER_URL ) ,
@@ -107,27 +108,27 @@ describe('integration', () => {
107108 await page . type ( 'input#user-input' , userId )
108109 await page . click ( 'button#login-button' )
109110 postData = postData && JSON . parse ( postData )
110- expect ( postData . user ) . toMatchObject ( { id : userId } )
111+ expect ( postData . data . user ) . toMatchObject ( { id : userId } )
111112 postData = undefined // reset for next page
112113
113114 // visit the other page, should persist userId
114115 await Promise . all ( [ page . waitForNavigation ( ) , page . click ( 'a' ) ] )
115116 await page . click ( 'button#send-button' )
116117 postData = postData && JSON . parse ( postData )
117- expect ( postData . user ) . toMatchObject ( { id : userId } )
118+ expect ( postData . data . user ) . toMatchObject ( { id : userId } )
118119 postData = undefined
119120
120121 // do logout, ensure logout sent event does NOT have user id
121122 await page . click ( '#logout-button' )
122123 postData = postData && JSON . parse ( postData )
123- expect ( postData . user ) . toMatchObject ( { id : '' } )
124+ expect ( postData . data . user ) . toMatchObject ( { id : '' } )
124125 postData = undefined
125126
126127 // go back and send event from home page, should NOT have user id
127128 await page . goBack ( )
128129 await page . click ( '#count-button' )
129130 postData = postData && JSON . parse ( postData )
130- expect ( postData . user ) . toMatchObject ( { id : '' } )
131+ expect ( postData . data . user ) . toMatchObject ( { id : '' } )
131132 } , 20000 )
132133
133134 it ( 'persists user id when coming back to site' , async ( ) => {
@@ -144,7 +145,7 @@ describe('integration', () => {
144145 await page . type ( 'input#user-input' , userId )
145146 await page . click ( 'button#login-button' )
146147 postData = postData && JSON . parse ( postData )
147- expect ( postData . user ) . toMatchObject ( { id : userId } )
148+ expect ( postData . data . user ) . toMatchObject ( { id : userId } )
148149 postData = undefined // reset for next page
149150
150151 // visit the other page, should persist userId
@@ -153,6 +154,21 @@ describe('integration', () => {
153154 await page . goto ( alt )
154155 await page . click ( 'button#send-button' )
155156 postData = postData && JSON . parse ( postData )
156- expect ( postData . user ) . toMatchObject ( { id : userId } )
157+ expect ( postData . data . user ) . toMatchObject ( { id : userId } )
157158 } , 8000 )
159+
160+ it ( 'calls template functions onIdentify and onUnidentify' , async ( ) => {
161+ const userId = 'user-id'
162+ const responseDivSelector = 'div#identify-response'
163+ // simulate a login that should store id to send w request
164+ await page . goto ( templatePage )
165+ await page . type ( '#input-user-id' , userId )
166+ await page . click ( 'button#login-button' )
167+ const identifyResponse = await page . $ ( responseDivSelector )
168+ expect ( await identifyResponse . evaluate ( node => node . innerText ) ) . toBe ( userId ) ;
169+
170+ await page . click ( 'button#logout-button' )
171+ const unidentifyResponse = await page . $ ( responseDivSelector )
172+ expect ( await unidentifyResponse . evaluate ( node => node . innerText ) ) . toBe ( '' ) ;
173+ } )
158174} )
0 commit comments