Skip to content

Commit aad1598

Browse files
authored
Merge pull request #184 from deriv-com/revert-170-ahmed/DAPI-819/chore--code-improvement
Revert "ahmed/DAPI-819/chore--code-improvement"
2 parents b3930fc + 4378afb commit aad1598

File tree

91 files changed

+3929
-64
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+3929
-64
lines changed

src/components/AccountSwitcher/__tests__/AccountSwitcher.test.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mockUseLogout.mockImplementation(() => {
3737
};
3838
});
3939

40-
describe.skip('HeroHeader', () => {
40+
describe('HeroHeader', () => {
4141
beforeEach(() => {
4242
mockUseAuthContext.mockImplementation(() => {
4343
return {
@@ -64,13 +64,13 @@ describe.skip('HeroHeader', () => {
6464
jest.clearAllMocks();
6565
});
6666

67-
it('Should render current account ', () => {
67+
it.skip('Should render current account ', () => {
6868
const current_account_button = screen.getByRole('button', { name: /CR111111/i });
6969

7070
expect(current_account_button).toBeInTheDocument();
7171
});
7272

73-
it('Should call do logout on logout button click', async () => {
73+
it.skip('Should call do logout on logout button click', async () => {
7474
const current_account_button = await screen.findByRole('button', { name: /CR111111/i });
7575

7676
await act(async () => {
@@ -86,7 +86,7 @@ describe.skip('HeroHeader', () => {
8686
expect(mockLogout).toHaveBeenCalledTimes(1);
8787
});
8888

89-
it('should be able to close the dropdown by clicking on the arrow', async () => {
89+
it.skip('should be able to close the dropdown by clicking on the arrow', async () => {
9090
const current_account_button = await screen.findByRole('button', { name: /CR111111/i });
9191

9292
await act(async () => {
@@ -102,7 +102,7 @@ describe.skip('HeroHeader', () => {
102102
expect(close_dropdown_button).not.toBeVisible();
103103
});
104104

105-
it('Should render Accounts when no account is selected', () => {
105+
it.skip('Should render Accounts when no account is selected', () => {
106106
cleanup();
107107
mockUseAuthContext.mockImplementation(() => {
108108
return {
@@ -127,7 +127,7 @@ describe.skip('HeroHeader', () => {
127127
expect(accounts_button).toBeInTheDocument();
128128
});
129129

130-
it('Should render the dropdown menu on current account button click', async () => {
130+
it.skip('Should render the dropdown menu on current account button click', async () => {
131131
const current_account_button = screen.getByRole('button', { name: /USD/i });
132132

133133
await act(async () => {
@@ -139,7 +139,7 @@ describe.skip('HeroHeader', () => {
139139
expect(menu_items.length).toBe(1);
140140
});
141141

142-
it('Should update current account on menu item click', async () => {
142+
it.skip('Should update current account on menu item click', async () => {
143143
mockUseAuthContext.mockImplementation(() => {
144144
return {
145145
loginAccounts: fake_accounts,
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
import useApiToken from '@site/src/hooks/useApiToken';
2+
import useAuthContext from '@site/src/hooks/useAuthContext';
3+
import userEvent from '@testing-library/user-event';
4+
import useAppManager from '@site/src/hooks/useAppManager';
5+
import { render, screen } from '@site/src/test-utils';
6+
7+
import React, { act } from 'react';
8+
import ApiTokenNavbarItem from '..';
9+
import { TTokensArrayType } from '@site/src/types';
10+
import { TDashboardTab } from '@site/src/contexts/app-manager/app-manager.context';
11+
12+
jest.mock('@site/src/hooks/useApiToken');
13+
const mockUseApiToken = useApiToken as jest.MockedFunction<
14+
() => Partial<ReturnType<typeof useApiToken>>
15+
>;
16+
17+
jest.mock('@site/src/hooks/useAuthContext');
18+
19+
const mockUseAuthContext = useAuthContext as jest.MockedFunction<
20+
() => Partial<ReturnType<typeof useAuthContext>>
21+
>;
22+
23+
jest.mock('@site/src/hooks/useAppManager');
24+
25+
const mockUseAppManager = useAppManager as jest.MockedFunction<
26+
() => Partial<ReturnType<typeof useAppManager>>
27+
>;
28+
29+
const mockUpdateCurrentTab = jest.fn();
30+
31+
mockUseAppManager.mockImplementation(() => ({
32+
updateCurrentTab: mockUpdateCurrentTab,
33+
}));
34+
35+
describe('Api Token Navbar Item', () => {
36+
it('Should NOT render anything when user is not logged in or is not authenticated', () => {
37+
mockUseAuthContext.mockImplementation(() => ({
38+
is_authorized: false,
39+
is_logged_in: false,
40+
}));
41+
42+
mockUseApiToken.mockImplementation(() => ({
43+
tokens: [],
44+
currentToken: {},
45+
isLoadingTokens: true,
46+
}));
47+
48+
const renderResult = render(<ApiTokenNavbarItem />);
49+
expect(renderResult.container).toBeEmptyDOMElement();
50+
});
51+
52+
it('Should close the token dropdown when clicking outside of it', async () => {
53+
mockUseAuthContext.mockImplementation(() => ({
54+
is_authorized: true,
55+
is_logged_in: true,
56+
}));
57+
58+
mockUseApiToken.mockImplementation(() => ({
59+
tokens: [
60+
{
61+
display_name: 'first_token',
62+
last_used: '',
63+
scopes: ['read', 'trade'],
64+
token: 'token_1',
65+
valid_for_ip: '',
66+
},
67+
{
68+
display_name: 'michio_app_pages',
69+
last_used: '2022-10-04 10:33:51',
70+
scopes: ['read', 'trade', 'payments', 'trading_information', 'admin'],
71+
token: 'token_2',
72+
valid_for_ip: '',
73+
},
74+
],
75+
currentToken: {
76+
display_name: 'first_token',
77+
last_used: '',
78+
scopes: ['read', 'trade'],
79+
token: 'token_1',
80+
valid_for_ip: '',
81+
},
82+
isLoadingTokens: false,
83+
}));
84+
85+
render(<ApiTokenNavbarItem />);
86+
87+
const current_account_button = screen.getByText(/first_token/i);
88+
await act(async () => {
89+
await userEvent.click(current_account_button);
90+
});
91+
92+
const alternative_account = screen.getByText(/michio_app_pages/i);
93+
expect(alternative_account).toBeVisible();
94+
95+
await act(async () => {
96+
await userEvent.click(document.body);
97+
});
98+
expect(alternative_account).not.toBeVisible();
99+
});
100+
101+
it('Should render current api token', async () => {
102+
mockUseAuthContext.mockImplementation(() => ({
103+
is_authorized: true,
104+
is_logged_in: true,
105+
}));
106+
107+
mockUseApiToken.mockImplementation(() => ({
108+
tokens: [
109+
{
110+
display_name: 'first_token',
111+
last_used: '',
112+
scopes: ['read', 'trade'],
113+
token: 'token_1',
114+
valid_for_ip: '',
115+
},
116+
{
117+
display_name: 'michio_app_pages',
118+
last_used: '2022-10-04 10:33:51',
119+
scopes: ['read', 'trade', 'payments', 'trading_information', 'admin'],
120+
token: 'token_2',
121+
valid_for_ip: '',
122+
},
123+
],
124+
currentToken: {
125+
display_name: 'first_token',
126+
last_used: '',
127+
scopes: ['read', 'trade'],
128+
token: 'token_1',
129+
valid_for_ip: '',
130+
},
131+
isLoadingTokens: false,
132+
}));
133+
134+
render(<ApiTokenNavbarItem />);
135+
136+
const currentTokenButton = screen.getByRole('button');
137+
138+
expect(currentTokenButton).toBeInTheDocument();
139+
140+
expect(currentTokenButton).toHaveTextContent('first_token');
141+
});
142+
143+
it('Should render please create token when current token is empty', () => {
144+
mockUseAuthContext.mockImplementation(() => ({
145+
is_authorized: true,
146+
is_logged_in: true,
147+
}));
148+
149+
mockUseApiToken.mockImplementation(() => ({
150+
tokens: [],
151+
currentToken: null,
152+
isLoadingTokens: false,
153+
}));
154+
155+
render(<ApiTokenNavbarItem />);
156+
157+
const currentTokenButton = screen.getByRole('link', { name: /add new token/i });
158+
159+
expect(currentTokenButton).toBeInTheDocument();
160+
});
161+
162+
it.skip('Should update app manager page when clicking on add new token', async () => {
163+
render(<ApiTokenNavbarItem />);
164+
165+
const create_token = await screen.findByText(/add new token/i);
166+
167+
await act(async () => {
168+
await userEvent.click(create_token);
169+
});
170+
171+
expect(mockUpdateCurrentTab).toHaveBeenCalledTimes(1);
172+
expect(mockUpdateCurrentTab).toHaveBeenCalledWith(TDashboardTab.MANAGE_TOKENS);
173+
});
174+
175+
it('Should render token in drop down', async () => {
176+
mockUseAuthContext.mockImplementation(() => ({
177+
is_authorized: true,
178+
is_logged_in: true,
179+
}));
180+
181+
const fake_tokens: TTokensArrayType = [
182+
{
183+
display_name: 'first_token',
184+
last_used: '',
185+
scopes: ['read', 'trade'],
186+
token: 'token_1',
187+
valid_for_ip: '',
188+
},
189+
{
190+
display_name: 'michio_app_pages',
191+
last_used: '2022-10-04 10:33:51',
192+
scopes: ['read', 'trade', 'payments', 'trading_information', 'admin'],
193+
token: 'token_2',
194+
valid_for_ip: '',
195+
},
196+
];
197+
198+
mockUseApiToken.mockImplementation(() => ({
199+
tokens: [...fake_tokens],
200+
currentToken: {
201+
display_name: 'first_token',
202+
last_used: '',
203+
scopes: ['read', 'trade'],
204+
token: 'token_1',
205+
valid_for_ip: '',
206+
},
207+
isLoadingTokens: false,
208+
}));
209+
210+
render(<ApiTokenNavbarItem />);
211+
212+
const current_account_button = screen.getByRole('button');
213+
await act(async () => {
214+
await userEvent.click(current_account_button);
215+
});
216+
const menu_items = screen.getAllByRole('menuitem');
217+
const tokens = menu_items.slice(0, 2);
218+
219+
expect(menu_items.length).toBe(1);
220+
221+
for (const [index, item] of tokens.entries()) {
222+
expect(item).toHaveTextContent(`${fake_tokens[index + 1].display_name}`);
223+
}
224+
});
225+
226+
it('Should update current token on menu item click', async () => {
227+
mockUseAuthContext.mockImplementation(() => ({
228+
is_authorized: true,
229+
is_logged_in: true,
230+
}));
231+
232+
const fake_tokens: TTokensArrayType = [
233+
{
234+
display_name: 'first_token',
235+
last_used: '',
236+
scopes: ['read', 'trade'],
237+
token: 'token_1',
238+
valid_for_ip: '',
239+
},
240+
{
241+
display_name: 'michio_app_pages',
242+
last_used: '2022-10-04 10:33:51',
243+
scopes: ['read', 'trade', 'payments', 'trading_information', 'admin'],
244+
token: 'token_2',
245+
valid_for_ip: '',
246+
},
247+
];
248+
249+
const mockUpdateCurrentToken = jest.fn();
250+
251+
mockUseApiToken.mockImplementation(() => ({
252+
tokens: fake_tokens,
253+
currentToken: {
254+
display_name: 'first_token',
255+
last_used: '',
256+
scopes: ['read', 'trade'],
257+
token: 'token_1',
258+
valid_for_ip: '',
259+
},
260+
isLoadingTokens: false,
261+
updateCurrentToken: mockUpdateCurrentToken,
262+
}));
263+
264+
render(<ApiTokenNavbarItem />);
265+
266+
const currentTokenButton = screen.getByRole('button');
267+
await act(async () => {
268+
await userEvent.click(currentTokenButton);
269+
});
270+
271+
const first_menu_item = screen.getByText(/michio_app_pages/i);
272+
273+
await act(async () => {
274+
await userEvent.click(first_menu_item);
275+
});
276+
277+
expect(mockUpdateCurrentToken).toHaveBeenCalledTimes(1);
278+
279+
expect(mockUpdateCurrentToken).toHaveBeenCalledWith({
280+
display_name: 'michio_app_pages',
281+
last_used: '2022-10-04 10:33:51',
282+
scopes: ['read', 'trade', 'payments', 'trading_information', 'admin'],
283+
token: 'token_2',
284+
valid_for_ip: '',
285+
});
286+
});
287+
});

0 commit comments

Comments
 (0)