Skip to content

Commit d99342b

Browse files
Merge pull request #194 from Bangarraju-Microsoft/US-9072-UT
test: Frontend Unit test cases integrated
2 parents 0ec816e + 2d87635 commit d99342b

42 files changed

Lines changed: 9456 additions & 1629 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/sync-branches.yml

Lines changed: 0 additions & 44 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ __pycache__/
77
static
88
scripts/config.json
99
venv
10-
myenv
10+
myenv
11+
frontend/coverage

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ https://azure.microsoft.com/en-us/explore/global-infrastructure/products-by-regi
7272

7373
If you are using your own data, the next step is optional.
7474

75-
4. Optional - Follow steps in [Sample data guide](./scripts/SAMPLE_DATA.md) to ingest the sample Promissory Note PDFs into the search index.
75+
4. Follow steps in [Sample data guide](./scripts/SAMPLE_DATA.md) to ingest the sample Promissory Note PDFs into the search index.
7676

7777
If you want to enable authentication, you will need to add an identity provider.
7878

frontend/__mocks__/dompurify.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const DOMPurify = {
2+
sanitize: jest.fn((input: string) => input), // Mock implementation that returns the input
3+
};
4+
5+
export default DOMPurify; // Use default export

frontend/__mocks__/fileMock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// __mocks__/fileMock.ts
2+
const fileMock = 'test-file-stub';
3+
4+
export default fileMock;

frontend/__mocks__/mockAPIData.ts

Lines changed: 164 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// __mocks__/react-markdown.tsx
2+
3+
import React from 'react';
4+
5+
// Mock implementation of react-markdown
6+
const mockNode = {
7+
children: [{ value: 'console.log("Test Code");' }]
8+
};
9+
const mockProps = { className: 'language-javascript' };
10+
11+
const ReactMarkdown: React.FC<{ children: React.ReactNode , components: any }> = ({ children,components }) => {
12+
return <div data-testid="reactMockDown">
13+
{/* {components && components.code({ node: mockNode, ...mockProps })} */}
14+
{children}</div>; // Simply render the children
15+
};
16+
17+
export default ReactMarkdown;

frontend/jest.config.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,55 @@ import type { Config } from '@jest/types'
22

33
const config: Config.InitialOptions = {
44
verbose: true,
5+
6+
preset: 'ts-jest',
7+
//testEnvironment: 'jsdom', // For React DOM testing
8+
testEnvironment: 'jest-environment-jsdom',
9+
testEnvironmentOptions: {
10+
customExportConditions: ['']
11+
},
12+
moduleNameMapper: {
13+
'\\.(css|less|scss)$': 'identity-obj-proxy', // For mocking static file imports
14+
'^react-markdown$': '<rootDir>/__mocks__/react-markdown.tsx',
15+
'^dompurify$': '<rootDir>/__mocks__/dompurify.js', // Point to the mock
16+
'\\.(jpg|jpeg|png|gif|svg)$': '<rootDir>/__mocks__/fileMock.ts',
17+
18+
},
19+
setupFilesAfterEnv: ['<rootDir>/src/test/setupTests.ts'], // For setting up testing environment like jest-dom
520
transform: {
6-
'^.+\\.tsx?$': 'ts-jest'
21+
'^.+\\.ts(x)?$': 'ts-jest', // For TypeScript files
22+
'^.+\\.js$': 'babel-jest', // For JavaScript files if you have Babel
723
},
8-
setupFilesAfterEnv: ['<rootDir>/polyfills.js']
24+
25+
setupFiles: ['<rootDir>/jest.polyfills.js'],
26+
collectCoverage: true,
27+
//collectCoverageFrom: ['src/**/*.{ts,tsx}'], // Adjust the path as needed
28+
//coverageReporters: ['json', 'lcov', 'text', 'clover'],
29+
30+
collectCoverageFrom: ['src/**/*.{ts,tsx,js,jsx}'],
31+
coverageThreshold: {
32+
global: {
33+
branches: 80,
34+
functions: 80,
35+
lines: 80,
36+
statements: 80,
37+
},
38+
},
39+
40+
coveragePathIgnorePatterns: [
41+
'<rootDir>/node_modules/', // Ignore node_modules
42+
'<rootDir>/__mocks__/', // Ignore mocks
43+
'<rootDir>/src/state/',
44+
'<rootDir>/src/api/',
45+
'<rootDir>/src/mocks/',
46+
//'<rootDir>/src/test/',
47+
'<rootDir>/src/components/QuestionInput/index.ts',
48+
'<rootDir>/src/components/Answer/index.ts',
49+
'<rootDir>/src/pages/NoPage.tsx',
50+
'<rootDir>/src/index.tsx',
51+
'<rootDir>/src/vite-env.d.ts',
52+
'<rootDir>/src/helpers/'
53+
],
954
}
1055

1156
export default config

frontend/jest.polyfills.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @note The block below contains polyfills for Node.js globals
3+
* required for Jest to function when running JSDOM tests.
4+
* These HAVE to be require's and HAVE to be in this exact
5+
* order, since "undici" depends on the "TextEncoder" global API.
6+
*
7+
* Consider migrating to a more modern test runner if
8+
* you don't want to deal with this.
9+
*/
10+
11+
const { TextDecoder, TextEncoder } = require('node:util')
12+
13+
Object.defineProperties(globalThis, {
14+
TextDecoder: { value: TextDecoder },
15+
TextEncoder: { value: TextEncoder },
16+
})
17+
18+
const { Blob } = require('node:buffer')
19+
const { fetch, Headers, FormData, Request, Response } = require('undici')
20+
21+
Object.defineProperties(globalThis, {
22+
fetch: { value: fetch, writable: true },
23+
Blob: { value: Blob },
24+
Headers: { value: Headers },
25+
FormData: { value: FormData },
26+
Request: { value: Request },
27+
Response: { value: Response },
28+
})

0 commit comments

Comments
 (0)