Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 88afda9

Browse files
committed
chore: added tests and states
1 parent 736a7cf commit 88afda9

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/features/dashboard/components/ApiTokenForm/__tests__/api-token.form.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event';
44
import ApiTokenForm from '../api-token.form';
55
import useCreateToken from '../../../hooks/useCreateToken';
66
import useApiToken from '@site/src/hooks/useApiToken';
7+
import TokenNameRestrictions from '../../TokenNameRestrictions/TokenNameRestrictions';
78

89
jest.mock('@site/src/hooks/useApiToken');
910

@@ -138,6 +139,14 @@ describe('Home Page', () => {
138139
expect(error).toBeVisible;
139140
});
140141

142+
it('should hide restrictions if error is present', async () => {
143+
const nameInput = screen.getByRole('textbox');
144+
const restrictions = screen.getByRole('list');
145+
expect(restrictions).toBeVisible();
146+
await userEvent.type(nameInput, 'testtoken1');
147+
expect(restrictions).not.toBeVisible();
148+
});
149+
141150
it('Should not create token when name input is empty', async () => {
142151
const nameInput = screen.getByRole('textbox');
143152

src/features/dashboard/components/ApiTokenForm/api-token.form.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { HTMLAttributes, useCallback, useState } from 'react';
1+
import React, { HTMLAttributes, useCallback, useEffect, useState } from 'react';
22
import { Text } from '@deriv/ui';
33
import { useForm } from 'react-hook-form';
44
import { Circles } from 'react-loader-spinner';
@@ -80,7 +80,7 @@ const scopes: TScope[] = [
8080

8181
const ApiTokenForm = (props: HTMLAttributes<HTMLFormElement>) => {
8282
const { createToken, isCreatingToken } = useCreateToken();
83-
const [restrictions, setRestrictions] = useState(false);
83+
const [hiderestrictions, setHideRestrictions] = useState(false);
8484
const [form_is_cleared, setFormIsCleared] = useState(false);
8585

8686
const {
@@ -94,7 +94,6 @@ const ApiTokenForm = (props: HTMLAttributes<HTMLFormElement>) => {
9494
resolver: yupResolver(schema),
9595
mode: 'all',
9696
});
97-
9897
const onSubmit = useCallback(
9998
(data: TApiTokenForm) => {
10099
const { name } = data;
@@ -120,6 +119,10 @@ const ApiTokenForm = (props: HTMLAttributes<HTMLFormElement>) => {
120119
[getValues, setValue],
121120
);
122121

122+
useEffect(() => {
123+
errors.name?.message ? setHideRestrictions(true) : setHideRestrictions(false);
124+
}, [errors.name?.message]);
125+
123126
return (
124127
<form role={'form'} onSubmit={handleSubmit(onSubmit)} {...props}>
125128
<div className={styles.steps_line} />
@@ -160,7 +163,7 @@ const ApiTokenForm = (props: HTMLAttributes<HTMLFormElement>) => {
160163
form_is_cleared={form_is_cleared}
161164
setFormIsCleared={setFormIsCleared}
162165
/>
163-
<TokenNameRestrictions />
166+
{!hiderestrictions && <TokenNameRestrictions />}
164167
<div className={styles.step_title}>
165168
<div className={`${styles.third_step} ${styles.step}`}>
166169
<Text as={'p'} type={'paragraph-1'} data-testid={'third-step-title'}>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import { render, screen } from '@site/src/test-utils';
3+
import TokenNameRestrictions from '../TokenNameRestrictions';
4+
5+
describe('Restrictions for App name', () => {
6+
beforeEach(() => {
7+
render(<TokenNameRestrictions />);
8+
});
9+
it('Should render the list', () => {
10+
const RestrictonList = screen.getByRole('list');
11+
expect(RestrictonList).toBeInTheDocument();
12+
});
13+
14+
it('Should display correct content inside list items', () => {
15+
const listItem1 = screen.getByText(
16+
'Only alphanumeric characters with spaces and underscores are allowed.',
17+
);
18+
const listItem2 = screen.getByText('Only 2-32 characters are allowed');
19+
const listItem3 = screen.getByText(
20+
'No duplicate token names are allowed for the same account.',
21+
);
22+
const listItem4 = screen.getByText(
23+
'No keywords "deriv" or "binary" or words that look similar, e.g. "_binary_" or "d3eriv" are allowed.',
24+
);
25+
26+
expect(listItem1).toBeInTheDocument();
27+
expect(listItem2).toBeInTheDocument();
28+
expect(listItem3).toBeInTheDocument();
29+
expect(listItem4).toBeInTheDocument();
30+
});
31+
});

0 commit comments

Comments
 (0)