Skip to content

Commit 0ee1f83

Browse files
authored
Add ID to entropy selector and invalid option for E2E testing (#3172)
I've made some minor changes to `test-snaps` related to the SIP-30 entropy selector. - Each selector now has a unique ID. Previously the same ID was shared by multiple components, which is semantically incorrect HTML, and also makes it more difficult to E2E test. - I've added an "invalid" option to the selector, to test that we properly throw errors for entropy sources that don't exist.
1 parent 06798b3 commit 0ee1f83

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

packages/test-snaps/src/features/snaps/bip32/BIP32.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useEntropySelector } from '../get-entropy/hooks';
77

88
export const BIP32: FunctionComponent = () => {
99
const { selector, source } = useEntropySelector({
10+
id: 'bip32',
1011
snapId: BIP_32_SNAP_ID,
1112
port: BIP_32_PORT,
1213
});

packages/test-snaps/src/features/snaps/bip44/BIP44.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useEntropySelector } from '../get-entropy/hooks';
1212
export const BIP44: FunctionComponent = () => {
1313
const [invokeSnap, { isLoading, data, error }] = useInvokeMutation();
1414
const { selector, source } = useEntropySelector({
15+
id: 'bip44',
1516
snapId: BIP_44_SNAP_ID,
1617
port: BIP_44_PORT,
1718
});
@@ -62,7 +63,7 @@ export const BIP44: FunctionComponent = () => {
6263
</span>
6364
</Result>
6465

65-
<SignMessage />
66+
<SignMessage source={source} />
6667
</Snap>
6768
);
6869
};

packages/test-snaps/src/features/snaps/bip44/components/SignMessage.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import { Result } from '../../../../components';
88
import { getSnapId } from '../../../../utils';
99
import { BIP_44_PORT, BIP_44_SNAP_ID } from '../constants';
1010

11-
export const SignMessage: FunctionComponent = () => {
11+
export type SignMessageProps = {
12+
source: string | undefined;
13+
};
14+
15+
export const SignMessage: FunctionComponent<SignMessageProps> = ({
16+
source,
17+
}) => {
1218
const [message, setMessage] = useState('');
1319
const [invokeSnap, { isLoading, data, error }] = useInvokeMutation();
1420

@@ -24,6 +30,7 @@ export const SignMessage: FunctionComponent = () => {
2430
method: 'signMessage',
2531
params: {
2632
message,
33+
...(source !== undefined && { source }),
2734
},
2835
}).catch(logError);
2936
};

packages/test-snaps/src/features/snaps/get-entropy/GetEntropy.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Snap } from '../../../components';
1111

1212
export const GetEntropy: FunctionComponent = () => {
1313
const { selector, source } = useEntropySelector({
14+
id: 'get-entropy',
1415
raw: true,
1516
snapId: GET_ENTROPY_SNAP_ID,
1617
port: GET_ENTROPY_PORT,

packages/test-snaps/src/features/snaps/get-entropy/components/EntropySelector.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { Result } from '../../../../components';
55

66
export type EntropySourcesProps = {
77
sources: EntropySource[];
8-
raw: boolean;
8+
id: string;
9+
raw?: boolean | undefined;
910
onChange: (source: string | undefined) => void;
1011
};
1112

@@ -27,6 +28,7 @@ function getSourceName(source: EntropySource) {
2728

2829
export const EntropySelector: FunctionComponent<EntropySourcesProps> = ({
2930
sources,
31+
id,
3032
raw,
3133
onChange,
3234
}) => {
@@ -43,7 +45,7 @@ export const EntropySelector: FunctionComponent<EntropySourcesProps> = ({
4345
<>
4446
<h3 className="h6">Entropy source</h3>
4547
<select
46-
id="select-chain"
48+
id={`${id}-entropy-selector`}
4749
className="form-select mb-3"
4850
onChange={handleChange}
4951
>
@@ -53,10 +55,11 @@ export const EntropySelector: FunctionComponent<EntropySourcesProps> = ({
5355
{getSourceName(source)}
5456
</option>
5557
))}
58+
<option value="invalid">Invalid</option>
5659
</select>
5760
{raw && (
5861
<Result className="mb-3">
59-
<pre id="entropySourcesResult" style={{ margin: 0 }}>
62+
<pre id={`${id}-raw-entropy-sources`} style={{ margin: 0 }}>
6063
{JSON.stringify(sources, null, 2)}
6164
</pre>
6265
</Result>

packages/test-snaps/src/features/snaps/get-entropy/hooks/useEntropySelector.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import { getSnapId, useInstalled } from '../../../../utils';
66
import { EntropySelector } from '../components';
77

88
export type UseEntropySelectorOptions = {
9+
/**
10+
* The prefix to add to the HTML ID for E2E testing purposes.
11+
*/
12+
id: string;
13+
914
/**
1015
* Whether to show the raw list of entropy sources.
1116
*/
12-
raw?: boolean;
17+
raw?: boolean | undefined;
1318

1419
/**
1520
* The snap ID to use for the entropy sources.
@@ -28,12 +33,15 @@ export type UseEntropySelectorOptions = {
2833
* @param options - The options to use.
2934
* @param options.snapId - The snap ID to use for the entropy sources.
3035
* @param options.port - The port to use for the entropy sources.
36+
* @param options.id - The prefix to add to the HTML ID for E2E testing
37+
* purposes.
3138
* @param options.raw - Whether to show the raw list of entropy sources.
3239
* @returns The entropy source and selector.
3340
*/
3441
export const useEntropySelector = ({
3542
snapId: publicSnapId,
3643
port,
44+
id,
3745
raw = false,
3846
}: UseEntropySelectorOptions) => {
3947
const [source, setSource] = useState<string | undefined>(undefined);
@@ -54,6 +62,8 @@ export const useEntropySelector = ({
5462
return {
5563
source,
5664
sources: data,
57-
selector: <EntropySelector sources={data} raw={raw} onChange={setSource} />,
65+
selector: (
66+
<EntropySelector id={id} sources={data} raw={raw} onChange={setSource} />
67+
),
5868
};
5969
};

0 commit comments

Comments
 (0)