diff --git a/README.md b/README.md
index a9ee5ed374..8b1b3c8a95 100644
--- a/README.md
+++ b/README.md
@@ -39,13 +39,13 @@ The JavaScript SDK lets you track customer event data from your website and send
- [**Usage in Chrome Extensions**](#usage-in-chrome-extensions)
- [**Usage in Serverless Runtimes**](#usage-in-serverless-runtimes)
-| **IMPORTANT**: We have deprecated the service worker export from RudderStack JavaScript SDK npm package and decoupled it to a new package.
If you still wish to use it for your project, see [**@rudderstack/analytics-js-service-worker package**](https://www.npmjs.com/package/@rudderstack/analytics-js-service-worker). |
-| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| **IMPORTANT**: The service worker export has been deprecated from the RudderStack JavaScript SDK npm package and moved to a new package.
If you still wish to use it for your project, see [**@rudderstack/analytics-js-service-worker package**](https://www.npmjs.com/package/@rudderstack/analytics-js-service-worker). |
+| :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
## Installing the JavaScript SDK
| For detailed installation steps, see the [JavaScript SDK documentation](https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/installation/). |
-| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
To integrate the JavaScript SDK with your website, place the following code snippet in the `
${JSON.stringify(testCase.inputData, undefined, 2)}+
${JSON.stringify(testCase.inputData, undefined, 2)}
- ${JSON.stringify(testCase.expectedResult, undefined, 2)} -+ }" data-test-case-id="${testCase.id}" style="white-space: pre-wrap;"> @@ -294,14 +297,17 @@ class TestBook { const observer = new MutationObserver(mutationList => { const resultDataElement = mutationList[0].addedNodes[0].parentNode; const resultData = resultDataElement.textContent.trim(); - const expectedResult = resultRowElement.lastElementChild.childNodes[1].textContent.trim(); - const sanitizedResultData = ResultsAssertions.sanitizeResultData( - resultData, - expectedResult, - ); + // Get the last but one child from resultRowElement + const expectedResult = + resultRowElement.childNodes[ + resultRowElement.childNodes.length - 2 + ].childNodes[1].textContent.trim(); + + const { resultData: sanitizedResultData, expectedResultData: sanitizedExpectedResultData } = + ResultsAssertions.sanitizeResultData(resultData, expectedResult); const assertionResult = ResultsAssertions.assertDeepObjectDiffResult( sanitizedResultData, - expectedResult, + sanitizedExpectedResultData, ); const statusElement = document.getElementById(`test-case-status-${testCaseId}`); @@ -311,6 +317,17 @@ class TestBook { behavior: 'smooth', block: 'center', }); + + const viewDiffElement = document.getElementById(`view-diff-${testCaseId}`); + if (assertionResult === 'success') { + // hide the element + viewDiffElement.classList.add('d-none'); + } else { + // show the element + viewDiffElement.classList.remove('d-none'); + + viewDiffElement.href = `https://jsondiff.com/#left=data:base64,${toBase64(sanitizedExpectedResultData)}&right=data:base64,${toBase64(sanitizedResultData)}`; + } }); observer.observe(resultContainerElement, { diff --git a/packages/sanity-suite/src/testBook/string.js b/packages/sanity-suite/src/testBook/string.js new file mode 100644 index 0000000000..0c17beef11 --- /dev/null +++ b/packages/sanity-suite/src/testBook/string.js @@ -0,0 +1,27 @@ +// The following text encoding and decoding is done before base64 encoding to prevent +// https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem + +/** + * Converts a bytes array to base64 encoded string + * @param bytes bytes array to be converted to base64 + * @returns base64 encoded string + */ +const bytesToBase64 = bytes => { + const binString = Array.from(bytes, x => String.fromCodePoint(x)).join(''); + return globalThis.btoa(binString); +}; + +/** + * Encodes a string to base64 even with unicode characters + * @param value input string + * @returns base64 encoded string + */ +const toBase64 = value => { + try { + return bytesToBase64(new TextEncoder().encode(value)); + } catch (err) { + return ''; + } +}; + +export { toBase64 };