added Overlay component

This commit is contained in:
Wenszel 2023-12-11 11:52:06 +01:00
parent e9cb2e4de9
commit aa6c03ebcb
8 changed files with 708 additions and 40 deletions

640
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,6 @@
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@mui/material": "^5.14.20",
"@testing-library/jest-dom": "^6.1.5",
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "^14.5.1",
"axios": "^1.6.2",
"prop-types": "^15.8.1",
@ -50,5 +48,9 @@
"last 1 safari version"
]
},
"proxy": "http://localhost:5000"
"proxy": "http://localhost:5000",
"devDependencies": {
"@testing-library/jest-dom": "^6.1.5",
"@testing-library/react": "^14.1.2"
}
}

View File

@ -7,6 +7,7 @@
border: 1px solid white;
border-radius: 8px;
margin: 20px;
z-index: 2;
}
.name-input-container > button {
margin-top: 5px;
@ -17,18 +18,6 @@
.name-input-container > input {
margin-top: 10px;
}
.name-overlay {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1;
}
input,
button {
padding: 0;

View File

@ -22,19 +22,17 @@ const NameInput = ({ isRoomPrivate, roomId }) => {
}, [socket]);
return (
<div className='name-overlay'>
<div className='name-input-container' style={{ height: isRoomPrivate ? '100px' : '50px' }}>
<input placeholder='Nickname' type='text' onChange={nickname.onChange} />
{isRoomPrivate ? (
<input
placeholder='Room password'
type='text'
onChange={password.onChange}
style={{ backgroundColor: isPasswordWrong ? 'red' : null }}
/>
) : null}
<button onClick={handleButtonClick}>JOIN</button>
</div>
<div className='name-input-container' style={{ height: isRoomPrivate ? '100px' : '50px' }}>
<input placeholder='Nickname' type='text' onChange={nickname.onChange} />
{isRoomPrivate ? (
<input
placeholder='Room password'
type='text'
onChange={password.onChange}
style={{ backgroundColor: isPasswordWrong ? 'red' : null }}
/>
) : null}
<button onClick={handleButtonClick}>JOIN</button>
</div>
);
};

View File

@ -6,6 +6,7 @@ import ReactLoading from 'react-loading';
import './ServerList.css';
import NameInput from '../NameInput/NameInput';
import Overlay from '../../Overlay/Overlay';
const ServerList = () => {
const socket = useContext(SocketContext);
@ -70,7 +71,11 @@ const ServerList = () => {
</div>
)}
</div>
{joining ? <NameInput roomId={clickedRoom._id} isRoomPrivate={clickedRoom.private} /> : null}
{joining ? (
<Overlay handleOverlayClose={() => setJoining(false)}>
<NameInput roomId={clickedRoom._id} isRoomPrivate={clickedRoom.private} />
</Overlay>
) : null}
</div>
);
};

View File

@ -0,0 +1,9 @@
import styles from './Overlay.module.css';
import useKeyPress from '../../hooks/useKeyPress';
const Overlay = ({ children, handleOverlayClose }) => {
useKeyPress('Escape', handleOverlayClose);
return <div className={styles.container}>{children}</div>;
};
export default Overlay;

View File

@ -0,0 +1,13 @@
.container {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1;
cursor: pointer;
}

View File

@ -0,0 +1,34 @@
import '@testing-library/jest-dom';
import React from 'react';
import { render } from '@testing-library/react';
import Overlay from './Overlay';
import userEvent from '@testing-library/user-event';
describe('Overlay component', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('renders Overlay component', () => {
const { container } = render(<Overlay handleOverlayClose={() => {}} />);
expect(container).toBeInTheDocument();
});
it('renders children inside Overlay', () => {
const { getByTestId } = render(
<Overlay handleOverlayClose={() => {}}>
<div data-testid='test-child' />
</Overlay>
);
expect(getByTestId('test-child')).toBeInTheDocument();
});
it('calls handleOverlayClose on Escape key press', async () => {
const handleOverlayCloseMock = jest.fn();
render(<Overlay handleOverlayClose={handleOverlayCloseMock} />);
await userEvent.type(document.body, '{Escape}');
expect(handleOverlayCloseMock).toHaveBeenCalled();
});
});