|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import React from 'react' |
| 3 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 4 | +import { renderHook } from '@testing-library/react' |
| 5 | +import { configureStore } from '@reduxjs/toolkit' |
| 6 | +import { |
| 7 | + InstitutionStatus, |
| 8 | + useInstitutionStatusMessage, |
| 9 | + useInstitutionStatus, |
| 10 | + getInstitutionStatus, |
| 11 | +} from '../institutionStatus' |
| 12 | +import * as institutionBlocks from '../institutionBlocks' |
| 13 | +import { Provider } from 'react-redux' |
| 14 | + |
| 15 | +// Mock dependencies |
| 16 | +vi.mock('../institutionBlocks', () => ({ |
| 17 | + institutionIsBlockedForCostReasons: vi.fn(), |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('src/utilities/Intl', () => ({ |
| 21 | + __: vi.fn((key: string, ...args: any[]) => { |
| 22 | + if (args.length > 0) { |
| 23 | + return key.replace( |
| 24 | + /%(\d+)/g, |
| 25 | + (match: string, num: string) => args[parseInt(num) - 1] || match, |
| 26 | + ) |
| 27 | + } |
| 28 | + return key |
| 29 | + }), |
| 30 | +})) |
| 31 | +// Mock store setup |
| 32 | +// Mock store setup |
| 33 | +const createMockStore = (unavailableInstitutions: any = []) => { |
| 34 | + return configureStore({ |
| 35 | + reducer: { |
| 36 | + experimentalFeatures: (state = { unavailableInstitutions }) => state, |
| 37 | + }, |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +const wrapper = ({ children, store }: { children: React.ReactNode; store: any }) => ( |
| 42 | + <Provider store={store}>{children}</Provider> |
| 43 | +) |
| 44 | + |
| 45 | +describe('institutionStatus', () => { |
| 46 | + beforeEach(() => { |
| 47 | + vi.clearAllMocks() |
| 48 | + }) |
| 49 | + |
| 50 | + describe('getInstitutionStatus', () => { |
| 51 | + it('returns OPERATIONAL when institution is null', () => { |
| 52 | + const result = getInstitutionStatus(null, []) |
| 53 | + expect(result).toBe(InstitutionStatus.OPERATIONAL) |
| 54 | + }) |
| 55 | + |
| 56 | + it('returns OPERATIONAL when unavailableInstitutions is not an array', () => { |
| 57 | + const institution = { guid: 'test-guid', name: 'Test Bank' } |
| 58 | + const result = getInstitutionStatus(institution, null as any) |
| 59 | + expect(result).toBe(InstitutionStatus.OPERATIONAL) |
| 60 | + }) |
| 61 | + |
| 62 | + it('returns CLIENT_BLOCKED_FOR_FEES when institution is blocked for cost reasons', () => { |
| 63 | + const institution = { guid: 'test-guid', name: 'Test Bank' } |
| 64 | + vi.mocked(institutionBlocks.institutionIsBlockedForCostReasons).mockReturnValue(true) |
| 65 | + |
| 66 | + const result = getInstitutionStatus(institution, []) |
| 67 | + expect(result).toBe(InstitutionStatus.CLIENT_BLOCKED_FOR_FEES) |
| 68 | + }) |
| 69 | + |
| 70 | + it('returns UNAVAILABLE when institution is in unavailableInstitutions by guid', () => { |
| 71 | + const institution = { guid: 'test-guid', name: 'Test Bank' } |
| 72 | + const unavailableInstitutions = [{ guid: 'test-guid', name: 'Other Bank' }] |
| 73 | + vi.mocked(institutionBlocks.institutionIsBlockedForCostReasons).mockReturnValue(false) |
| 74 | + |
| 75 | + const result = getInstitutionStatus(institution, unavailableInstitutions) |
| 76 | + expect(result).toBe(InstitutionStatus.UNAVAILABLE) |
| 77 | + }) |
| 78 | + |
| 79 | + it('returns UNAVAILABLE when institution is in unavailableInstitutions by name', () => { |
| 80 | + const institution = { guid: 'test-guid', name: 'Test Bank' } |
| 81 | + const unavailableInstitutions = [{ guid: 'other-guid', name: 'Test Bank' }] |
| 82 | + vi.mocked(institutionBlocks.institutionIsBlockedForCostReasons).mockReturnValue(false) |
| 83 | + |
| 84 | + const result = getInstitutionStatus(institution, unavailableInstitutions) |
| 85 | + expect(result).toBe(InstitutionStatus.UNAVAILABLE) |
| 86 | + }) |
| 87 | + |
| 88 | + it('returns OPERATIONAL when institution is not blocked or unavailable', () => { |
| 89 | + const institution = { guid: 'test-guid', name: 'Test Bank' } |
| 90 | + const unavailableInstitutions = [{ guid: 'other-guid', name: 'Other Bank' }] |
| 91 | + vi.mocked(institutionBlocks.institutionIsBlockedForCostReasons).mockReturnValue(false) |
| 92 | + |
| 93 | + const result = getInstitutionStatus(institution, unavailableInstitutions) |
| 94 | + expect(result).toBe(InstitutionStatus.OPERATIONAL) |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + describe('useInstitutionStatus', () => { |
| 99 | + it('returns institution status using redux state', () => { |
| 100 | + const institution = { guid: 'test-guid', name: 'Test Bank' } |
| 101 | + const unavailableInstitutions = [{ guid: 'test-guid', name: 'Test Bank' }] |
| 102 | + const store = createMockStore(unavailableInstitutions) |
| 103 | + |
| 104 | + const { result } = renderHook(() => useInstitutionStatus(institution), { |
| 105 | + wrapper: ({ children }) => wrapper({ children, store }), |
| 106 | + }) |
| 107 | + |
| 108 | + expect(result.current).toBe(InstitutionStatus.UNAVAILABLE) |
| 109 | + }) |
| 110 | + |
| 111 | + it('handles null institution', () => { |
| 112 | + const store = createMockStore([]) |
| 113 | + |
| 114 | + const { result } = renderHook(() => useInstitutionStatus(null), { |
| 115 | + wrapper: ({ children }) => wrapper({ children, store }), |
| 116 | + }) |
| 117 | + |
| 118 | + expect(result.current).toBe(InstitutionStatus.OPERATIONAL) |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + describe('useInstitutionStatusMessage', () => { |
| 123 | + it('throws error when institution is missing required fields', () => { |
| 124 | + const store = createMockStore([]) |
| 125 | + const institution = { guid: '', name: '' } |
| 126 | + |
| 127 | + expect(() => { |
| 128 | + renderHook(() => useInstitutionStatusMessage(institution), { |
| 129 | + wrapper: ({ children }) => wrapper({ children, store }), |
| 130 | + }) |
| 131 | + }).toThrow('Selected institution is not defined or missing name and guid') |
| 132 | + }) |
| 133 | + |
| 134 | + it('throws error when unavailableInstitutions is not an array', () => { |
| 135 | + const store = createMockStore(null) |
| 136 | + const institution = { guid: 'test-guid', name: 'Test Bank' } |
| 137 | + |
| 138 | + expect(() => { |
| 139 | + renderHook(() => useInstitutionStatusMessage(institution), { |
| 140 | + wrapper: ({ children }) => wrapper({ children, store }), |
| 141 | + }) |
| 142 | + }).toThrow('Experimental feature unavailableInstitutions is not defined or not an array') |
| 143 | + }) |
| 144 | + |
| 145 | + it('returns fee-related message for CLIENT_BLOCKED_FOR_FEES status', () => { |
| 146 | + const institution = { guid: 'test-guid', name: 'Test Bank' } |
| 147 | + const store = createMockStore([]) |
| 148 | + vi.mocked(institutionBlocks.institutionIsBlockedForCostReasons).mockReturnValue(true) |
| 149 | + |
| 150 | + const { result } = renderHook(() => useInstitutionStatusMessage(institution), { |
| 151 | + wrapper: ({ children }) => wrapper({ children, store }), |
| 152 | + }) |
| 153 | + |
| 154 | + expect(result.current).toEqual({ |
| 155 | + title: 'Free Test Bank Connections Are No Longer Available', |
| 156 | + body: 'Test Bank now charges a fee for us to access your account data. To avoid passing that cost on to you, we no longer support Test Bank connections.', |
| 157 | + }) |
| 158 | + }) |
| 159 | + |
| 160 | + it('returns unavailable message for UNAVAILABLE status', () => { |
| 161 | + const institution = { guid: 'test-guid', name: 'Test Bank' } |
| 162 | + const unavailableInstitutions = [{ guid: 'test-guid', name: 'Test Bank' }] |
| 163 | + const store = createMockStore(unavailableInstitutions) |
| 164 | + vi.mocked(institutionBlocks.institutionIsBlockedForCostReasons).mockReturnValue(false) |
| 165 | + |
| 166 | + const { result } = renderHook(() => useInstitutionStatusMessage(institution), { |
| 167 | + wrapper: ({ children }) => wrapper({ children, store }), |
| 168 | + }) |
| 169 | + |
| 170 | + expect(result.current).toEqual({ |
| 171 | + title: 'Connection not supported by Test Bank', |
| 172 | + body: "Test Bank currently limits how your data can be shared. We'll enable this connection once Test Bank opens access.", |
| 173 | + }) |
| 174 | + }) |
| 175 | + |
| 176 | + it('returns empty message for OPERATIONAL status', () => { |
| 177 | + const institution = { guid: 'test-guid', name: 'Test Bank' } |
| 178 | + const store = createMockStore([]) |
| 179 | + vi.mocked(institutionBlocks.institutionIsBlockedForCostReasons).mockReturnValue(false) |
| 180 | + |
| 181 | + const { result } = renderHook(() => useInstitutionStatusMessage(institution), { |
| 182 | + wrapper: ({ children }) => wrapper({ children, store }), |
| 183 | + }) |
| 184 | + |
| 185 | + expect(result.current).toEqual({ |
| 186 | + title: '', |
| 187 | + body: '', |
| 188 | + }) |
| 189 | + }) |
| 190 | + }) |
| 191 | +}) |
0 commit comments