-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
M-OC
committed
Aug 29, 2019
1 parent
55a85ba
commit e626b22
Showing
7 changed files
with
107 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,40 @@ | ||
/* @flow */ | ||
import { getClientID, getMerchantID } from '@paypal/sdk-client/src'; | ||
|
||
export const getPropertyId = ({ paramsToPropertyIdUrl }) => { | ||
return new Promise(resolve => { | ||
const clientId = getClientID(); | ||
const merchantId = getMerchantID()[0]; | ||
const propertyIdKey = `property-id-${ clientId }-${ merchantId }`; | ||
const savedPropertyId = window.localStorage.getItem(propertyIdKey); | ||
const currentUrl = `${ window.location.protocol }//${ window.location.host }`; | ||
if (savedPropertyId) { | ||
return resolve(savedPropertyId); | ||
} | ||
let url; | ||
if (paramsToPropertyIdUrl) { | ||
url = paramsToPropertyIdUrl(); | ||
} else { | ||
url = 'https://paypal.com/tagmanager/containers/xo'; | ||
} | ||
return window.fetch(`${ url }?mrid=${ merchantId }&url=${ encodeURIComponent(currentUrl) }`) | ||
.then(res => { | ||
if (res.status === 200) { | ||
return res; | ||
} | ||
}) | ||
.then(r => r.json()).then(container => { | ||
window.localStorage.setItem(propertyIdKey, container.id); | ||
resolve(container.id); | ||
}) | ||
.catch(() => { | ||
// doing nothing for now since there's no logging | ||
}); | ||
}); | ||
}; | ||
import type { | ||
Config | ||
} from './types'; | ||
|
||
export const getPropertyId = ({ paramsToPropertyIdUrl } : Config) => { | ||
// $FlowFixMe | ||
return new Promise(resolve => { | ||
const clientId = getClientID(); | ||
const merchantId = getMerchantID()[0]; | ||
const propertyIdKey = `property-id-${ clientId }-${ merchantId }`; | ||
const savedPropertyId = window.localStorage.getItem(propertyIdKey); | ||
const currentUrl = `${ window.location.protocol }//${ window.location.host }`; | ||
if (savedPropertyId) { | ||
return resolve(savedPropertyId); | ||
} | ||
let url; | ||
// $FlowFixMe | ||
if (paramsToPropertyIdUrl) { | ||
url = paramsToPropertyIdUrl(); | ||
} else { | ||
url = 'https://paypal.com/tagmanager/containers/xo'; | ||
} | ||
return window.fetch(`${ url }?mrid=${ merchantId }&url=${ encodeURIComponent(currentUrl) }`) | ||
.then(res => { | ||
if (res.status === 200) { | ||
return res; | ||
} | ||
}) | ||
.then(r => r.json()).then(container => { | ||
window.localStorage.setItem(propertyIdKey, container.id); | ||
resolve(container.id); | ||
}) | ||
.catch(() => { | ||
// doing nothing for now since there's no logging | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -464,7 +464,7 @@ describe('paypal.Tracker', () => { | |
user: { | ||
id: 'abc123', | ||
email: '__test__email9', | ||
name: '__test__userName9', | ||
name: '__test__userName9' | ||
}, | ||
propertyId, | ||
trackingType: 'setUser', | ||
|
@@ -498,7 +498,7 @@ describe('paypal.Tracker', () => { | |
user: { | ||
id: 'abc123', | ||
email: '[email protected]', | ||
name: '__test__name', | ||
name: '__test__name' | ||
}, | ||
propertyId, | ||
trackingType: 'setUser', | ||
|
@@ -546,7 +546,7 @@ describe('paypal.Tracker', () => { | |
user: { | ||
id: 'abc123', | ||
email: '__test__email2', | ||
name: '__test__name1', | ||
name: '__test__name1' | ||
}, | ||
propertyId, | ||
trackingType: 'cartEvent', | ||
|
@@ -588,7 +588,7 @@ describe('paypal.Tracker', () => { | |
total: '12345.67', | ||
currencyCode: 'USD', | ||
cartEventType: 'addToCart', | ||
user: { id: '__test__cookie-id' }, | ||
user: { id: '__test__cookie-id', email: null, name: null }, | ||
propertyId, | ||
trackingType: 'cartEvent', | ||
clientId: 'abcxyz123', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
/* global it describe beforeEach expect jest */ | ||
/* @flow */ | ||
/* global it describe beforeEach afterAll expect jest */ | ||
import { Tracker } from '../../src/tracker-component'; | ||
import { getPropertyId } from '../../src/lib/get-property-id'; | ||
import { track } from '../../src/lib/track'; | ||
|
||
import constants from '../../src/lib/constants'; | ||
|
||
jest.mock('../../src/lib/track') | ||
jest.mock('../../src/lib/track'); | ||
jest.mock('../../src/lib/get-property-id', () => { | ||
return { | ||
// eslint-disable-next-line require-await | ||
getPropertyId: async () => 'mockpropertyidofsomekind' | ||
} | ||
}) | ||
}; | ||
}); | ||
|
||
describe('setUser', () => { | ||
const { defaultTrackerConfig } = constants | ||
const { defaultTrackerConfig } = constants; | ||
|
||
let config; | ||
let mockItem; | ||
|
@@ -35,74 +35,70 @@ describe('setUser', () => { | |
price: '100.00', | ||
title: 'Best Buy', | ||
url: 'http://localhost.paypal.com:8080/us/gifts/brands/best-buy', | ||
quantity: 1, | ||
quantity: 1 | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
track.mockReset(); | ||
window.localStorage.removeItem('paypal-cr-cart'); | ||
window.localStorage.removeItem('paypal-cr-cart-expirty'); | ||
}) | ||
}); | ||
|
||
afterAll(() => { | ||
track.mockRestore(); | ||
getPropertyId.mockRestore(); | ||
}) | ||
}); | ||
|
||
it('user should be set when the tracker is initialized', () => { | ||
const tracker = Tracker(config); | ||
let args | ||
|
||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
|
||
args = track.mock.calls | ||
const args = track.mock.calls; | ||
|
||
expect(args[0][0].user).toEqual(config.user); | ||
expect(args[1][0].user).toEqual(config.user); | ||
}); | ||
|
||
it('no user should be set if no configuration is passed to initialization', (done) => { | ||
const tracker = Tracker(); | ||
let args | ||
|
||
// wait for mock propertyId to resolve | ||
setTimeout(() => { | ||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
|
||
args = track.mock.calls | ||
const args = track.mock.calls; | ||
expect(args[0][0].user).toEqual(defaultTrackerConfig.user); | ||
expect(args[1][0].user).toEqual(defaultTrackerConfig.user); | ||
done() | ||
}, 100) | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('no user should be set if no user is passed to initialization', () => { | ||
const tracker = Tracker({propertyId: 'somevalue'}); | ||
let args | ||
const tracker = Tracker({ propertyId: 'somevalue' }); | ||
|
||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
|
||
args = track.mock.calls | ||
const args = track.mock.calls; | ||
expect(args[0][0].user).toEqual(defaultTrackerConfig.user); | ||
expect(args[1][0].user).toEqual(defaultTrackerConfig.user); | ||
}); | ||
|
||
it('user should be set when set user is called', () => { | ||
const tracker = Tracker({propertyId: 'somevalue'}); | ||
let args | ||
const tracker = Tracker({ propertyId: 'somevalue' }); | ||
|
||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
|
||
tracker.setUser(config.user) | ||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
tracker.setUser(config.user); | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
|
||
args = track.mock.calls | ||
const args = track.mock.calls; | ||
expect(args[0][0].user).toEqual(defaultTrackerConfig.user); | ||
expect(args[1][0].user).toEqual(defaultTrackerConfig.user); | ||
expect(args[2][0].user).toEqual(config.user); | ||
|
@@ -115,19 +111,18 @@ describe('setUser', () => { | |
id: 'wut', | ||
name: 'Steve Jobs', | ||
email: '[email protected]' | ||
} | ||
}; | ||
|
||
const tracker = Tracker(config); | ||
let args | ||
|
||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
|
||
tracker.setUser(alternateUser) | ||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
tracker.setUser(alternateUser); | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
|
||
args = track.mock.calls | ||
const args = track.mock.calls; | ||
expect(args[0][0].user).toEqual(config.user); | ||
expect(args[1][0].user).toEqual(config.user); | ||
expect(args[2][0].user).toEqual(alternateUser); | ||
|
@@ -141,19 +136,18 @@ describe('setUser', () => { | |
id: 'wut', | ||
name: 'Steve Jobs', | ||
email: '[email protected]' | ||
} | ||
}; | ||
|
||
const tracker = Tracker({propertyId: 'somevalue'}); | ||
let args | ||
const tracker = Tracker({ propertyId: 'somevalue' }); | ||
|
||
tracker.setUser(alternateUser) | ||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
tracker.setUser(alternateUser); | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
|
||
tracker.setUser({user: config.user}) | ||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
args = track.mock.calls | ||
tracker.setUser({ user: config.user }); | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
const args = track.mock.calls; | ||
|
||
expect(args[0][0].user).toEqual(alternateUser); | ||
expect(args[1][0].user).toEqual(alternateUser); | ||
|
@@ -168,22 +162,21 @@ describe('setUser', () => { | |
id: 'wut', | ||
name: 'Steve Jobs', | ||
email: '[email protected]' | ||
} | ||
}; | ||
const tracker = Tracker(config); | ||
let args | ||
|
||
tracker.setUser({ | ||
id: null, | ||
email: null, | ||
name: null | ||
}) | ||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
|
||
tracker.setUser(alternateUser) | ||
tracker.addToCart({items: [mockItem]}) | ||
tracker.removeFromCart({items: [mockItem]}) | ||
args = track.mock.calls | ||
}); | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
|
||
tracker.setUser(alternateUser); | ||
tracker.addToCart({ items: [ mockItem ] }); | ||
tracker.removeFromCart({ items: [ mockItem ] }); | ||
const args = track.mock.calls; | ||
|
||
expect(args[0][0].user).toEqual(defaultTrackerConfig.user); | ||
expect(args[1][0].user).toEqual(defaultTrackerConfig.user); | ||
|