@@ -1799,29 +1799,29 @@ describe('AccountsController', () => {
17991799 } ) ;
18001800
18011801 describe ( '#getNextAccountNumber' , ( ) => {
1802- it ( 'should return the next account number' , async ( ) => {
1803- const messenger = buildMessenger ( ) ;
1804- mockUUID
1805- . mockReturnValueOnce ( 'mock-id' ) // call to check if its a new account
1806- . mockReturnValueOnce ( 'mock-id2' ) // call to check if its a new account
1807- . mockReturnValueOnce ( 'mock-id3' ) // call to check if its a new account
1808- . mockReturnValueOnce ( 'mock-id2' ) // call to add account
1809- . mockReturnValueOnce ( 'mock-id3' ) ; // call to add account
1810-
1811- const mockSimpleKeyring1 = createExpectedInternalAccount ( {
1812- id : 'mock-id2' ,
1813- name : 'Account 2' ,
1814- address : '0x555' ,
1815- keyringType : 'Simple Key Pair' ,
1816- } ) ;
1817- const mockSimpleKeyring2 = createExpectedInternalAccount ( {
1818- id : 'mock-id3' ,
1819- name : 'Account 3' ,
1820- address : '0x666' ,
1821- keyringType : 'Simple Key Pair' ,
1822- } ) ;
1802+ // Account names start at 2 since have 1 HD account + 2 simple keypair accounts (and both
1803+ // those keyring types are "grouped" together)
1804+ const mockSimpleKeyring1 = createExpectedInternalAccount ( {
1805+ id : 'mock-id2' ,
1806+ name : 'Account 2' ,
1807+ address : '0x555' ,
1808+ keyringType : 'Simple Key Pair' ,
1809+ } ) ;
1810+ const mockSimpleKeyring2 = createExpectedInternalAccount ( {
1811+ id : 'mock-id3' ,
1812+ name : 'Account 3' ,
1813+ address : '0x666' ,
1814+ keyringType : 'Simple Key Pair' ,
1815+ } ) ;
1816+ const mockSimpleKeyring3 = createExpectedInternalAccount ( {
1817+ id : 'mock-id4' ,
1818+ name : 'Account 4' ,
1819+ address : '0x777' ,
1820+ keyringType : 'Simple Key Pair' ,
1821+ } ) ;
18231822
1824- const mockNewKeyringState = {
1823+ const mockNewKeyringStateWith = ( simpleAddressess : string [ ] ) => {
1824+ return {
18251825 isUnlocked : true ,
18261826 keyrings : [
18271827 {
@@ -1830,10 +1830,21 @@ describe('AccountsController', () => {
18301830 } ,
18311831 {
18321832 type : 'Simple Key Pair' ,
1833- accounts : [ mockSimpleKeyring1 . address , mockSimpleKeyring2 . address ] ,
1833+ accounts : simpleAddressess ,
18341834 } ,
18351835 ] ,
18361836 } ;
1837+ } ;
1838+
1839+ it ( 'should return the next account number' , async ( ) => {
1840+ const messenger = buildMessenger ( ) ;
1841+ mockUUID
1842+ . mockReturnValueOnce ( 'mock-id' ) // call to check if its a new account
1843+ . mockReturnValueOnce ( 'mock-id2' ) // call to check if its a new account
1844+ . mockReturnValueOnce ( 'mock-id3' ) // call to check if its a new account
1845+ . mockReturnValueOnce ( 'mock-id2' ) // call to add account
1846+ . mockReturnValueOnce ( 'mock-id3' ) ; // call to add account
1847+
18371848 const accountsController = setupAccountsController ( {
18381849 initialState : {
18391850 internalAccounts : {
@@ -1848,18 +1859,77 @@ describe('AccountsController', () => {
18481859
18491860 messenger . publish (
18501861 'KeyringController:stateChange' ,
1851- mockNewKeyringState ,
1862+ mockNewKeyringStateWith ( [
1863+ mockSimpleKeyring1 . address ,
1864+ mockSimpleKeyring2 . address ,
1865+ ] ) ,
18521866 [ ] ,
18531867 ) ;
18541868
18551869 const accounts = accountsController . listAccounts ( ) ;
1856-
18571870 expect ( accounts ) . toStrictEqual ( [
18581871 mockAccount ,
18591872 setLastSelectedAsAny ( mockSimpleKeyring1 ) ,
18601873 setLastSelectedAsAny ( mockSimpleKeyring2 ) ,
18611874 ] ) ;
18621875 } ) ;
1876+
1877+ it ( 'should return the next account number even with an index gap' , async ( ) => {
1878+ const messenger = buildMessenger ( ) ;
1879+ const mockAccountUUIDs = new MockNormalAccountUUID ( [
1880+ mockAccount ,
1881+ mockSimpleKeyring1 ,
1882+ mockSimpleKeyring2 ,
1883+ mockSimpleKeyring3 ,
1884+ ] ) ;
1885+ mockUUID . mockImplementation ( mockAccountUUIDs . mock . bind ( mockAccountUUIDs ) ) ;
1886+
1887+ const accountsController = setupAccountsController ( {
1888+ initialState : {
1889+ internalAccounts : {
1890+ accounts : {
1891+ [ mockAccount . id ] : mockAccount ,
1892+ } ,
1893+ selectedAccount : mockAccount . id ,
1894+ } ,
1895+ } ,
1896+ messenger,
1897+ } ) ;
1898+
1899+ messenger . publish (
1900+ 'KeyringController:stateChange' ,
1901+ mockNewKeyringStateWith ( [
1902+ mockSimpleKeyring1 . address ,
1903+ mockSimpleKeyring2 . address ,
1904+ ] ) ,
1905+ [ ] ,
1906+ ) ;
1907+
1908+ // We then remove "Acccount 2" to create a gap
1909+ messenger . publish (
1910+ 'KeyringController:stateChange' ,
1911+ mockNewKeyringStateWith ( [ mockSimpleKeyring2 . address ] ) ,
1912+ [ ] ,
1913+ ) ;
1914+
1915+ // Finally we add a 3rd account, and it should be named "Account 4" (despite having a gap
1916+ // since "Account 2" no longer exists)
1917+ messenger . publish (
1918+ 'KeyringController:stateChange' ,
1919+ mockNewKeyringStateWith ( [
1920+ mockSimpleKeyring2 . address ,
1921+ mockSimpleKeyring3 . address ,
1922+ ] ) ,
1923+ [ ] ,
1924+ ) ;
1925+
1926+ const accounts = accountsController . listAccounts ( ) ;
1927+ expect ( accounts ) . toStrictEqual ( [
1928+ mockAccount ,
1929+ setLastSelectedAsAny ( mockSimpleKeyring2 ) ,
1930+ setLastSelectedAsAny ( mockSimpleKeyring3 ) ,
1931+ ] ) ;
1932+ } ) ;
18631933 } ) ;
18641934
18651935 describe ( 'getAccountByAddress' , ( ) => {
0 commit comments