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

Commit 765011a

Browse files
Hubert KosterHubert Koster
authored andcommitted
chore: adding test coverage
1 parent 8efb908 commit 765011a

File tree

1 file changed

+97
-68
lines changed

1 file changed

+97
-68
lines changed

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

Lines changed: 97 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,6 @@ const mockUseApiToken = useApiToken as jest.MockedFunction<
1111
() => Partial<ReturnType<typeof useApiToken>>
1212
>;
1313

14-
mockUseApiToken.mockImplementation(() => ({
15-
tokens: [
16-
{
17-
display_name: 'testtoken1',
18-
last_used: '',
19-
scopes: ['read', 'trade', 'payments', 'admin'],
20-
token: 'asdf1234',
21-
valid_for_ip: '',
22-
},
23-
{
24-
display_name: 'testtoken2',
25-
last_used: '',
26-
scopes: ['read', 'trade', 'payments', 'admin'],
27-
token: 'asdf1235',
28-
valid_for_ip: '',
29-
},
30-
],
31-
}));
32-
3314
jest.mock('@site/src/features/dashboard/hooks/useCreateToken');
3415

3516
const mockUseCreateToken = useCreateToken as jest.MockedFunction<typeof useCreateToken>;
@@ -77,74 +58,122 @@ const scopes = [
7758
];
7859

