Skip to content

Commit

Permalink
tweaking serialize replacer to handle arrays correctly, adding more d…
Browse files Browse the repository at this point in the history
…eterminative row addition check to test
  • Loading branch information
jvigliotta committed Nov 26, 2024
1 parent 20538eb commit 172b902
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,36 @@ test.describe('Display Layout', () => {
});

// Verify filtering is working correctly
// Wait for some data to populate
// eslint-disable-next-line playwright/no-wait-for-timeout
await page.waitForTimeout(2000); // Wait for a few state changes

// Create a promise that resolves when we've seen enough new rows added
const rowsMutationPromise = page.evaluate(() => {
return new Promise((resolve) => {
const targetNode = document.querySelector('[aria-label="Table Filter Off Value Frame"]');
const config = { childList: true, subtree: true };
let changeCount = 0;
const requiredChanges = 20; // Number of changes to wait for

const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
// Count added or removed nodes
changeCount += mutation.addedNodes.length + mutation.removedNodes.length;
}
});

// Check if the required number of changes has been met
if (changeCount >= requiredChanges) {
console.log(`Detected ${requiredChanges} changes in the table.`);
observer.disconnect(); // Disconnect observer after the required changes
resolve();
}
});

observer.observe(targetNode, config);
});
});

await rowsMutationPromise;

// Check ON table doesn't have any OFF values
await expect(tableFilterOn.locator('td[title="OFF"]')).toHaveCount(0);
Expand Down
44 changes: 20 additions & 24 deletions src/api/telemetry/TelemetryAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,38 +270,31 @@ export default class TelemetryAPI {
* - Primitives: Returned as-is
*/
sanitizeForSerialization(key, value) {
// Handle non-object types directly, except functions
if (typeof value !== 'object' || value === null) {
// Handle null and primitives directly
if (value === null || typeof value !== 'object') {
return value;
}

// Replace functions and other non-serializable values with their key names
if (
typeof value === 'function' ||
(value && typeof value === 'object' && !(Object.getPrototypeOf(value) === Object.prototype))
) {
return `@${key}`;
}

// Handle arrays by returning a sanitized array
// Handle arrays
if (Array.isArray(value)) {
return value.map((item, index) => this.sanitizeForSerialization(String(index), item));

Check warning on line 280 in src/api/telemetry/TelemetryAPI.js

View check run for this annotation

Codecov / codecov/patch

src/api/telemetry/TelemetryAPI.js#L280

Added line #L280 was not covered by tests
}

// Only process plain objects
if (Object.getPrototypeOf(value) === Object.prototype) {
const sortedObject = {};
const keys = Object.keys(value).sort();
for (const objectKey of keys) {
const itemValue = value[objectKey];
const sanitizedValue = this.sanitizeForSerialization(objectKey, itemValue);
sortedObject[objectKey] = sanitizedValue;
}
return sortedObject;
// Replace functions and non-plain objects with their key names
if (typeof value === 'function' || Object.getPrototypeOf(value) !== Object.prototype) {
return `@${key}`;
}

// Handle plain objects
const sortedObject = {};
const keys = Object.keys(value).sort();
for (const objectKey of keys) {
const itemValue = value[objectKey];
const sanitizedValue = this.sanitizeForSerialization(objectKey, itemValue);
sortedObject[objectKey] = sanitizedValue;
}

// If it's not a plain object, return the key name
return `@${key}`;
return sortedObject;
}

/**
Expand All @@ -318,7 +311,10 @@ export default class TelemetryAPI {
* @returns {number} A positive integer hash of the options object
*/
#hashOptions(options) {
const sanitizedOptionsString = JSON.stringify(options, this.sanitizeForSerialization);
const sanitizedOptionsString = JSON.stringify(
options,
this.sanitizeForSerialization.bind(this)
);

let hash = 0;
const prime = 31;
Expand Down

0 comments on commit 172b902

Please sign in to comment.