Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/components/message/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getActiveTags,
ppDebug,
createState,
parseObjFromEncoding,
getRequestDuration,
getTsCookieFromStorage,
getOrCreateDeviceID
Expand Down Expand Up @@ -187,8 +186,9 @@ const Message = function ({ markup, meta, parentStyles, warnings }) {
request('GET', `${window.location.origin}/credit-presentment/smart/message?${query}`).then(
({ data: resData }) => {
const jsonData = resData.slice(resData.indexOf('<!--') + 4, resData.indexOf('-->'));
// TODO: Cleanup ternary and remove 'parseObjFromEncoding' from utils; only JSON.parse will be needed.
const data = jsonData.startsWith('{') ? JSON.parse(jsonData) : parseObjFromEncoding(jsonData);

const data = JSON.parse(jsonData);

button.innerHTML = data.markup ?? markup ?? '';
const buttonWidth = button.offsetWidth;
const buttonHeight = button.offsetHeight;
Expand Down
13 changes: 0 additions & 13 deletions src/utils/miscellaneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,6 @@ export function request(method, url, { data, headers, withCredentials } = {}) {
xhttp.send(typeof data === 'object' ? JSON.stringify(data) : data);
});
}

export function parseObjFromEncoding(encodedStr) {
// equivalent to JSON.parse(fromBinary(atob(encodedStr))) as in initScript
const binary = atob(encodedStr);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
// need to use .apply instead of spread operator so IE can understand
const decodedStr = String.fromCharCode.apply(null, new Uint16Array(bytes.buffer));
return JSON.parse(decodedStr);
}

export function createEvent(name) {
if (typeof Event === 'function') {
return new Event(name);
Expand Down
24 changes: 3 additions & 21 deletions 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, parseObjFromEncoding } from 'src/utils';
import { request, createState } from 'src/utils';
import xPropsMock from 'utils/xPropsMock';

const ts = {
Expand All @@ -15,17 +15,10 @@ jest.mock('src/utils', () => ({
getOrCreateDeviceID: jest.fn(() => 'uid_26a2522628_mtc6mjk6nti'),
request: jest.fn(() =>
Promise.resolve({
data: '<!--ewAiAG0AYQByAGsAdQBwACIAOgAiADwAZABpAHYAPgBtAG8AYwBrADwALwBkAGkAdgA+ACIALAAiAG0AZQB0AGEAIgA6AHsAIgBtAGUAcwBzAGEAZwBlAFIAZQBxAHUAZQBzAHQASQBkACIAOgAiADIAMwA0ADUANgAiAH0ALAAiAHAAYQByAGUAbgB0AFMAdAB5AGwAZQBzACIAOgAiAGIAbwBkAHkAIAB7ACAAYwBvAGwAbwByADoAIABiAGwAdQBlADsAIAB9ACIALAAiAHcAYQByAG4AaQBuAGcAcwAiADoAWwBdAH0A-->'
data: '<!--{"markup":"<div>mock</div>","meta":{"messageRequestId":"23456"},"parentStyles":"body { color: blue; }","warnings":[]}-->'
})
),
parseObjFromEncoding: jest.fn(() => ({
markup: '<div>mock</div>',
meta: {
messageRequestId: '23456'
},
parentStyles: 'body { color: blue; }',
warnings: []
})),

// eslint-disable-next-line no-console
ppDebug: jest.fn(() => console.log('PayPal Debug Message')),
getRequestDuration: jest.fn(() => 123)
Expand Down Expand Up @@ -63,7 +56,6 @@ describe('Message', () => {
createState.mockClear();
request.mockClear();
xPropsMock.clear();
parseObjFromEncoding.mockClear();
});

test('Renders the button with styles', () => {
Expand Down Expand Up @@ -180,14 +172,4 @@ describe('Message', () => {
ts
});
});

test('raw json data from request', async () => {
request.mockReturnValue(
Promise.resolve({
data: '<!--{"markup":"<div>json response</div>","meta":{"messageRequestId":"34567"}}-->'
})
);
Message(serverData);
expect(parseObjFromEncoding).not.toHaveBeenCalled();
});
});
Loading