From 064a36fcbb4e2ad9f6a2c348d956f85855da761e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 24 May 2025 20:57:40 +0000 Subject: [PATCH] feat: Add Jest testing for getState util --- assets/jest.config.js | 14 +++++ .../SystemSignatures/helpers/getState.test.ts | 52 +++++++++++++++++++ assets/package.json | 6 ++- 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 assets/jest.config.js create mode 100644 assets/js/hooks/Mapper/components/mapInterface/widgets/SystemSignatures/helpers/getState.test.ts diff --git a/assets/jest.config.js b/assets/jest.config.js new file mode 100644 index 00000000..6c419da0 --- /dev/null +++ b/assets/jest.config.js @@ -0,0 +1,14 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'jsdom', + roots: [''], + moduleDirectories: ['node_modules', 'js'], + moduleNameMapper: { + '^@/(.*)$': '/js/$1', + '\.scss$': 'identity-obj-proxy', // Mock SCSS files + }, + transform: { + '^.+\.(ts|tsx)$': 'ts-jest', + '^.+\.(js|jsx)$': 'babel-jest', // Add babel-jest for JS/JSX files if needed + }, +}; diff --git a/assets/js/hooks/Mapper/components/mapInterface/widgets/SystemSignatures/helpers/getState.test.ts b/assets/js/hooks/Mapper/components/mapInterface/widgets/SystemSignatures/helpers/getState.test.ts new file mode 100644 index 00000000..94fcf398 --- /dev/null +++ b/assets/js/hooks/Mapper/components/mapInterface/widgets/SystemSignatures/helpers/getState.test.ts @@ -0,0 +1,52 @@ +import { getState } from './getState'; +import { UNKNOWN_SIGNATURE_NAME } from '@/hooks/Mapper/helpers'; +import { SignatureGroup, SystemSignature } from '@/hooks/Mapper/types'; + +describe('getState', () => { + const mockSignaturesMatch: string[] = []; // This parameter is not used in the function + + it('should return 0 if group is undefined', () => { + const newSig: SystemSignature = { id: '1', name: 'Test Sig', group: undefined } as SystemSignature; + expect(getState(mockSignaturesMatch, newSig)).toBe(0); + }); + + it('should return 0 if group is CosmicSignature', () => { + const newSig: SystemSignature = { id: '1', name: 'Test Sig', group: SignatureGroup.CosmicSignature } as SystemSignature; + expect(getState(mockSignaturesMatch, newSig)).toBe(0); + }); + + it('should return 1 if group is not CosmicSignature and name is undefined', () => { + const newSig: SystemSignature = { id: '1', name: undefined, group: SignatureGroup.Wormhole } as SystemSignature; + expect(getState(mockSignaturesMatch, newSig)).toBe(1); + }); + + it('should return 1 if group is not CosmicSignature and name is empty', () => { + const newSig: SystemSignature = { id: '1', name: '', group: SignatureGroup.Wormhole } as SystemSignature; + expect(getState(mockSignaturesMatch, newSig)).toBe(1); + }); + + it('should return 1 if group is not CosmicSignature and name is UNKNOWN_SIGNATURE_NAME', () => { + const newSig: SystemSignature = { id: '1', name: UNKNOWN_SIGNATURE_NAME, group: SignatureGroup.Wormhole } as SystemSignature; + expect(getState(mockSignaturesMatch, newSig)).toBe(1); + }); + + it('should return 2 if group is not CosmicSignature and name is a non-empty string', () => { + const newSig: SystemSignature = { id: '1', name: 'Custom Name', group: SignatureGroup.Wormhole } as SystemSignature; + expect(getState(mockSignaturesMatch, newSig)).toBe(2); + }); + + // According to the current implementation, state = -1 is unreachable + // because the conditions for 0, 1, and 2 cover all possibilities for the given inputs. + // If the logic of getState were to change to make -1 possible, a test case should be added here. + // For now, we can test a scenario that should lead to one of the valid states, + // for example, if group is something other than CosmicSignature and name is valid. + it('should handle other valid signature groups correctly, leading to state 2 with a valid name', () => { + const newSig: SystemSignature = { id: '1', name: 'Combat Site', group: SignatureGroup.CombatSite } as SystemSignature; + expect(getState(mockSignaturesMatch, newSig)).toBe(2); + }); + + it('should handle other valid signature groups correctly, leading to state 1 with an empty name', () => { + const newSig: SystemSignature = { id: '1', name: '', group: SignatureGroup.DataSite } as SystemSignature; + expect(getState(mockSignaturesMatch, newSig)).toBe(1); + }); +}); diff --git a/assets/package.json b/assets/package.json index 1c8af0ae..ad86556d 100644 --- a/assets/package.json +++ b/assets/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "vite build --emptyOutDir false", "watch": "vite build --watch --minify false --emptyOutDir false --clearScreen true --mode development", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "jest" }, "engines": { "node": ">= 18.0.0" @@ -50,6 +50,7 @@ "@tailwindcss/aspect-ratio": "^0.4.2", "@tailwindcss/forms": "^0.5.7", "@tailwindcss/typography": "^0.5.13", + "@types/jest": "^29.5.12", "@types/lodash.debounce": "^4.0.9", "@types/lodash.isequal": "^4.5.8", "@types/react": "^18.3.12", @@ -59,6 +60,7 @@ "@vitejs/plugin-react": "^4.3.3", "@vitejs/plugin-react-refresh": "^1.3.6", "autoprefixer": "^10.4.19", + "babel-jest": "^29.7.0", "child_process": "^1.0.2", "eslint": "^8.57.0", "eslint-config-prettier": "^9.1.0", @@ -67,6 +69,7 @@ "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-refresh": "^0.4.6", "heroicons": "^2.0.18", + "jest": "^29.7.0", "merge-options": "^3.0.4", "postcss": "^8.4.38", "postcss-cli": "^11.0.0", @@ -74,6 +77,7 @@ "prettier": "^3.2.5", "sass": "^1.77.2", "sass-loader": "^14.2.1", + "ts-jest": "^29.1.2", "typescript": "^5.2.2", "vite": "^5.0.5", "vite-plugin-cdn-import": "^1.0.1"