mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(account settings): activate imported account
This commit is contained in:
@@ -350,6 +350,40 @@ describe('AccountSettings', () => {
|
||||
expect(getLocationText()).toBe('/subs/settings#account-settings');
|
||||
});
|
||||
|
||||
it('activates the resolved account name when an imported account name already exists', async () => {
|
||||
hookMocks.useAccounts.mockReturnValue({
|
||||
accounts: [
|
||||
{ id: 'test-id', name: 'Account 1', author: { shortAddress: '0x1...3' } },
|
||||
{ id: 'existing-imported-id', name: 'Imported', author: { shortAddress: '0x9...9' } },
|
||||
],
|
||||
});
|
||||
fileReaderState.result = JSON.stringify({
|
||||
account: {
|
||||
id: 'imported-id',
|
||||
name: 'Imported',
|
||||
author: { address: '0xabc' },
|
||||
},
|
||||
});
|
||||
hookMocks.importAccount.mockResolvedValue(undefined);
|
||||
hookMocks.setActiveAccount.mockResolvedValue(undefined);
|
||||
|
||||
render();
|
||||
|
||||
await act(async () => {
|
||||
getButtonByText('import_account_backup').click();
|
||||
});
|
||||
|
||||
const file = new File(['{}'], 'account.json', { type: 'application/json' });
|
||||
await act(async () => {
|
||||
createdInput?.onchange?.({ target: { files: [file] } } as unknown as Event);
|
||||
await Promise.resolve();
|
||||
});
|
||||
await flushMicrotasks();
|
||||
|
||||
expect(hookMocks.importAccount).toHaveBeenCalledOnce();
|
||||
expect(hookMocks.setActiveAccount).toHaveBeenCalledWith('Imported 2');
|
||||
});
|
||||
|
||||
it('surfaces import errors without navigating or reloading', async () => {
|
||||
fileReaderState.result = JSON.stringify({
|
||||
account: {
|
||||
|
||||
@@ -57,6 +57,13 @@ const getSafeAccountBackupFileName = (accountName: string | undefined): string =
|
||||
return `${safeName}.json`;
|
||||
};
|
||||
|
||||
const getImportedAccountActiveName = (importedAccountName: string | undefined, accounts: Array<{ name?: string }>): string | undefined => {
|
||||
if (!importedAccountName) {
|
||||
return undefined;
|
||||
}
|
||||
return accounts.some((account) => account?.name === importedAccountName) ? `${importedAccountName} 2` : importedAccountName;
|
||||
};
|
||||
|
||||
// Inner component keyed by account id so state resets when user switches account
|
||||
const AccountSettingsEditor = ({
|
||||
account,
|
||||
@@ -154,14 +161,15 @@ const AccountSettingsEditor = ({
|
||||
}
|
||||
|
||||
const modifiedAccountJson = JSON.stringify(accountData);
|
||||
const importedAccountActiveName = getImportedAccountActiveName(accountData.account?.name, accounts);
|
||||
const result = await withErrorHandling(
|
||||
async () => {
|
||||
await importAccount(modifiedAccountJson);
|
||||
if (accountData.account?.author?.address) {
|
||||
rememberImportedAccountAddress(accountData.account.author.address);
|
||||
}
|
||||
if (accountData.account?.name) {
|
||||
await setActiveAccount(accountData.account.name);
|
||||
if (importedAccountActiveName) {
|
||||
await setActiveAccount(importedAccountActiveName);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user