From 96e5f1737fb7a84a0c2757e8cecd2841d44b8d61 Mon Sep 17 00:00:00 2001
From: Sai Kumar Battinoju <88789928+saikumarrs@users.noreply.github.com>
Date: Fri, 3 May 2024 13:17:21 +0530
Subject: [PATCH] chore: update sanity suite (#1705)
* chore: update sanity suite
* fix: update source config expected responses as per the latest updates
* chore: update README.md
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
* chore: update README.md to avoid ambiguity
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
* chore: update README.md
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
* fix: implement ai bot review comments
* chore: fix code formatting in readme file
---------
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
---
README.md | 83 +-
.../__fixtures__/sourceConfig1.json | 897 +-----------------
.../__fixtures__/sourceConfigDMT1.json | 408 +-------
.../__fixtures__/sourceConfigDev1.json | 550 +----------
.../sourceConfigIgnoredProperties.js | 42 +-
.../src/testBook/ResultAssertions.js | 10 +-
.../sanity-suite/src/testBook/TestBook.js | 51 +-
packages/sanity-suite/src/testBook/string.js | 27 +
8 files changed, 176 insertions(+), 1892 deletions(-)
create mode 100644 packages/sanity-suite/src/testBook/string.js
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 };