-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f233a5
commit 05fe5a7
Showing
7 changed files
with
188 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// @ts-nocheck | ||
import React from 'react'; | ||
import ReactDOM, { unmountComponentAtNode } from 'react-dom'; | ||
import { DateTime } from 'luxon'; | ||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
import BookingDrawer from './BookingDrawer'; | ||
import UserDrawer from './UserDrawer'; | ||
|
||
const fakeRoom = { | ||
id: '123', | ||
name: 'Amor', | ||
building: 'Hermia 5', | ||
capacity: 15, | ||
features: ['TV', 'Whiteboard'], | ||
nextCalendarEvent: DateTime.now().plus({ minutes: 30 }).toUTC().toISO(), | ||
email: '[email protected]' | ||
}; | ||
let container = null; | ||
|
||
describe('BookingDrawer', () => { | ||
beforeEach(() => { | ||
// Setup a DOM element as a render target | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
// Cleanup on exiting | ||
container.remove(); | ||
container = null; | ||
}); | ||
|
||
it('Shows logout button', async () => { | ||
const userMock = jest.fn(); | ||
render( | ||
<UserDrawer | ||
open={true} | ||
toggle={jest.fn()} | ||
bookRoom={userMock} | ||
room={fakeRoom} | ||
duration={15} | ||
additionalDuration={0} | ||
availableMinutes={30} | ||
onAddTime={jest.fn()} | ||
startingTime={'Now'} | ||
/>, | ||
container | ||
); | ||
|
||
const bookButton = screen.queryByTestId('BookNowButton'); | ||
await waitFor(() => expect(bookButton).toBeTruthy()); | ||
|
||
fireEvent.click(bookButton); | ||
expect(userMock).toBeCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import React, { | ||
Dispatch, | ||
PropsWithChildren, | ||
ProviderProps, | ||
useState | ||
} from 'react'; | ||
import { logout } from '../services/authService'; | ||
import useCreateNotification from '../hooks/useCreateNotification'; | ||
import { useHistory } from 'react-router-dom'; | ||
|
||
export interface UserSettingsContextProps { | ||
showUserSettingsMenu: boolean; | ||
setShowUserSettingsMenu: (show: boolean) => void; | ||
expandedFeaturesAll: boolean; | ||
setExpandedFeaturesAll: (expand: boolean) => void; | ||
doLogout: () => void; | ||
} | ||
|
||
export const userSettingsContextDefaults = { | ||
showUserSettingsMenu: false, | ||
expandedFeaturesAll: false | ||
}; | ||
export const UserSettingsContext = React.createContext< | ||
Partial<UserSettingsContextProps> | ||
>({ | ||
...userSettingsContextDefaults | ||
}); | ||
|
||
export const UserSettingsProvider = ( | ||
props: PropsWithChildren<ProviderProps<Partial<UserSettingsContextProps>>> | ||
) => { | ||
const { createSuccessNotification, createErrorNotification } = | ||
useCreateNotification(); | ||
const history = useHistory(); | ||
const doLogout = () => { | ||
logout() | ||
.then(() => { | ||
createSuccessNotification('Logout successful'); | ||
history.push('/login'); | ||
}) | ||
.catch(() => { | ||
createErrorNotification('Error in logout, try again later'); | ||
history.push('/login'); | ||
}); | ||
}; | ||
|
||
const [showUserSettingsMenu, setShowUserSettingsMenu] = React.useState( | ||
props.value?.showUserSettingsMenu || false | ||
); | ||
const [expandedFeaturesAll, setExpandedFeaturesAll] = useState( | ||
false as boolean | ||
); | ||
|
||
const ctx = { | ||
showUserSettingsMenu, | ||
setShowUserSettingsMenu, | ||
doLogout, | ||
expandedFeaturesAll, | ||
setExpandedFeaturesAll | ||
}; | ||
|
||
return ( | ||
<UserSettingsContext.Provider | ||
value={{ ...userSettingsContextDefaults, ...props.value, ...ctx }} | ||
> | ||
{props.children} | ||
</UserSettingsContext.Provider> | ||
); | ||
}; | ||
|
||
export const useUserSettings = (): UserSettingsContextProps => { | ||
const ctx = React.useContext(UserSettingsContext); | ||
return ctx as UserSettingsContextProps; | ||
}; |