Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,47 @@
<title>Messages Dev Sandbox</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<script src="//localhost.paypal.com:8080/sdk/js?client-id=DEV_US_MULTI&components=messages"></script>
<script src="https://www.paypal.com/sdk/js?client-id=AdSCvWUTxBifIHg5Mq3RHGyQ2SIJAPYi_kA6IYqQa7-m7mf6TyDgzrfoBxqToKN2nDoEfNXy8T3OwgOv&components=messages"></script>
</head>
<body>
<div class="messages" data-pp-message></div>
<h2>DeviceID Drift Demo</h2>
<p>
Open DevTools Network tab and filter by <code>smart/message</code>. Watch the <code>deviceID</code> query
param change after the initial render.
</p>
<div id="amount-display" style="font-size: 18px; margin: 10px 0"></div>
<div class="messages"></div>
<div id="log" style="margin-top: 20px; font-family: monospace; font-size: 13px; white-space: pre-wrap"></div>

<script>
// paypal
// .Messages({
// style: {
// layout: 'flex',
// ratio: '8x1',
// color: 'blue'
// }
// })
// .render('.messages');
var log = document.getElementById('log');
var amountDisplay = document.getElementById('amount-display');

function addLog(msg) {
log.textContent += new Date().toLocaleTimeString() + ' — ' + msg + '\n';
}

amountDisplay.textContent = 'Amount: $100';
addLog('Initial render with amount: $100');

paypal
.Messages({
amount: 100,
style: { layout: 'text' }
})
.render('.messages');

setTimeout(function () {
amountDisplay.textContent = 'Amount: $19.19';
addLog('Prop update — re-rendering with amount: $19.19');

paypal
.Messages({
amount: 19.19,
style: { layout: 'text' }
})
.render('.messages');
}, 120000);
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions src/components/message/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
createState,
getRequestDuration,
getTsCookieFromStorage,
getOrCreateDeviceID,
getGlobalSessionID
} from '../../utils';

Expand Down Expand Up @@ -132,6 +131,7 @@ const Message = function ({ markup, meta, parentStyles, warnings }) {
channel,
contextualComponents,
treatmentsHash,
deviceID,
disableSetCookie,
features,
pageType
Expand Down Expand Up @@ -178,7 +178,7 @@ const Message = function ({ markup, meta, parentStyles, warnings }) {
merchant_config: merchantConfigHash,
channel,
contextual_components: contextualComponents,
deviceID: getOrCreateDeviceID(),
deviceID,
treatments: treatmentsHash,
disableSetCookie,
features,
Expand Down
28 changes: 27 additions & 1 deletion tests/unit/spec/src/components/message/Message.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getByText, fireEvent, queryByText } from '@testing-library/dom';

import Message from 'src/components/message/Message';
import { request, createState } from 'src/utils';
import { request, createState, getOrCreateDeviceID } from 'src/utils';
import xPropsMock from 'utils/xPropsMock';

const ts = {
Expand Down Expand Up @@ -178,4 +178,30 @@ describe('Message', () => {
ts
});
});

test('Prop update uses xprops.deviceID instead of getOrCreateDeviceID', async () => {
const MERCHANT_DEVICE_ID = 'uid_3ee89db893_mdm6mdu6mtk';
const PAYPAL_DEVICE_ID = 'uid_1521da4bd8_mtc6mjk6nti';

window.xprops.amount = 100;
window.xprops.deviceID = MERCHANT_DEVICE_ID;

const messageDocument = document.body.appendChild(Message(serverData));

expect(getByText(messageDocument, /test/i)).toBeInTheDocument();
expect(window.xprops.onReady).toHaveBeenCalledTimes(1);

getOrCreateDeviceID.mockReturnValue(PAYPAL_DEVICE_ID);

updateProps({ amount: 500 });

await new Promise(resolve => setTimeout(resolve, 0));

expect(request).toHaveBeenCalledTimes(1);

const requestUrl = request.mock.calls[0][1];

expect(requestUrl).toContain(`deviceID=${MERCHANT_DEVICE_ID}`);
expect(requestUrl).not.toContain(PAYPAL_DEVICE_ID);
});
});
Loading