Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<SideDrawer> testing with React Router #251

Merged
merged 3 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/BaseRoutesMock.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@ const ColorModeContext = createContext({toggleColorMode: () => {}})
* @return {Object} React component
*/
export function MockRoutes({contentElt}) {
return (
<MockElement>
<MemoryRouter>
<Routes>
<Route path="/*" element={contentElt}/>
</Routes>
</MemoryRouter>
</MockElement>
)
}

export const MockElement = ({children}) => {
const {theme, colorMode} = useTheme()

return (
<ColorModeContext.Provider value={colorMode}>
<ThemeProvider theme={theme}>
<MemoryRouter>
<Routes>
<Route path="/*" element={contentElt} />
</Routes>
</MemoryRouter>
{children}
</ThemeProvider>
</ColorModeContext.Provider>
)
Expand Down
6 changes: 4 additions & 2 deletions src/Components/SideDrawer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {useIsMobile} from './Hooks'
import MobileDrawer from './MobileDrawer'
import {PropertiesPanel, NotesPanel} from './SideDrawerPanels'
import {getHashParams} from '../utils/location'
import {useLocation} from 'react-router-dom'

/**
* SideDrawer contains the ItemPanel and CommentPanel and allows for
Expand Down Expand Up @@ -85,16 +86,17 @@ export default function SideDrawerWrapper() {
const openDrawer = useStore((state) => state.openDrawer)
const toggleIsCommentsOn = useStore((state) => state.toggleIsCommentsOn)
const setSelectedIssueId = useStore((state) => state.setSelectedIssueId)
const location = useLocation()

useEffect(() => {
const issueHash = getHashParams(window.location, 'i')
const issueHash = getHashParams(location, 'i')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oogali nice!

if (issueHash !== undefined) {
const extractedCommentId = issueHash.split(':')[1]
setSelectedIssueId(Number(extractedCommentId))
openDrawer()
toggleIsCommentsOn()
}
}, [])
}, [location])

return (
<>
Expand Down
43 changes: 22 additions & 21 deletions src/Components/SideDrawer.test.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react'
import {render, screen} from '@testing-library/react'
import {MockRoutes, MOCK_SELECTED_ELEMENT} from '../BaseRoutesMock.test'
import {render, screen, waitFor} from '@testing-library/react'
import {MockRoutes, MOCK_SELECTED_ELEMENT, MockElement} from '../BaseRoutesMock.test'
import SideDrawerWrapper from './SideDrawer'
import {act, renderHook} from '@testing-library/react-hooks'
import useStore from '../store/useStore'
// import {addHashParams, getHashParams, ISSUE_PREFIX} from '../utils/location'
import {MemoryRouter} from 'react-router-dom'


test('side drawer notes', () => {
Expand Down Expand Up @@ -58,24 +58,25 @@ test('side drawer - issues id in url', () => {
})


// test('side drawer - issues id in url', () => {
// const {result} = renderHook(() => useStore((state) => state))

// // rying to set the url -- but don't think it is working
// addHashParams(window.location, ISSUE_PREFIX, {id: '1257156364'})
// // I am not sure if getHashParams is working
// const issueHash = getHashParams(window.location, 'i')
test('side drawer - opened via URL', async () => {
const {result} = renderHook(() => useStore((state) => state))

// // the previous test is testing the rest of the process
// const extractedCommentId = issueHash.split(':')[1]
// act(() => {
// result.current.setSelectedIssueId(Number(extractedCommentId))
// result.current.toggleIsCommentsOn()
// result.current.openDrawer()
// })
// render(<MockRoutes contentElt={<SideDrawerWrapper/>}/>)
// expect(screen.getByText('Note')).toBeInTheDocument()
// expect(screen.getByText('BLDRS-LOCAL_MODE-ID:1257156364')).toBeInTheDocument()
// })
const {getByText} = render(
<MemoryRouter initialEntries={['/v/p/index.ifc#i:1257156364::c:-26.91,28.84,112.47,-22,16.21,-3.48']}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<MockElement>
<SideDrawerWrapper />
</MockElement>
</MemoryRouter>,
)

await waitFor(() => {
expect(getByText('Note')).toBeInTheDocument()
expect(getByText('BLDRS-LOCAL_MODE-ID:1257156364')).toBeInTheDocument()
})

// reset the store
act(() => {
result.current.setSelectedElement({})
result.current.toggleIsCommentsOn()
})
})