7960
describe('Home Page', () => {
80-
beforeEach(() => {
81-
render(<ApiTokenForm />);
82-
});
61+
describe('General tests', () => {
62+
beforeEach(() => {
63+
mockUseApiToken.mockImplementation(() => ({
64+
tokens: [
65+
{
66+
display_name: 'testtoken1',
67+
last_used: '',
68+
scopes: ['read', 'trade', 'payments', 'admin'],
69+
token: 'asdf1234',
70+
valid_for_ip: '',
71+
},
72+
{
73+
display_name: 'testtoken2',
74+
last_used: '',
75+
scopes: ['read', 'trade', 'payments', 'admin'],
76+
token: 'asdf1235',
77+
valid_for_ip: '',
78+
},
79+
],
80+
}));
81+
82+
render(<ApiTokenForm />);
83+
});
8384

84-
afterEach(() => {
85-
cleanup();
86-
jest.clearAllMocks();
87-
});
85+
afterEach(() => {
86+
cleanup();
87+
jest.clearAllMocks();
88+
});
8889

89-
it('Should render first step title', () => {
90-
const firstStep = screen.getByTestId('first-step-title');
91-
expect(firstStep).toHaveTextContent(/Select scopes based on the access you need./i);
92-
});
90+
it('Should render first step title', () => {
91+
const firstStep = screen.getByTestId('first-step-title');
92+
expect(firstStep).toHaveTextContent(/Select scopes based on the access you need./i);
93+
});
9394

94-
it('Should render all of scopes checkbox cards', () => {
95-
scopes.forEach((item) => {
96-
const apiTokenCard = screen.getByTestId(`api-token-card-${item.name}`);
97-
expect(apiTokenCard).toBeInTheDocument();
95+
it('Should render all of scopes checkbox cards', () => {
96+
scopes.forEach((item) => {
97+
const apiTokenCard = screen.getByTestId(`api-token-card-${item.name}`);
98+
expect(apiTokenCard).toBeInTheDocument();
99+
});
98100
});
99-
});
100101

101-
it('Should render second step title', () => {
102-
const secondStep = screen.getByTestId('second-step-title');
103-
expect(secondStep).toHaveTextContent(
104-
/Name your token and click on Create to generate your token./i,
105-
);
106-
});
102+
it('Should render second step title', () => {
103+
const secondStep = screen.getByTestId('second-step-title');
104+
expect(secondStep).toHaveTextContent(
105+
/Name your token and click on Create to generate your token./i,
106+
);
107+
});
107108

108-
it('Should check the checkbox when clicked on api token card', async () => {
109-
const adminTokenCard = screen.getByTestId('api-token-card-admin');
110-
const withinAdminTokenCard = within(adminTokenCard);
111-
const adminCheckbox = withinAdminTokenCard.getByRole<HTMLInputElement>('checkbox');
109+
it('Should check the checkbox when clicked on api token card', async () => {
110+
const adminTokenCard = screen.getByTestId('api-token-card-admin');
111+
const withinAdminTokenCard = within(adminTokenCard);
112+
const adminCheckbox = withinAdminTokenCard.getByRole<HTMLInputElement>('checkbox');
112113

113-
expect(adminCheckbox.checked).toBeFalsy();
114+
expect(adminCheckbox.checked).toBeFalsy();
114115

115-
await userEvent.click(adminTokenCard);
116+
await userEvent.click(adminTokenCard);
116117

117-
expect(adminCheckbox.checked).toBeTruthy();
118-
});
118+
expect(adminCheckbox.checked).toBeTruthy();
119+
});
119120

120-
it('Should create token on form submit', async () => {
121-
const nameInput = screen.getByRole('textbox');
121+
it('Should create token on form submit', async () => {
122+
const nameInput = screen.getByRole('textbox');
122123

123-
await userEvent.type(nameInput, 'test create token');
124+
await userEvent.type(nameInput, 'test create token');
124125

125-
const submitButton = screen.getByRole('button', { name: /Create/i });
126-
await userEvent.click(submitButton);
126+
const submitButton = screen.getByRole('button', { name: /Create/i });
127+
await userEvent.click(submitButton);
127128

128-
expect(mockCreateToken).toHaveBeenCalledTimes(1);
129-
expect(mockCreateToken).toHaveBeenCalledWith('test create token', []);
130-
});
129+
expect(mockCreateToken).toHaveBeenCalledTimes(1);
130+
expect(mockCreateToken).toHaveBeenCalledWith('test create token', []);
131+
});
131132

132-
it('Should not be able to create a token if name already exists', async () => {
133-
const nameInput = screen.getByRole('textbox');
133+
it('Should not be able to create a token if name already exists', async () => {
134+
const nameInput = screen.getByRole('textbox');
134135

135-
await userEvent.type(nameInput, 'testtoken1');
136+
await userEvent.type(nameInput, 'testtoken1');
136137

137-
const error = screen.getByText(/That name is taken. Choose another./i);
138-
expect(error).toBeVisible;
139-
});
138+
const error = screen.getByText(/That name is taken. Choose another./i);
139+
expect(error).toBeVisible;
140+
});
140141

141-
it('Should not create token when name input is empty', async () => {
142-
const nameInput = screen.getByRole('textbox');
142+
it('Should not create token when name input is empty', async () => {
143+
const nameInput = screen.getByRole('textbox');
143144

144-
await userEvent.clear(nameInput);
145+
await userEvent.clear(nameInput);
145146

146-
await userEvent.click(nameInput);
147+
await userEvent.click(nameInput);
147148

148-
expect(mockCreateToken).not.toHaveBeenCalled();
149+
expect(mockCreateToken).not.toHaveBeenCalled();
150+
});
151+
});
152+
describe('Token limit', () => {
153+
const createMaxTokens = () => {
154+
const token_array = [];
155+
for (let i = 0; i < 30; i++) {
156+
token_array.push({
157+
display_name: `testtoken${i}`,
158+
last_used: '',
159+
scopes: ['read', 'trade', 'payments', 'admin'],
160+
token: 'asdf1234',
161+
valid_for_ip: '',
162+
});
163+
}
164+
return token_array;
165+
};
166+
167+
it('Should show an error when the user tries to create more than 30 tokens', async () => {
168+
mockUseApiToken.mockImplementation(() => ({ tokens: createMaxTokens() }));
169+
render(<ApiTokenForm />);
170+
171+
const nameInput = screen.getByRole('textbox');
172+
173+
await userEvent.type(nameInput, 'asdf');
174+
175+
const error = screen.getByText(/reached 30 tokens creation limit/i);
176+
expect(error).toBeVisible();
177+
});
149178
});
150179
});

0 commit comments

Comments
 (0)