Skip to content

Commit 4708131

Browse files
committed
Fixing a profile dropdown behavior and added a chevron-down icon
1 parent 6a76498 commit 4708131

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

__tests__/navbar/navbar.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ describe('Tests the navbar and its components on various pages', () => {
1515

1616
const navLinks = await navbarPage.$('.nav-links');
1717
expect(navLinks).toBeTruthy();
18+
19+
const devFlag = await navbarPage.evaluate(() => {
20+
return new URLSearchParams(window.location.search).get('dev') === 'true';
21+
});
22+
23+
const chevronIcon = await navbarPage.$('#chevron-down');
24+
if (devFlag) {
25+
expect(chevronIcon).toBeTruthy();
26+
} else {
27+
expect(chevronIcon).toBeFalsy();
28+
}
1829
};
1930

2031
beforeAll(async () => {
@@ -99,4 +110,49 @@ describe('Tests the navbar and its components on various pages', () => {
99110
await page.goto(`${LOCAL_TEST_PAGE_URL}/feed/index.html`);
100111
await testNavbar(page);
101112
});
113+
it('should close the dropdown by clicking outside the dropdown under dev feature flag', async () => {
114+
await page.goto(`${LOCAL_TEST_PAGE_URL}?dev=true`);
115+
116+
const userInfoHandle = await page.$('.user-info');
117+
const dropdownHandle = await page.$('#dropdown');
118+
119+
expect(userInfoHandle).toBeTruthy();
120+
expect(dropdownHandle).toBeTruthy();
121+
122+
await page.evaluate(() => {
123+
const userInfo = document.querySelector('.user-info');
124+
if (userInfo) {
125+
userInfo.click();
126+
}
127+
});
128+
await page.mouse.click(100, 100);
129+
const dropdownIsActive = await dropdownHandle.evaluate((el) =>
130+
el.classList.contains('active'),
131+
);
132+
expect(dropdownIsActive).toBe(false);
133+
});
134+
it('should keep the dropdown open when clicking outside when feature flag is off', async () => {
135+
await page.goto(`${LOCAL_TEST_PAGE_URL}?dev=false`);
136+
await page.waitForSelector('#dropdown');
137+
await page.evaluate(() => {
138+
const dropdown = document.getElementById('dropdown');
139+
if (dropdown && !dropdown.classList.contains('active')) {
140+
dropdown.classList.add('active');
141+
}
142+
});
143+
let dropdownIsActive = await page.evaluate(() => {
144+
const dropdown = document.getElementById('dropdown');
145+
return dropdown?.classList.contains('active') ?? false;
146+
});
147+
expect(dropdownIsActive).toBe(true);
148+
await page.evaluate(() => {
149+
document.body.click();
150+
});
151+
await page.waitForTimeout(200);
152+
const newDropdownHandle = await page.$('#dropdown');
153+
const newDropdownIsActive = await newDropdownHandle.evaluate((el) =>
154+
el.classList.contains('active'),
155+
);
156+
expect(newDropdownIsActive).toBe(true);
157+
});
102158
});

images/chevron-down.svg

Lines changed: 1 addition & 1 deletion
Loading

navbar.global.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
const urlParams = new URLSearchParams(window.location.search);
2+
const devFlag = urlParams.get('dev') === 'true';
3+
const chevronIcon = devFlag
4+
? `<span><img id="chevron-down" src="images/chevron-down.svg" alt="chevron-down icon" /></span>`
5+
: '';
16
const addNavbartoPage = async () => {
27
const navbar = document?.getElementById('tasksNav');
38
const navbarParams = new URLSearchParams(window?.location?.search);
49
navbar.innerHTML = `
510
<div class="logo">
611
<a href="/index.html">
7-
<img src="/images/[email protected]" alt="logo" />
12+
<img src="/images/[email protected]" alt="logo" />
813
</a>
914
</div>
1015
<div class="nav-links">
@@ -31,6 +36,7 @@ const addNavbartoPage = async () => {
3136
<span>
3237
<img id="user-img" src="" alt="" />
3338
</span>
39+
${chevronIcon}
3440
</div>
3541
<div id="dropdown"></div>
3642
`;

userLogin.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const urlParam = new URLSearchParams(window.location.search);
2+
const isDevFlag = urlParam.get('dev') === 'true';
13
const DROPDOWN_OPTIONS = [
24
{
35
name: 'Home',
@@ -89,7 +91,16 @@ async function handleUserSignin() {
8991
dropdown.classList.add('active');
9092
}
9193
});
92-
94+
document.addEventListener('click', (event) => {
95+
if (
96+
isDevFlag &&
97+
dropdown.classList.contains('active') &&
98+
!dropdown.contains(event.target) &&
99+
!userInfo.contains(event.target)
100+
) {
101+
dropdown.classList.remove('active');
102+
}
103+
});
93104
signOutElement.addEventListener('click', () => {
94105
getSelfUser('/auth/signout');
95106
});

0 commit comments

Comments
 (0)