|
| 1 | +import { combineReducers, configureStore } from '@reduxjs/toolkit'; |
| 2 | +import { act, fireEvent, render, screen } from '@testing-library/react'; |
| 3 | +import { Provider } from 'react-redux'; |
| 4 | +import { MemoryRouter } from 'react-router-dom'; |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +import accountsReducer from '../../store/accountsSlice'; |
| 8 | +import OpenhumanLinkModal, { OPENHUMAN_LINK_EVENT } from '../OpenhumanLinkModal'; |
| 9 | + |
| 10 | +// Mock modules that require Tauri runtime |
| 11 | +vi.mock('@tauri-apps/api/core', () => ({ isTauri: vi.fn(() => false) })); |
| 12 | +vi.mock('../../lib/nativeNotifications/tauriBridge', () => ({ |
| 13 | + ensureNotificationPermission: vi.fn(), |
| 14 | + getNotificationPermissionState: vi.fn().mockResolvedValue('prompt'), |
| 15 | + showNativeNotification: vi.fn(), |
| 16 | +})); |
| 17 | +vi.mock('../../services/webviewAccountService', () => ({ |
| 18 | + isTauri: vi.fn(() => false), |
| 19 | + purgeWebviewAccount: vi.fn().mockResolvedValue(undefined), |
| 20 | +})); |
| 21 | + |
| 22 | +const mockNavigate = vi.fn(); |
| 23 | +vi.mock('react-router-dom', async () => { |
| 24 | + const actual = await vi.importActual('react-router-dom'); |
| 25 | + return { ...actual, useNavigate: () => mockNavigate }; |
| 26 | +}); |
| 27 | + |
| 28 | +function createStore() { |
| 29 | + return configureStore({ |
| 30 | + reducer: combineReducers({ |
| 31 | + accounts: accountsReducer, |
| 32 | + // Stubs for selectors that may be read elsewhere |
| 33 | + channelConnections: () => ({}), |
| 34 | + }), |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function renderModal(store = createStore()) { |
| 39 | + return { |
| 40 | + store, |
| 41 | + ...render( |
| 42 | + <Provider store={store}> |
| 43 | + <MemoryRouter> |
| 44 | + <OpenhumanLinkModal /> |
| 45 | + </MemoryRouter> |
| 46 | + </Provider> |
| 47 | + ), |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +function openAccountsModal() { |
| 52 | + act(() => { |
| 53 | + window.dispatchEvent( |
| 54 | + new CustomEvent(OPENHUMAN_LINK_EVENT, { detail: { path: 'accounts/setup' } }) |
| 55 | + ); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +describe('OpenhumanLinkModal accounts setup', () => { |
| 60 | + beforeEach(() => { |
| 61 | + vi.clearAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('renders provider toggles when accounts/setup path is opened', () => { |
| 65 | + renderModal(); |
| 66 | + openAccountsModal(); |
| 67 | + |
| 68 | + expect(screen.getByLabelText('Connect WhatsApp Web')).toBeInTheDocument(); |
| 69 | + expect(screen.getByLabelText('Connect Telegram Web')).toBeInTheDocument(); |
| 70 | + expect(screen.getByLabelText('Connect Slack')).toBeInTheDocument(); |
| 71 | + expect(screen.getByLabelText('Connect Discord')).toBeInTheDocument(); |
| 72 | + expect(screen.getByLabelText('Connect LinkedIn')).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('toggle ON adds account to Redux store', () => { |
| 76 | + const { store } = renderModal(); |
| 77 | + openAccountsModal(); |
| 78 | + |
| 79 | + fireEvent.click(screen.getByLabelText('Connect Telegram Web')); |
| 80 | + |
| 81 | + const state = store.getState().accounts; |
| 82 | + const telegramAccount = Object.values(state.accounts).find(a => a.provider === 'telegram'); |
| 83 | + expect(telegramAccount).toBeDefined(); |
| 84 | + expect(telegramAccount!.status).toBe('pending'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('toggle OFF removes account from Redux store', () => { |
| 88 | + const { store } = renderModal(); |
| 89 | + openAccountsModal(); |
| 90 | + |
| 91 | + // Toggle ON |
| 92 | + fireEvent.click(screen.getByLabelText('Connect Telegram Web')); |
| 93 | + expect(Object.values(store.getState().accounts.accounts)).toHaveLength(1); |
| 94 | + |
| 95 | + // Toggle OFF |
| 96 | + fireEvent.click(screen.getByLabelText('Disconnect Telegram Web')); |
| 97 | + expect(Object.values(store.getState().accounts.accounts)).toHaveLength(0); |
| 98 | + }); |
| 99 | + |
| 100 | + it('Done button navigates to /chat and sets first new account as active', () => { |
| 101 | + const { store } = renderModal(); |
| 102 | + openAccountsModal(); |
| 103 | + |
| 104 | + // Toggle two providers ON |
| 105 | + fireEvent.click(screen.getByLabelText('Connect Telegram Web')); |
| 106 | + fireEvent.click(screen.getByLabelText('Connect Slack')); |
| 107 | + |
| 108 | + const accountIds = store.getState().accounts.order; |
| 109 | + expect(accountIds).toHaveLength(2); |
| 110 | + |
| 111 | + // Click the CTA (dynamic label: "Continue with Telegram Web sign-in") |
| 112 | + fireEvent.click(screen.getByRole('button', { name: /Continue with Telegram Web sign-in/ })); |
| 113 | + |
| 114 | + expect(store.getState().accounts.activeAccountId).toBe(accountIds[0]); |
| 115 | + expect(mockNavigate).toHaveBeenCalledWith('/chat'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('Skip button closes modal without navigating', () => { |
| 119 | + renderModal(); |
| 120 | + openAccountsModal(); |
| 121 | + |
| 122 | + fireEvent.click(screen.getByLabelText('Connect Telegram Web')); |
| 123 | + fireEvent.click(screen.getByRole('button', { name: 'Skip for now' })); |
| 124 | + |
| 125 | + expect(mockNavigate).not.toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('Done without any new toggles does not navigate', () => { |
| 129 | + renderModal(); |
| 130 | + openAccountsModal(); |
| 131 | + |
| 132 | + fireEvent.click(screen.getByRole('button', { name: 'Done' })); |
| 133 | + expect(mockNavigate).not.toHaveBeenCalled(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('shows dynamic CTA label when a provider is toggled on', () => { |
| 137 | + renderModal(); |
| 138 | + openAccountsModal(); |
| 139 | + |
| 140 | + // Before toggling, button says "Done" |
| 141 | + expect(screen.getByRole('button', { name: 'Done' })).toBeInTheDocument(); |
| 142 | + |
| 143 | + // Toggle Discord on |
| 144 | + fireEvent.click(screen.getByLabelText('Connect Discord')); |
| 145 | + |
| 146 | + // CTA should now reference Discord |
| 147 | + expect( |
| 148 | + screen.getByRole('button', { name: /Continue with Discord sign-in/ }) |
| 149 | + ).toBeInTheDocument(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('shows status indicator for existing accounts with a status', () => { |
| 153 | + const store = createStore(); |
| 154 | + // Pre-populate an account with 'open' status |
| 155 | + store.dispatch({ |
| 156 | + type: 'accounts/addAccount', |
| 157 | + payload: { |
| 158 | + id: 'test-acct-1', |
| 159 | + provider: 'telegram', |
| 160 | + label: 'Telegram', |
| 161 | + createdAt: new Date().toISOString(), |
| 162 | + status: 'open', |
| 163 | + }, |
| 164 | + }); |
| 165 | + |
| 166 | + render( |
| 167 | + <Provider store={store}> |
| 168 | + <MemoryRouter> |
| 169 | + <OpenhumanLinkModal /> |
| 170 | + </MemoryRouter> |
| 171 | + </Provider> |
| 172 | + ); |
| 173 | + openAccountsModal(); |
| 174 | + |
| 175 | + expect(screen.getByText('Connected')).toBeInTheDocument(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('shows "Needs sign-in" for accounts with pending status', () => { |
| 179 | + const store = createStore(); |
| 180 | + store.dispatch({ |
| 181 | + type: 'accounts/addAccount', |
| 182 | + payload: { |
| 183 | + id: 'test-acct-2', |
| 184 | + provider: 'slack', |
| 185 | + label: 'Slack', |
| 186 | + createdAt: new Date().toISOString(), |
| 187 | + status: 'pending', |
| 188 | + }, |
| 189 | + }); |
| 190 | + |
| 191 | + render( |
| 192 | + <Provider store={store}> |
| 193 | + <MemoryRouter> |
| 194 | + <OpenhumanLinkModal /> |
| 195 | + </MemoryRouter> |
| 196 | + </Provider> |
| 197 | + ); |
| 198 | + openAccountsModal(); |
| 199 | + |
| 200 | + expect(screen.getByText('Needs sign-in')).toBeInTheDocument(); |
| 201 | + }); |
| 202 | +}); |
0 commit comments