Skip to content

Commit

Permalink
chore: cut release (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seavenly authored Nov 19, 2024
2 parents 99b7ed2 + a2e49c1 commit 3ba643d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 39 deletions.
5 changes: 3 additions & 2 deletions src/components/message/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ const Message = function ({ markup, meta, parentStyles, warnings }) {

request('GET', `${window.location.origin}/credit-presentment/smart/message?${query}`).then(
({ data: resData }) => {
const encodedData = resData.slice(resData.indexOf('<!--') + 4, resData.indexOf('-->'));
const data = parseObjFromEncoding(encodedData);
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);
button.innerHTML = data.markup ?? markup ?? '';
const buttonWidth = button.offsetWidth;
const buttonHeight = button.offsetHeight;
Expand Down
13 changes: 12 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, parseObjFromEncoding } from 'src/utils';
import xPropsMock from 'utils/xPropsMock';

const ts = {
Expand Down Expand Up @@ -63,6 +63,7 @@ describe('Message', () => {
createState.mockClear();
request.mockClear();
xPropsMock.clear();
parseObjFromEncoding.mockClear();
});

test('Renders the button with styles', () => {
Expand Down Expand Up @@ -179,4 +180,14 @@ 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();
});
});
31 changes: 3 additions & 28 deletions utils/devServerProxy/lib/miscellaneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,15 @@ export const localizeCurrency =

export const waitForTimeout = timeout => new Promise(resolve => setTimeout(resolve, timeout));

export function btoa(str) {
const buffer = Buffer.from(str.toString(), 'binary');
return buffer.toString('base64');
}

export function toBinary(string) {
const codeUnits = new Uint16Array(string.length);
for (let i = 0; i < codeUnits.length; i++) {
codeUnits[i] = string.charCodeAt(i);
}
return String.fromCharCode(...new Uint8Array(codeUnits.buffer));
}

export const createMockZoidMarkup = ({ component, scriptUID, port, encodedData }) => {
export const createMockZoidMarkup = ({ component, scriptUID, port, jsonData }) => {
const setupFunctionName = component === 'message' ? 'crc.setupMessage' : 'crc.setupModal';

const interfaceScript = `<script>var interface = (window.opener ?? window.parent).document.querySelector('[data-uid-auto="${scriptUID}"]').outerHTML; document.write(interface);</script>`;
const componentScript = `<script src="//localhost.paypal.com:${port}/smart-credit-${component}.js"></script>`;
const initializerScript = `<script>${setupFunctionName}(JSON.parse(fromBinary(atob(document.firstChild.nodeValue))))</script>`;
const utilScript = `
<script>
function fromBinary(binary) {
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return String.fromCharCode.apply(null, new Uint16Array(bytes.buffer));
}
</script>
`;
const initializerScript = `<script>${setupFunctionName}(JSON.parse(document.firstChild.nodeValue))</script>`;

return `
<!--${encodedData}-->
<!--${jsonData}-->
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -104,7 +80,6 @@ export const createMockZoidMarkup = ({ component, scriptUID, port, encodedData }
<body>
${component !== 'modal-v2-lander' ? interfaceScript : ''}
${componentScript}
${utilScript}
${initializerScript}
</body>
</html>
Expand Down
6 changes: 3 additions & 3 deletions utils/devServerProxy/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import got from 'got';

import { PORT, VARIANT } from '../../src/server/constants';
import { populateTemplate, createMockZoidMarkup, waitForTimeout, btoa, toBinary } from './lib/miscellaneous';
import { populateTemplate, createMockZoidMarkup, waitForTimeout } from './lib/miscellaneous';
import getDevAccountDetails from './lib/devAccountDetails';

// set this environment variable to simulate the time for the request to be answered
Expand Down Expand Up @@ -104,8 +104,8 @@ export default function createMessageRoutes(app, server, compiler) {
app.get('/credit-presentment/smart/message', async (req, res) => {
const { scriptUID } = req.query;
const props = await getMessageData(req, compiler);
const encodedData = btoa(toBinary(JSON.stringify(props)));
const markup = createMockZoidMarkup({ component: 'message', encodedData, scriptUID, port });
const jsonData = JSON.stringify(props);
const markup = createMockZoidMarkup({ component: 'message', jsonData, scriptUID, port });

await waitForTimeout(REQUEST_DELAY);

Expand Down
10 changes: 5 additions & 5 deletions utils/devServerProxy/modals.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMockZoidMarkup, populateTemplate, waitForTimeout, btoa, toBinary } from './lib/miscellaneous';
import { createMockZoidMarkup, populateTemplate, waitForTimeout } from './lib/miscellaneous';
import getDevAccountDetails from './lib/devAccountDetails';

const REQUEST_DELAY = process.env.REQUEST_DELAY ?? 500;
Expand Down Expand Up @@ -53,7 +53,7 @@ export default function createModalRoutes(app, server) {
app.get('/credit-presentment/smart/modal', async (req, res) => {
const { scriptUID } = req.query;
const props = getModalData(req);
const encodedData = btoa(toBinary(JSON.stringify(props)));
const jsonData = JSON.stringify(props);

const postfix = (() => {
if (props.views) {
Expand All @@ -77,7 +77,7 @@ export default function createModalRoutes(app, server) {

const markup = createMockZoidMarkup({
component: `modal-${postfix}`,
encodedData,
jsonData,
scriptUID,
port
});
Expand All @@ -97,10 +97,10 @@ export default function createModalRoutes(app, server) {

app.get('/credit-presentment/lander/modal', async (req, res) => {
const props = getModalData(req);
const encodedData = btoa(toBinary(JSON.stringify(props)));
const jsonData = JSON.stringify(props);
const markup = createMockZoidMarkup({
component: 'modal-v2-lander',
encodedData,
jsonData,
port
});

Expand Down

0 comments on commit 3ba643d

Please sign in to comment.