Skip to content
Open
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
1 change: 1 addition & 0 deletions dashboard/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const config = {

// Module name mapping for path aliases
moduleNameMapper: {
Comment thread
Brijesh619 marked this conversation as resolved.
'^sanitize-html$': '<rootDir>/src/__mocks__/sanitize-html.ts',
'^@/(.*)$': '<rootDir>/src/$1',
'^@components/(.*)$': '<rootDir>/src/components/$1',
'^@api/(.*)\.js$': '<rootDir>/src/api/$1.ts',
Expand Down
121 changes: 116 additions & 5 deletions dashboard/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"react-router-dom": "6.30.4",
"react-toastify": "10.0.5",
"recharts": "2.15.1",
"sanitize-html": "2.17.4"
"sanitize-html": "2.17.6"
},
"devDependencies": {
"@babel/preset-env": "7.28.5",
Expand Down
50 changes: 50 additions & 0 deletions dashboard/src/__mocks__/sanitize-html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

type SanitizeFn = (html: string, options?: Record<string, unknown>) => string;
let actualSanitizeModule: SanitizeFn | { default: SanitizeFn } | null | undefined;

const getSanitizeFn = (): SanitizeFn | null => {
if (actualSanitizeModule === undefined) {
try {
actualSanitizeModule = jest.requireActual('sanitize-html/index.js') as SanitizeFn | { default: SanitizeFn };
} catch (e) {
actualSanitizeModule = null;
}
}

if (typeof actualSanitizeModule === 'function') {
return actualSanitizeModule;
}
if (actualSanitizeModule && typeof actualSanitizeModule === 'object' && 'default' in actualSanitizeModule && typeof actualSanitizeModule.default === 'function') {
return actualSanitizeModule.default;
}

return null;
};

const sanitizeHtml = (html: string, options?: Record<string, unknown>) => {
const sanitizeFn = getSanitizeFn();

if (options && typeof sanitizeFn === 'function') {
return sanitizeFn(html, options);
}

const htmlStr = typeof html === 'string' ? html : String(html);
return htmlStr.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
};
export default sanitizeHtml;
43 changes: 25 additions & 18 deletions dashboard/src/setupTests.simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,37 @@
/** Simplified test setup file for Node 12 compatibility */

import '@testing-library/jest-dom';
import { TextEncoder, TextDecoder } from 'util';

export {};

// Polyfill TextEncoder and TextDecoder for React Router DOM in Jest
if (typeof global.TextEncoder === 'undefined') {
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder as unknown as typeof global.TextDecoder;
}


// Basic mocks that don't rely on newer JS features
(global as any).ResizeObserver = function() {
return {
observe: function() {},
unobserve: function() {},
disconnect: function() {}
};
};
Object.assign(global, {
ResizeObserver: class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
});

(global as any).IntersectionObserver = function() {
return {
observe: function() {},
unobserve: function() {},
disconnect: function() {},
takeRecords: function() { return []; },
root: null,
rootMargin: '',
thresholds: []
};
};
Object.assign(global, {
IntersectionObserver: class IntersectionObserver {
root = null;
rootMargin = '';
thresholds = [];
observe() {}
unobserve() {}
disconnect() {}
takeRecords() { return []; }
}
});

// Mock window.matchMedia (jsdom / browser tests only)
if (typeof window !== 'undefined') {
Expand Down
Loading
Loading