|
| 1 | +const puppeteer = require('puppeteer'); |
| 2 | + |
| 3 | +const { |
| 4 | + fetchedApplications, |
| 5 | + acceptedApplications, |
| 6 | +} = require('../../mock-data/applications'); |
| 7 | +const { superUserForAudiLogs } = require('../../mock-data/users'); |
| 8 | + |
| 9 | +const SITE_URL = 'http://localhost:8000'; |
| 10 | +// helper/loadEnv.js file causes API_BASE_URL to be stagin-api on local env url in taskRequest/index.html |
| 11 | +const API_BASE_URL = 'https://staging-api.realdevsquad.com'; |
| 12 | + |
| 13 | +describe('Applications page', () => { |
| 14 | + let browser; |
| 15 | + let page; |
| 16 | + |
| 17 | + jest.setTimeout(60000); |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + browser = await puppeteer.launch({ |
| 21 | + headless: 'new', |
| 22 | + ignoreHTTPSErrors: true, |
| 23 | + args: ['--incognito', '--disable-web-security'], |
| 24 | + }); |
| 25 | + }); |
| 26 | + beforeEach(async () => { |
| 27 | + page = await browser.newPage(); |
| 28 | + |
| 29 | + await page.setRequestInterception(true); |
| 30 | + |
| 31 | + page.on('request', (request) => { |
| 32 | + if ( |
| 33 | + request.url() === `${API_BASE_URL}/applications?size=5` || |
| 34 | + request.url() === |
| 35 | + `${API_BASE_URL}/applications?next=YwTi6zFNI3GlDsZVjD8C&size=5` |
| 36 | + ) { |
| 37 | + request.respond({ |
| 38 | + status: 200, |
| 39 | + contentType: 'application/json', |
| 40 | + body: JSON.stringify({ |
| 41 | + applications: fetchedApplications, |
| 42 | + next: '/applications?next=YwTi6zFNI3GlDsZVjD8C&size=5', |
| 43 | + }), |
| 44 | + headers: { |
| 45 | + 'Access-Control-Allow-Origin': '*', |
| 46 | + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
| 47 | + 'Access-Control-Allow-Headers': 'Content-Type, Authorization', |
| 48 | + }, |
| 49 | + }); |
| 50 | + } else if ( |
| 51 | + request.url() === `${API_BASE_URL}/applications?size=5&status=accepted` |
| 52 | + ) { |
| 53 | + request.respond({ |
| 54 | + status: 200, |
| 55 | + contentType: 'application/json', |
| 56 | + body: JSON.stringify({ applications: acceptedApplications }), |
| 57 | + headers: { |
| 58 | + 'Access-Control-Allow-Origin': '*', |
| 59 | + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
| 60 | + 'Access-Control-Allow-Headers': 'Content-Type, Authorization', |
| 61 | + }, |
| 62 | + }); |
| 63 | + } else if (request.url() === `${API_BASE_URL}/users/self`) { |
| 64 | + request.respond({ |
| 65 | + status: 200, |
| 66 | + contentType: 'application/json', |
| 67 | + headers: { |
| 68 | + 'Access-Control-Allow-Origin': '*', |
| 69 | + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
| 70 | + 'Access-Control-Allow-Headers': 'Content-Type, Authorization', |
| 71 | + }, |
| 72 | + body: JSON.stringify(superUserForAudiLogs), |
| 73 | + }); |
| 74 | + } else if ( |
| 75 | + request.url() === `${API_BASE_URL}/applications/lavEduxsb2C5Bl4s289P` |
| 76 | + ) { |
| 77 | + request.respond({ |
| 78 | + status: 200, |
| 79 | + contentType: 'application/json', |
| 80 | + body: JSON.stringify({ |
| 81 | + message: 'application updated successfully!', |
| 82 | + }), |
| 83 | + headers: { |
| 84 | + 'Access-Control-Allow-Origin': '*', |
| 85 | + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
| 86 | + 'Access-Control-Allow-Headers': 'Content-Type, Authorization', |
| 87 | + }, |
| 88 | + }); |
| 89 | + } else { |
| 90 | + request.continue(); |
| 91 | + } |
| 92 | + }); |
| 93 | + await page.goto(`${SITE_URL}/applications`); |
| 94 | + await page.waitForNetworkIdle(); |
| 95 | + }); |
| 96 | + |
| 97 | + afterEach(async () => { |
| 98 | + await page.close(); |
| 99 | + }); |
| 100 | + |
| 101 | + afterAll(async () => { |
| 102 | + await browser.close(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should render the initial UI elements', async function () { |
| 106 | + const title = await page.$('.header h1'); |
| 107 | + const filterButton = await page.$('.filter-button'); |
| 108 | + const applicationCards = await page.$$('.application-card'); |
| 109 | + expect(title).toBeTruthy(); |
| 110 | + expect(filterButton).toBeTruthy(); |
| 111 | + expect(applicationCards).toBeTruthy(); |
| 112 | + expect(applicationCards.length).toBe(5); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should load and render the accepted application requests when accept is selected from filter, and after clearing the filter it should again show all the applications', async function () { |
| 116 | + await page.click('.filter-button'); |
| 117 | + |
| 118 | + await page.$eval('input[name="status"][value="accepted"]', (radio) => |
| 119 | + radio.click(), |
| 120 | + ); |
| 121 | + await page.click('.apply-filter-button'); |
| 122 | + await page.waitForNetworkIdle(); |
| 123 | + let applicationCards = await page.$$('.application-card'); |
| 124 | + expect(applicationCards.length).toBe(4); |
| 125 | + |
| 126 | + await page.click('.filter-button'); |
| 127 | + await page.click('.clear-btn'); |
| 128 | + |
| 129 | + await page.waitForNetworkIdle(); |
| 130 | + applicationCards = await page.$$('.application-card'); |
| 131 | + expect(applicationCards.length).toBe(5); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should load more applications on going to the bottom of the page', async function () { |
| 135 | + let applicationCards = await page.$$('.application-card'); |
| 136 | + expect(applicationCards.length).toBe(5); |
| 137 | + await page.evaluate(() => { |
| 138 | + const element = document.querySelector('#page_bottom_element'); |
| 139 | + if (element) { |
| 140 | + element.scrollIntoView({ behavior: 'auto' }); |
| 141 | + } |
| 142 | + }); |
| 143 | + await page.waitForNetworkIdle(); |
| 144 | + applicationCards = await page.$$('.application-card'); |
| 145 | + expect(applicationCards.length).toBe(10); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should open application details modal for application, when user click on view details on any card', async function () { |
| 149 | + const applicationDetailsModal = await page.$('.application-details'); |
| 150 | + expect( |
| 151 | + await applicationDetailsModal.evaluate((el) => |
| 152 | + el.classList.contains('hidden'), |
| 153 | + ), |
| 154 | + ).toBe(true); |
| 155 | + await page.click('.view-details-button'); |
| 156 | + expect( |
| 157 | + await applicationDetailsModal.evaluate((el) => |
| 158 | + el.classList.contains('hidden'), |
| 159 | + ), |
| 160 | + ).toBe(false); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should show toast message with application updated successfully', async function () { |
| 164 | + await page.click('.view-details-button'); |
| 165 | + await page.click('.application-details-accept'); |
| 166 | + const toast = await page.$('#toast'); |
| 167 | + expect(await toast.evaluate((el) => el.classList.contains('hidden'))).toBe( |
| 168 | + false, |
| 169 | + ); |
| 170 | + expect(await toast.evaluate((el) => el.innerText)).toBe( |
| 171 | + 'application updated successfully!', |
| 172 | + ); |
| 173 | + await page.waitForNetworkIdle(); |
| 174 | + }); |
| 175 | +}); |
0 commit comments