Skip to content

Commit

Permalink
Write unit tests for the 'makeSortedPhrase' util
Browse files Browse the repository at this point in the history
  • Loading branch information
neuodev committed Mar 16, 2023
1 parent 655b7c8 commit 11c4927
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
Expand Up @@ -10,6 +10,7 @@ import styles from './VerifyRecoveryPhraseStep.scss';
import classnames from 'classnames';
import { ReactComponent as VerifiedIcon } from '../../../assets/images/verify-icon-green.inline.svg'
import environment from '../../../environment';
import { makeSortedPhrase } from '../../../utils/recoveryPhrase';

const messages: * = defineMessages({
description: {
Expand Down Expand Up @@ -74,30 +75,9 @@ function VerifyRecoveryPhraseStep(props: Props & Intl): Node {
return () => setCurrentStep(CREATE_WALLET_SETPS.ADD_WALLET_DETAILS)
};

const shortedRecoveryPhrase = useMemo(() => {
const sorted = recoveryPhrase.slice().sort();
const sortedRecoveryPhrase = useMemo(() => makeSortedPhrase(recoveryPhrase), [recoveryPhrase]);

const wordIndexes = new Set();

return sorted.map((sortedWord, sortedWordIdx) => {
const originalIdx = recoveryPhrase.findIndex((originalWord, idx) => {
return sortedWord === originalWord && !wordIndexes.has(idx)
});

if (originalIdx === -1) throw new Error('Word not found in the original recovery phrase. Should never happen');

// Mark word index as watched to handle recovery phrase with duplicates
wordIndexes.add(originalIdx);

return {
word: sortedWord,
originalIdx,
id: sortedWordIdx,
};
});
}, [recoveryPhrase]);

console.log(shortedRecoveryPhrase);
console.log(sortedRecoveryPhrase, recoveryPhrase);

return (
<Stack alignItems='center' justifyContent='center' className={styles.component}>
Expand Down
41 changes: 41 additions & 0 deletions packages/yoroi-extension/app/utils/recoveryPhrase.test.js
@@ -0,0 +1,41 @@
// @flow

import { makeSortedPhrase } from './recoveryPhrase';

describe('makeSortedPhrase()', () => {
test('sort recovery phrase with unique words', () => {
const phraseWithUniqueWords = ['w_3', 'w_2', 'w_1'];
const sorted = makeSortedPhrase(phraseWithUniqueWords);

const [first, second, third] = sorted;
expect(first.id).toBe(0);
expect(first.word).toBe('w_1');
expect(first.originalIdx).toBe(2);

expect(second.id).toBe(1);
expect(second.word).toBe('w_2');
expect(second.originalIdx).toBe(1);

expect(third.id).toBe(2);
expect(third.word).toBe('w_3');
expect(third.originalIdx).toBe(0);
});

test('sort recovery phrase with duplicates', () => {
const recoveryPhraseWithDuplicates = ['abandon', 'address', 'abandon'];
const sorted = makeSortedPhrase(recoveryPhraseWithDuplicates);

const [first, second, third] = sorted;
expect(first.id).toBe(0);
expect(first.word).toBe('abandon');
expect(first.originalIdx).toBe(0);

expect(second.id).toBe(1);
expect(second.word).toBe('abandon');
expect(second.originalIdx).toBe(2);

expect(third.id).toBe(2);
expect(third.word).toBe('address');
expect(third.originalIdx).toBe(1);
})
});

0 comments on commit 11c4927

Please sign in to comment.