Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 92 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/__tests__/app.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './test_suites/DisplayUsers.test'
import './test_suites/ChangeStatus.test'
51 changes: 51 additions & 0 deletions src/__tests__/setup.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { afterEach } from 'vitest'
import { cleanup } from '@testing-library/react'
import '@testing-library/jest-dom/vitest'
import fetch from 'node-fetch';

global.fetch = fetch

global.baseUsers = [
{
"id": 1,
"name": "theGrey",
"profile_image": "./images/theGrey.jpg",
"status": "Online"
},
{
"id": 2,
"name": "fr0d0",
"profile_image": "./images/fr0d0.jpg",
"status": "Away"
},
{
"id": 3,
"name": "bor0mir123",
"profile_image": "./images/bor0mir123.jpg",
"status": "Offline"
},
{
"id": 4,
"name": "elf-friend",
"profile_image": "./images/elf-friend.jpg",
"status": "Online"
},
{
"id": 5,
"name": "legolas",
"profile_image": "./images/legolas.jpg",
"status": "Online"
}
]

global.setFetchResponse = (val) => {
global.fetch = vi.fn(() => Promise.resolve({
json: () => Promise.resolve(val),
ok: true,
status: 200
}))
}

afterEach(() => {
cleanup();
})
27 changes: 27 additions & 0 deletions src/__tests__/test_suites/ChangeStatus.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import App from '../../components/App';
import '@testing-library/jest-dom';

describe('Our App will', () => {
test('edit the status of the user', async () => {
global.setFetchResponse(global.baseUsers)
const { findAllByTestId } = render(<App />)
const statusButtons = await findAllByTestId('status-item')
global.setFetchResponse({id: 1,name: 'theGrey', profile_image: './images/theGrey.webp', "status": "Offline"})
fireEvent.click(statusButtons[0])
expect(fetch).toHaveBeenCalledWith("http://localhost:6001/users/1", {
method: "patch",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({id: 1,name: 'theGrey', profile_image: './images/theGrey.jpg', "status": "Offline"}),
})
const profiles = await findAllByTestId('user-item')
const changedProfile = profiles[0]
const status = changedProfile.querySelector("p").textContent
await waitFor(() => {
expect(status).toBe("Offline");
});
});
})
40 changes: 40 additions & 0 deletions src/__tests__/test_suites/DisplayUsers.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { render } from '@testing-library/react';
import App from '../../components/App';
import '@testing-library/jest-dom';

describe('Our app will', () => {
test('displays all users on startup', async () => {
global.setFetchResponse(global.baseUsers);

const { findAllByTestId } = render(<App />);
const userItems = await findAllByTestId('user-item');

// Check count
expect(userItems).toHaveLength(global.baseUsers.length);

// Check names
const userNames = userItems.map((item) =>
item.querySelector('h4').textContent
);
const baseUsersNames = global.baseUsers.map((user) => user.name);
expect(userNames).toEqual(baseUsersNames);

// ✅ Fix selector and split logic
const userProfileImage = userItems.map((item) => {
const imageEl = item.querySelector('.inline-block-child.profile-image');
return imageEl ? imageEl.src.split('/').pop() : null;
});
const baseUserProfileImages = global.baseUsers.map((user) =>
user.profile_image.split('/').pop()
);
expect(userProfileImage).toEqual(baseUserProfileImages);

// Check statuses
const userStatus = userItems.map((item) =>
item.querySelector('p').textContent
);
const baseUserStatus = global.baseUsers.map((user) => user.status);
expect(userStatus).toEqual(baseUserStatus);
});
});
27 changes: 26 additions & 1 deletion src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,34 @@ import UserList from './UserList'
function App() {
const [users, setUsers] = useState([])

useEffect(()=>{
fetch('http://localhost:6001/users')
.then(r=>r.json())
.then(data=>setUsers(data))
},[])

function changeStatus(updatedUser){
fetch(`http://localhost:6001/users/${updatedUser.id}`,{
method: "patch",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(updatedUser),
}
)
.then((r) => r.json())
.then((data) => {
setUsers((prevUsers) =>
prevUsers.map((user) =>
user.id === updatedUser.id ? data : user
)
);
});
}

return (
<div>
<UserList users={users}/>
<UserList users={users} changeStatus={changeStatus}/>
</div>
)
}
Expand Down
Loading