diff --git a/__tests__/users/onboarding-31-days-multiple-selections.test.js b/__tests__/users/onboarding-31-days-multiple-selections.test.js new file mode 100644 index 00000000..d06e5d4e --- /dev/null +++ b/__tests__/users/onboarding-31-days-multiple-selections.test.js @@ -0,0 +1,143 @@ +const puppeteer = require('puppeteer'); + +describe('Tests the "Onboarding > 31 Days" Filter', () => { + let browser; + let page; + + jest.setTimeout(60000); + + beforeAll(async () => { + browser = await puppeteer.launch({ + headless: 'new', //change headless to 'new' to check the tests in browser + ignoreHTTPSErrors: true, + args: ['--incognito', '--disable-web-security'], + devtools: false, + }); + page = await browser.newPage(); + + await page.setRequestInterception(true); + + page.on('request', (interceptedRequest) => { + const url = interceptedRequest.url(); + if (url === 'https://api.realdevsquad.com/tasks/sunny-s') { + // When we encounter the respective api call we respond with the below response + interceptedRequest.respond({ + status: 200, + contentType: 'application/json', + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type, Authorization', + }, + body: JSON.stringify(userDetailsApi), + }); + } else if (url === 'https://api.realdevsquad.com/users/self') { + // When we encounter the respective api call we respond with the below response + interceptedRequest.respond({ + status: 200, + contentType: 'application/json', + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type, Authorization', + }, + body: JSON.stringify(superUserDetails), // Y contains the json of a superuser in the server which will grant us the access to view the page without locks + }); + } else { + interceptedRequest.continue(); + } + }); + await page.goto('http://localhost:8000/users/?dev=true'); + + await page.waitForNetworkIdle(); + }); + + afterAll(async () => { + await browser.close(); + }); + + it('should gives results for Onboarding > 31 SELECTED', async () => { + const taskDiv = await page.$('.filter-button'); + expect(taskDiv).toBeTruthy(); + + await taskDiv.click(); + + await page.waitForTimeout(2000); + const elements = await page.$$('.checkbox-label'); + + // Checking if elements are found + expect(elements).toBeTruthy(); + + const checkbox = await page.$('#ONBOARDING31DAYS'); + await checkbox.click(); + + const applyfilterbutton = await page.$('.apply-filter-button'); + expect(applyfilterbutton).toBeTruthy(); + await applyfilterbutton.click(); + + await page.waitForTimeout(500); + }); + it('should gives results for ACTIVE SELECTED', async () => { + const taskDiv = await page.$('.filter-button'); + expect(taskDiv).toBeTruthy(); + + await taskDiv.click(); + + // await page.waitForTimeout(2000); enable to see the tests in actions + const elements = await page.$$('.checkbox-label'); + + const clear = await page.$('#clear-button'); + await clear.click(); + + // await page.waitForTimeout(2000); enable to see the tests in actions + + const taskDiv2 = await page.$('.filter-button'); + await taskDiv2.click(); + expect(taskDiv2).toBeTruthy(); + // await page.waitForTimeout(2000); enable to see the tests in actions + expect(elements).toBeTruthy(); + + const checkbox = await page.$('#ACTIVE'); + await checkbox.click(); + + const applyfilterbutton = await page.$('.apply-filter-button'); + expect(applyfilterbutton).toBeTruthy(); + await applyfilterbutton.click(); + + await page.waitForTimeout(500); + }); + + it('should gives results for both ACTIVE & Onboarding > 31 SELECTED', async () => { + const taskDiv = await page.$('.filter-button'); + expect(taskDiv).toBeTruthy(); + + await taskDiv.click(); + + // await page.waitForTimeout(2000); enable to see the tests in actions + const elements = await page.$$('.checkbox-label'); + + // Checking if elements are found + expect(elements).toBeTruthy(); + + const clear = await page.$('#clear-button'); + await clear.click(); + + // await page.waitForTimeout(2000); enable to see the tests in actions + + const taskDiv2 = await page.$('.filter-button'); + await taskDiv2.click(); + expect(taskDiv2).toBeTruthy(); + // await page.waitForTimeout(2000); enable to see the tests in actions + const checkbox = await page.$('#ONBOARDING31DAYS'); + await checkbox.click(); + // await page.waitForTimeout(2000); enable to see the tests in actions + const checkbox2 = await page.$('#ACTIVE'); + await checkbox2.click(); + // await page.waitForTimeout(2000); enable to see the tests in actions + const applyfilterbutton = await page.$('.apply-filter-button'); + expect(applyfilterbutton).toBeTruthy(); + await applyfilterbutton.click(); + + await page.waitForTimeout(500); + }); +}); diff --git a/users/script.js b/users/script.js index 1c7626c8..d10ceabc 100644 --- a/users/script.js +++ b/users/script.js @@ -400,11 +400,10 @@ function populateAvailability() { { name: 'Ooo (Out of Office)', id: 'OOO' }, { name: 'Idle', id: 'IDLE' }, { name: 'Onboarding', id: 'ONBOARDING' }, - { name: 'Onboarding > 31d', id: 'ONBOARDING31DAYS' }, ]; - if (params.get('dev') != 'true') { - availabilityArr.pop(); + if (params.get('dev') === 'true') { + availabilityArr.push({ name: 'Onboarding > 31d', id: 'ONBOARDING31DAYS' }); } for (let i = 0; i < availabilityArr.length; i++) { const { name, id } = availabilityArr[i]; @@ -585,18 +584,6 @@ async function persistUserDataBasedOnQueryParams() { } } -async function getUsersInOnboardingFor31Days() { - try { - const usersRequest = await makeApiCall( - `${RDS_API_USERS}/search/?state=ONBOARDING&time=31d`, - ); - const { users } = await usersRequest.json(); - return users; - } catch (err) { - throw new Error(`User list request failed with error: ${err}`); - } -} - // Function to apply the filter when the "Apply Filter" button is clicked applyFilterButton.addEventListener('click', async () => { filterModal.classList.toggle('hidden'); @@ -608,17 +595,49 @@ applyFilterButton.addEventListener('click', async () => { checkedValuesSkills, checkedValuesAvailability, ); - // Check if the "Onboarding > 31 Days" checkbox is checked - const onboarding31DaysFilter = - document.getElementById('ONBOARDING31DAYS').checked; - try { - let users; - if (onboarding31DaysFilter) { - // If the checkbox is checked, fetch users from the specific API endpoint - users = await getUsersInOnboardingFor31Days(); - } else { - // If the checkbox is not checked, fetch users with other filters + // Feature Flag Start + if (params.get('dev') === 'true') { + const onboarding31DaysFilter = + document.getElementById('ONBOARDING31DAYS').checked; + try { + let users; + if (onboarding31DaysFilter) { + let queryParams = getFilteredUsersURL( + checkedValuesSkills, + checkedValuesAvailability, + ); + + queryParams = replaceOnboarding31days(queryParams); + const usersRequest = await makeApiCall( + `${RDS_API_USERS}/search${queryParams}`, + ); + const { users: filteredUsers } = await usersRequest.json(); + users = filteredUsers; + } else { + let queryParams = getFilteredUsersURL( + checkedValuesSkills, + checkedValuesAvailability, + ); + const usersRequest = await makeApiCall( + `${RDS_API_USERS}/search${queryParams}`, + ); + const { users: filteredUsers } = await usersRequest.json(); + users = filteredUsers; + } + + manipulateQueryParamsToURL(queryParams); + // Display the filtered user list + showUserList(users); + } catch (err) { + throw new Error(`User list request failed with error: ${err}`); + } + } + // feature flag end + else { + try { + let users; + const queryParams = getFilteredUsersURL( checkedValuesSkills, checkedValuesAvailability, @@ -628,16 +647,35 @@ applyFilterButton.addEventListener('click', async () => { ); const { users: filteredUsers } = await usersRequest.json(); users = filteredUsers; - } - manipulateQueryParamsToURL(queryParams); - // Display the filtered user list - showUserList(users); - } catch (err) { - throw new Error(`User list request failed with error: ${err}`); + manipulateQueryParamsToURL(queryParams); + // Display the filtered user list + showUserList(users); + } catch (err) { + throw new Error(`User list request failed with error: ${err}`); + } } }); +function replaceOnboarding31days(queryParams) { + if (queryParams.includes('&state=ONBOARDING31DAYS')) { + // Replace "&state=ONBOARDING31DAYS" with "&state=ONBOARDING&time=31d" + queryParams = queryParams.replace( + '&state=ONBOARDING31DAYS', + '&state=ONBOARDING&time=31d', + ); + return queryParams; + } + if (queryParams.includes('?state=ONBOARDING31DAYS')) { + // Replace "&state=ONBOARDING31DAYS" with "&state=ONBOARDING&time=31d" + queryParams = queryParams.replace( + '?state=ONBOARDING31DAYS', + '?state=ONBOARDING&time=31d', + ); + return queryParams; + } +} + function clearCheckboxes(name) { const checkboxes = document.querySelectorAll(`input[name="${name}"]`); checkboxes.forEach((cb) => {