Skip to content

A local-first personal finance app

fix context menu showing on multi-account views (#8662)

Matt Fiddaman348aa53c7903
diff --git a/packages/desktop-client/src/components/sidebar/Account.test.tsx b/packages/desktop-client/src/components/sidebar/Account.test.tsx
new file mode 100644
index 0000000000..52dc5b3f1c
--- /dev/null
+++ b/packages/desktop-client/src/components/sidebar/Account.test.tsx
@@ -0,0 +1,121 @@
+import React from 'react';
+import type { ReactNode } from 'react';
+import { DndProvider } from 'react-dnd';
+import { HTML5Backend } from 'react-dnd-html5-backend';
+import { MemoryRouter } from 'react-router';
+
+import { generateAccount } from '@actual-app/core/mocks';
+import { initServer } from '@actual-app/core/platform/client/connection';
+import { act, fireEvent, render, screen } from '@testing-library/react';
+
+import { SpreadsheetProvider } from '#hooks/useSpreadsheet';
+import {
+ configureTestAppStore,
+ createTestQueryClient,
+ TestProviders,
+} from '#mocks';
+import * as bindings from '#spreadsheet/bindings';
+
+import { Account } from './Account';
+
+vi.mock(
+ '@actual-app/core/platform/client/connection',
+ () => import('#mocks/connection'),
+);
+
+// jsdom does not implement matchMedia, which the component uses for
+// touch-device detection
+window.matchMedia = (query: string): MediaQueryList => ({
+ matches: false,
+ media: query,
+ onchange: null,
+ addListener: vi.fn(),
+ removeListener: vi.fn(),
+ addEventListener: vi.fn(),
+ removeEventListener: vi.fn(),
+ dispatchEvent: vi.fn(() => false),
+});
+
+describe('sidebar Account context menu', () => {
+ let store: ReturnType<typeof configureTestAppStore>;
+
+ beforeEach(() => {
+ initServer({
+ query: async () => ({ data: [], dependencies: [] }),
+ 'get-cell': async () => ({ name: 'test-cell', value: 0 }),
+ });
+
+ store = configureTestAppStore({ queryClient: createTestQueryClient() });
+ });
+
+ async function renderRow(children: ReactNode) {
+ const result = render(
+ <TestProviders store={store}>
+ <SpreadsheetProvider>
+ <MemoryRouter>
+ <DndProvider backend={HTML5Backend}>{children}</DndProvider>
+ </MemoryRouter>
+ </SpreadsheetProvider>
+ </TestProviders>,
+ );
+ // Flush the async notes/balance queries kicked off on mount
+ await act(() => Promise.resolve());
+ return result;
+ }
+
+ function contextMenuItemNames() {
+ return store
+ .getState()
+ .contextMenu.items.map(item =>
+ typeof item === 'object' ? item.name : null,
+ );
+ }
+
+ it('does not open on the All accounts row', async () => {
+ await renderRow(
+ <Account
+ name="All accounts"
+ to="/accounts"
+ query={bindings.allAccountBalance()}
+ />,
+ );
+
+ fireEvent.contextMenu(screen.getByText('All accounts'));
+
+ expect(store.getState().contextMenu.isOpen).toBe(false);
+ expect(contextMenuItemNames()).toEqual([]);
+ });
+
+ it('does not open on the On budget row', async () => {
+ await renderRow(
+ <Account
+ name="On budget"
+ to="/accounts/onbudget"
+ query={bindings.onBudgetAccountBalance()}
+ />,
+ );
+
+ fireEvent.contextMenu(screen.getByText('On budget'));
+
+ expect(store.getState().contextMenu.isOpen).toBe(false);
+ expect(contextMenuItemNames()).toEqual([]);
+ });
+
+ it('opens rename/close on an account row', async () => {
+ const account = generateAccount('Bank of America');
+
+ await renderRow(
+ <Account
+ name={account.name}
+ account={account}
+ to={`/accounts/${account.id}`}
+ query={bindings.accountBalance(account.id)}
+ />,
+ );
+
+ fireEvent.contextMenu(screen.getByText('Bank of America'));
+
+ expect(store.getState().contextMenu.isOpen).toBe(true);
+ expect(contextMenuItemNames()).toEqual(['account-rename', 'account-close']);
+ });
+});
diff --git a/packages/desktop-client/src/components/sidebar/Account.tsx b/packages/desktop-client/src/components/sidebar/Account.tsx
index 7e20ea4dd4..768789f833 100644
--- a/packages/desktop-client/src/components/sidebar/Account.tsx
+++ b/packages/desktop-client/src/components/sidebar/Account.tsx
@@ -136,7 +136,7 @@ export function Account<FieldName extends SheetFields<'account'>>({
);
useContextMenu({
triggerRef,
- enabled: account && needsTooltip,
+ enabled: account != null && needsTooltip,
items: [
{
name: 'account-rename',
diff --git a/upcoming-release-notes/account-context-menu.md b/upcoming-release-notes/account-context-menu.md
new file mode 100644
index 0000000000..6d6b2f6d3c
--- /dev/null
+++ b/upcoming-release-notes/account-context-menu.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [matt-fidd]
+---
+
+Fix context menu showing on multi-account views