Skip to content

Commit

Permalink
Fix delete button logic (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosABVA authored Jan 4, 2025
1 parent bce319e commit 7659576
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
66 changes: 64 additions & 2 deletions src/__tests__/components/pages/account/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Header', () => {
beforeEach(() => {
jest.spyOn(Split, 'create').mockReturnValue({ guid: 'createdSplit' } as Split);
jest.spyOn(apiHook, 'useAccount').mockReturnValue({ data: undefined } as UseQueryResult<Account>);
jest.spyOn(apiHook, 'useSplitsPagination').mockReturnValue({ data: undefined } as UseQueryResult<Split[]>);
jest.spyOn(apiHook, 'useSplitsPagination').mockReturnValue({ data: [] as Split[] } as UseQueryResult<Split[]>);
});

afterEach(() => {
Expand All @@ -59,6 +59,7 @@ describe('Header', () => {
commodity: {
mnemonic: 'EUR',
},
childrenIds: [] as String[],
} as Account;

jest.spyOn(apiHook, 'useAccount').mockReturnValueOnce({
Expand Down Expand Up @@ -101,7 +102,7 @@ describe('Header', () => {
expect.objectContaining({
className: 'btn btn-danger',
'data-tooltip-id': 'delete-help',
disabled: true,
disabled: false,
id: 'delete-account',
modalTitle: 'Confirm you want to remove this account',
}),
Expand Down Expand Up @@ -134,6 +135,7 @@ describe('Header', () => {
commodity: {
mnemonic: 'EUR',
},
childrenIds: [] as String[]
} as Account;

jest.spyOn(apiHook, 'useAccount').mockReturnValueOnce({
Expand All @@ -143,4 +145,64 @@ describe('Header', () => {
const { container } = render(<Header account={account} />);
expect(container).toMatchSnapshot();
});

it('delete is disabled if contains children', async () => {
const account = {
guid: 'guid',
path: 'Assets:account',
name: 'account',
type: 'TYPE',
parentId: 'parent',
commodity: {
mnemonic: 'EUR',
},
childrenIds: [
"child_guid"
]
} as Account;

render(<Header account={account} />);

expect(FormButton).toHaveBeenCalledWith(
expect.objectContaining({
className: 'btn btn-danger',
'data-tooltip-id': 'delete-help',
disabled: true,
id: 'delete-account',
modalTitle: 'Confirm you want to remove this account',
}),
undefined,
);
});

it('delete is disabled if contains splits', async () => {
const account = {
guid: 'guid',
path: 'Assets:account',
name: 'account',
type: 'TYPE',
parentId: 'parent',
commodity: {
mnemonic: 'EUR',
},
childrenIds: [] as String[]
} as Account;

jest.spyOn(apiHook, 'useSplitsPagination').mockReturnValueOnce({
data: { length: 1 } as Split[],
} as UseQueryResult<Split[]>);

render(<Header account={account} />);

expect(FormButton).toHaveBeenCalledWith(
expect.objectContaining({
className: 'btn btn-danger',
'data-tooltip-id': 'delete-help',
disabled: true,
id: 'delete-account',
modalTitle: 'Confirm you want to remove this account',
}),
undefined,
);
});
});
6 changes: 3 additions & 3 deletions src/components/pages/account/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function Header({
const router = useRouter();

const latestDate = splits?.[0]?.transaction?.date;
const deletable = splits?.length === 0;
const deletable = splits?.length === 0 && account.childrenIds?.length === 0;

let title: string | React.JSX.Element = account.path;
if (account.path.lastIndexOf(':') > 0) {
Expand Down Expand Up @@ -100,10 +100,10 @@ export default function Header({
id="delete-help"
>
<p>
Accounts that contain transactions can&apos;t be deleted.
Accounts that contain transactions or children can&apos;t be deleted.
</p>
<p>
Move the transactions to another account first.
Move the transactions or children to another account first.
</p>
</Tooltip>
)
Expand Down

0 comments on commit 7659576

Please sign in to comment.