-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4171 from LiskHQ/4169-no-error-providing-second-p…
…assphrase Disable send button for an incorrect second passphrase - Closes #4169
- Loading branch information
Showing
4 changed files
with
104 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useState } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { selectActiveTokenAccount } from '@store/selectors'; | ||
import { extractPublicKey } from '@utils/account'; | ||
|
||
const empty2ndPass = { | ||
data: '', | ||
error: -1, | ||
feedback: [], | ||
}; | ||
|
||
const setSecondPassphrase = () => { | ||
const [secondPass, set2ndPass] = useState(empty2ndPass); | ||
const account = useSelector(selectActiveTokenAccount); | ||
|
||
const validate2ndPass = (data, error) => { | ||
const messages = []; | ||
if (error) { | ||
messages.push(messages); | ||
return messages; | ||
} | ||
|
||
const secondPublicKey = account.keys.mandatoryKeys | ||
.filter(item => item !== account.summary.publicKey); | ||
const publicKey = extractPublicKey(data); | ||
|
||
// compare them | ||
if (!secondPublicKey.length || publicKey !== secondPublicKey[0]) { | ||
messages.push('This passphrase does not belong to your account.'); | ||
} | ||
return messages; | ||
}; | ||
|
||
const setter = (data, error) => { | ||
const feedback = validate2ndPass(data, error); | ||
set2ndPass({ | ||
data, | ||
error: data === '' ? -1 : feedback.length, | ||
feedback, | ||
}); | ||
}; | ||
|
||
return [secondPass, setter]; | ||
}; | ||
|
||
export default setSecondPassphrase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useSelector } from 'react-redux'; | ||
import setSecondPassphrase from './setSecondPassphrase'; | ||
import accounts from '../../test/constants/accounts'; | ||
|
||
jest.mock('@store'); | ||
|
||
// const mockSelector = jest.fn(); | ||
useSelector.mockReturnValue(accounts.secondPass); | ||
|
||
describe('setSecondPassphrase', () => { | ||
it('Should return second passphrase with no error message', () => { | ||
const result = renderHook(() => setSecondPassphrase()); | ||
const [state, setState] = result.result.current; | ||
expect(state.error).toBe(-1); | ||
act(() => { | ||
setState(accounts.secondPass.secondPass); | ||
}); | ||
expect(result.result.current[0].error).toBe(0); | ||
}); | ||
|
||
it('Should return second passphrase with relevant error message', () => { | ||
const result = renderHook(() => setSecondPassphrase()); | ||
const [state, setState] = result.result.current; | ||
expect(state.error).toBe(-1); | ||
act(() => { | ||
setState(accounts.genesis.passphrase); | ||
}); | ||
expect(result.result.current[0].error).toBe(1); | ||
}); | ||
|
||
it('Should return second passphrase with an external error message, if provided', () => { | ||
const result = renderHook(() => setSecondPassphrase()); | ||
const [state, setState] = result.result.current; | ||
expect(state.error).toBe(-1); | ||
act(() => { | ||
setState('wrong_pass', 'The pass is invalid'); | ||
}); | ||
expect(result.result.current[0].error).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters