Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client Advisor | Frontend test cases added #107

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
49 changes: 49 additions & 0 deletions .github/workflows/test_client_advisor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Unit Tests - Client Advisor

on:
push:
branches: [main, dev]
# Trigger on changes in these specific paths
paths:
- 'ClientAdvisor/**'
pull_request:
branches: [main, dev]
types:
- opened
- ready_for_review
- reopened
- synchronize
paths:
- 'ClientAdvisor/**'

jobs:
test_client_advisor:

name: Client Advisor Tests
runs-on: ubuntu-latest
# The if condition ensures that this job only runs if changes are in the ClientAdvisor folder

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
- name: Install Frontend Dependencies
run: |
cd ClientAdvisor/App/frontend
npm install
- name: Run Frontend Tests with Coverage
run: |
cd ClientAdvisor/App/frontend
npm run test -- --coverage
- uses: actions/upload-artifact@v4
with:
name: client-advisor-frontend-coverage
path: |
ClientAdvisor/App/frontend/coverage/
ClientAdvisor/App/frontend/coverage/lcov-report/
5 changes: 5 additions & 0 deletions ClientAdvisor/App/frontend/__mocks__/dompurify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const DOMPurify = {
sanitize: jest.fn((input: string) => input), // Mock implementation that returns the input
};

export default DOMPurify; // Use default export
4 changes: 4 additions & 0 deletions ClientAdvisor/App/frontend/__mocks__/fileMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// __mocks__/fileMock.ts
const fileMock = 'test-file-stub';

export default fileMock;
164 changes: 164 additions & 0 deletions ClientAdvisor/App/frontend/__mocks__/mockAPIData.ts

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions ClientAdvisor/App/frontend/__mocks__/react-markdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// __mocks__/react-markdown.tsx

import React from 'react';

// Mock implementation of react-markdown
const mockNode = {
children: [{ value: 'console.log("Test Code");' }]
};
const mockProps = { className: 'language-javascript' };

const ReactMarkdown: React.FC<{ children: React.ReactNode , components: any }> = ({ children,components }) => {
return <div data-testid="reactMockDown">
{components && components.code({ node: mockNode, ...mockProps })}
{children}</div>; // Simply render the children
};

export default ReactMarkdown;
41 changes: 39 additions & 2 deletions ClientAdvisor/App/frontend/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,47 @@ import type { Config } from '@jest/types'

const config: Config.InitialOptions = {
verbose: true,

preset: 'ts-jest',
//testEnvironment: 'jsdom', // For React DOM testing
testEnvironment: 'jest-environment-jsdom',
testEnvironmentOptions: {
customExportConditions: ['']
},
moduleNameMapper: {
'\\.(css|less|scss)$': 'identity-obj-proxy', // For mocking static file imports
'^react-markdown$': '<rootDir>/__mocks__/react-markdown.tsx',
'^dompurify$': '<rootDir>/__mocks__/dompurify.js', // Point to the mock
'\\.(jpg|jpeg|png|gif|svg)$': '<rootDir>/__mocks__/fileMock.ts',

},
setupFilesAfterEnv: ['<rootDir>/src/test/setupTests.ts'], // For setting up testing environment like jest-dom
transform: {
'^.+\\.tsx?$': 'ts-jest'
'^.+\\.ts(x)?$': 'ts-jest', // For TypeScript files
'^.+\\.js$': 'babel-jest', // For JavaScript files if you have Babel
},
setupFilesAfterEnv: ['<rootDir>/polyfills.js']

setupFiles: ['<rootDir>/jest.polyfills.js'],
collectCoverage: true,
//collectCoverageFrom: ['src/**/*.{ts,tsx}'], // Adjust the path as needed
//coverageReporters: ['json', 'lcov', 'text', 'clover'],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},

coveragePathIgnorePatterns: [
'<rootDir>/node_modules/', // Ignore node_modules
'<rootDir>/__mocks__/', // Ignore mocks
'<rootDir>/src/state/',
'<rootDir>/src/api/',
'<rootDir>/src/mocks/',
//'<rootDir>/src/test/',
],
}

export default config
28 changes: 28 additions & 0 deletions ClientAdvisor/App/frontend/jest.polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @note The block below contains polyfills for Node.js globals
* required for Jest to function when running JSDOM tests.
* These HAVE to be require's and HAVE to be in this exact
* order, since "undici" depends on the "TextEncoder" global API.
*
* Consider migrating to a more modern test runner if
* you don't want to deal with this.
*/

const { TextDecoder, TextEncoder } = require('node:util')

Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
})

const { Blob } = require('node:buffer')
const { fetch, Headers, FormData, Request, Response } = require('undici')

Object.defineProperties(globalThis, {
fetch: { value: fetch, writable: true },
Blob: { value: Blob },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
})
Loading
Loading