diff --git a/__tests__/users/App.test.js b/__tests__/users/App.test.js index 05b36b53..54cbbeeb 100644 --- a/__tests__/users/App.test.js +++ b/__tests__/users/App.test.js @@ -37,9 +37,13 @@ describe('App Component', () => { headers, body: JSON.stringify({ ...filteredUsersData, - users: filteredUsersData.users.filter( - (user) => user.roles.in_discord, - ), + ...mockUserData, + users: [ + ...filteredUsersData.users.filter( + (user) => user.roles.in_discord, + ), + ...mockUserData.users.filter((user) => user.roles.in_discord), + ], }), }); } else if (url === `${API_BASE_URL}/users/search/?verified=true`) { @@ -49,9 +53,11 @@ describe('App Component', () => { headers, body: JSON.stringify({ ...filteredUsersData, - users: filteredUsersData.users.filter((user) => user.discordId), ...mockUserData, - users: mockUserData.users.filter((user) => user.discordId), + users: [ + ...filteredUsersData.users.filter((user) => user.discordId), + ...mockUserData.users.filter((user) => user.discordId), + ], }), }); } else { @@ -96,4 +102,48 @@ describe('App Component', () => { const url = await page.url(); expect(url).toContain('?tab=verified'); }); + + it('should handle user card clicks and apply active_tab class to clicked card only in discord tab', async () => { + await page.goto(`${BASE_URL}/users/discord/?tab=in_discord`); + await page.waitForNetworkIdle(); + await page.waitForSelector('.user_card[data-key]'); + const userCardTestIds = await page.$$eval( + '[data-testid^="user-card-"]', + (cards) => cards.map((card) => card.getAttribute('data-testid')), + ); + for (let i = 0; i < userCardTestIds.length; i++) { + const userCardSelector = `[data-testid="${userCardTestIds[i]}"]`; + const userCardElement = await page.$(userCardSelector); + await userCardElement.click(); + await page.waitForTimeout(1000); + const isActive = await page.evaluate((selector) => { + return document + .querySelector(selector) + ?.classList.contains('active_tab'); + }, userCardSelector); + expect(isActive).toBe(true); + } + }); + + it('should handle user card clicks and apply active_tab class to clicked card only verified tab', async () => { + await page.goto(`${BASE_URL}/users/discord/?tab=verified`); + await page.waitForNetworkIdle(); + await page.waitForSelector('.user_card[data-key]'); + const userCardTestIds = await page.$$eval( + '[data-testid^="user-card-"]', + (cards) => cards.map((card) => card.getAttribute('data-testid')), + ); + for (let i = 0; i < userCardTestIds.length; i++) { + const userCardSelector = `[data-testid="${userCardTestIds[i]}"]`; + const userCardElement = await page.$(userCardSelector); + await userCardElement.click(); + await page.waitForTimeout(1000); + const isActive = await page.evaluate((selector) => { + return document + .querySelector(selector) + ?.classList.contains('active_tab'); + }, userCardSelector); + expect(isActive).toBe(true); + } + }); }); diff --git a/users/discord/App.js b/users/discord/App.js index 87fa873d..d1b57e0e 100644 --- a/users/discord/App.js +++ b/users/discord/App.js @@ -37,16 +37,16 @@ const handleTabNavigation = async (e) => { } }; -const handleUserSelected = (e) => { - const selectedUserId = - e.target?.getAttribute('data_key') || - e.target.parentElement?.getAttribute('data_key'); - - if (selectedUserId) { - showUser = usersData[activeTab]?.findIndex( - (user) => user.id === selectedUserId, +const handleUserSelected = (userId) => { + if (userId) { + const selectedUserIndex = usersData[activeTab]?.findIndex( + (user) => user.id === userId, ); - rerender(App(), window['root']); + + if (selectedUserIndex !== -1) { + showUser = selectedUserIndex; + rerender(App(), window['root']); + } } }; diff --git a/users/discord/components/UsersSection.js b/users/discord/components/UsersSection.js index 4d0ac94b..51773f79 100644 --- a/users/discord/components/UsersSection.js +++ b/users/discord/components/UsersSection.js @@ -5,7 +5,7 @@ export const UsersSection = ({ users, showUser, handleUserSelected }) => { 'aside', { class: 'users_section', - onClick: handleUserSelected, + 'data-testid': 'users-section', }, users?.map((user) => { @@ -17,6 +17,7 @@ export const UsersSection = ({ users, showUser, handleUserSelected }) => { }`, 'data-testid': `user-card-${user.id}`, 'data-key': user.id, + onclick: () => handleUserSelected(user.id), }, [ createElement('img', { diff --git a/users/discord/utils/util.js b/users/discord/utils/util.js index 83cd40e5..1937147e 100644 --- a/users/discord/utils/util.js +++ b/users/discord/utils/util.js @@ -1,3 +1,8 @@ +const API_BASE_URL = + window.location.hostname === 'localhost' + ? 'https://staging-api.realdevsquad.com' + : window.API_BASE_URL; + export const getUsers = async (tab) => { let URL = { in_discord: `${API_BASE_URL}/users/search/?role=in_discord`,