generated from RealDevSquad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathApp.test.js
More file actions
150 lines (119 loc) · 4.54 KB
/
App.test.js
File metadata and controls
150 lines (119 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const puppeteer = require('puppeteer');
const { filteredUsersData } = require('../../mock-data/users');
const { mockUserData } = require('../../mock-data/users/mockdata');
const API_BASE_URL = 'https://staging-api.realdevsquad.com';
describe('App Component', () => {
let browser;
let page;
jest.setTimeout(60000);
let config = {
launchOptions: {
headless: 'new',
ignoreHTTPSErrors: true,
args: ['--incognito', '--disable-web-security'],
},
};
const BASE_URL = 'http://localhost:8000';
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
};
beforeAll(async () => {
browser = await puppeteer.launch(config.launchOptions);
page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', (interceptedRequest) => {
const url = interceptedRequest.url();
if (url === `${API_BASE_URL}/users/search/?role=in_discord`) {
interceptedRequest.respond({
status: 200,
contentType: 'application/json',
headers,
body: JSON.stringify({
...filteredUsersData,
users: filteredUsersData.users.filter(
(user) => user.roles.in_discord,
),
}),
});
} else if (url === `${API_BASE_URL}/users/search/?verified=true`) {
interceptedRequest.respond({
status: 200,
contentType: 'application/json',
headers,
body: JSON.stringify({
...filteredUsersData,
users: filteredUsersData.users.filter((user) => user.discordId),
...mockUserData,
users: mockUserData.users.filter((user) => user.discordId),
}),
});
} else {
interceptedRequest.continue();
}
});
await page.goto(`${BASE_URL}/users/discord/`); // Replace with your app's URL
await page.waitForNetworkIdle();
});
afterAll(async () => {
await browser.close();
});
it('should render all sections', async () => {
let tabsSection = await page.$('.tabs_section');
let usersSection = await page.$('.users_section');
let firstUser = await page.$('.user_card');
let userDetailsSection = await page.$('.user_details_section');
expect(tabsSection).toBeDefined();
const tabs = await tabsSection.$$('.tab');
expect(tabs.length).toEqual(2);
expect(usersSection).toBeDefined();
expect(userDetailsSection).toBeDefined();
});
it('should update the URL query string and re-render the app', async () => {
// Click on the "Linked Accounts" tab
await page.click('[data_key="verified"]');
// Get the current URL and make sure the query string has been updated
const url = await page.url();
expect(url).toContain('?tab=verified');
});
it('should update the URL query string on search', async () => {
const initialUrl = await page.url();
await page.waitForSelector('.search_field');
await page.type('.search_field', 'John Doe');
await page.click('.search_button');
const updatedUrl = await page.url();
expect(updatedUrl).toContain('search=John+Doe');
});
it('should display user details when a user card is clicked', async () => {
await page.waitForSelector('.active_tab');
await page.click('.active_tab');
await page.waitForSelector('.user_details_section');
const userDetailsDisplayed =
(await page.$('.user_details_section')) !== null;
expect(userDetailsDisplayed).toBeTruthy();
});
it('should display search results matching the search term', async () => {
await page.type('.search_field', 'shu');
await page.click('.search_button');
await page.waitForSelector('.user_card');
const userCards = await page.$$('.user_card');
let searchTermFound = false;
for (const card of userCards) {
const cardContent = await card.evaluate((node) => node.innerText);
if (cardContent.toLowerCase().includes('shubham')) {
searchTermFound = true;
break;
}
}
expect(searchTermFound).toBeTruthy();
});
it('should handle empty search results gracefully', async () => {
await page.type('.search_field', 'bdhsbhj'); //represents a string which won't yeild any search result
await page.click('.search_button');
await page.waitForSelector('.no_user_found');
const emptyResultsMessageDisplayed =
(await page.$('.no_user_found')) !== null;
expect(emptyResultsMessageDisplayed).toBeTruthy();
});
});