Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Feb 3, 2023
1 parent 44ce186 commit 9968182
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 23 deletions.
1 change: 0 additions & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"file-saver": "^2.0.5",
"lodash": "^4.17.21",
"prop-types": "^15.8.1",
"query-string": "^8.1.0",
"react": "^18.2.0",
"react-bootstrap": "^2.7.0",
"react-bootstrap-typeahead": "^6.0.0",
Expand Down
20 changes: 7 additions & 13 deletions ui/src/app/core/components/ProtectRoute.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { BrowserRouter, MemoryRouter, Route, Routes, useNavigate } from 'react-router-dom';
import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom';
import { ProtectRoute } from './ProtectRoute';

const mockIsAdmin = jest.fn();
Expand All @@ -10,21 +10,13 @@ jest.mock('../user/UserContext', () => ({
useCurrentUser: () => ({role: 'ROLE_ADMIN'}),
}));

const renderWithRouter = (ui) => {
return render(
<MemoryRouter initialEntries={['/dashboard']}>
{ui}
</MemoryRouter>
)
}

describe('AdminRoute user is admin', () => {
beforeEach(() => {
mockIsAdmin.mockReturnValue(true);
});

it('should render the component if user is an admin', () => {
render(<ProtectRoute redirectTo={"/dashboard"}><React.Fragment>hi there</React.Fragment></ProtectRoute>, { wrapper: MemoryRouter });
render(<ProtectRoute redirectTo={"/dashboard"}><React.Fragment>hi there</React.Fragment></ProtectRoute>, { wrapper: BrowserRouter });
expect(screen.getByText('hi there')).toBeInTheDocument();
});
});
Expand All @@ -35,9 +27,11 @@ describe('AdminRoute user is NOT admin', () => {
});

it('should redirect the user to the dashboard if not admin', () => {
renderWithRouter(<Routes>
<Route path="/dashboard" element={ <ProtectRoute redirectTo={'/dashboard'}><React.Fragment>hi there</React.Fragment></ProtectRoute> } />
</Routes>, {route: '/foo'});
render(<Routes>
<Route path="/" element={<Navigate to="/dashboard"/> } />
<Route path="/dashboard" element={ <React.Fragment>dashboard</React.Fragment> } />
<Route path="/admin" element={ <ProtectRoute redirectTo={'/dashboard'}><React.Fragment>hi there</React.Fragment></ProtectRoute> } />
</Routes>, { wrapper: BrowserRouter });
expect(screen.getByText('dashboard')).toBeInTheDocument();
});
});
2 changes: 0 additions & 2 deletions ui/src/app/core/hooks/usePrompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export function usePrompt(when, message, {
let blocker = useBlocker(when);
const [show, setShow] = React.useState(false);

console.log(blocker);

React.useEffect(() => {
if (blocker.state === "blocked" && !when) {
blocker.reset();
Expand Down
6 changes: 3 additions & 3 deletions ui/src/app/form/component/fields/FilterTargetField.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { useTranslator } from '../../../i18n/hooks';
import { InfoIcon } from '../InfoIcon';
import { AsyncTypeahead } from 'react-bootstrap-typeahead';
import useFetch from 'use-http';
import queryString from 'query-string';
import API_BASE_PATH from '../../../App.constant';
import isNil from 'lodash/isNil';
import capitalize from 'lodash/capitalize';
Expand All @@ -19,6 +18,7 @@ import Editor from 'react-simple-code-editor';

import { FilterTargetPreview } from '../../../metadata/hoc/FilterTargetPreview';
import { remove } from 'lodash';
import { createSearchParams } from 'react-router-dom';

const ToggleButton = ({ isOpen, onClick, disabled }) => (
<Button
Expand Down Expand Up @@ -78,10 +78,10 @@ const FilterTargetField = ({
});

async function searchIds (query) {
const { entityIds } = get(`?${queryString.stringify({
const { entityIds } = get(`?${createSearchParams({
term: query,
limit: 10
})}`)
}).toString()}`)
if (response.ok) {
setSearchIds(entityIds);
}
Expand Down
7 changes: 3 additions & 4 deletions ui/src/app/metadata/view/MetadataHistory.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { createSearchParams, useNavigate, useParams } from 'react-router-dom';
import { Link } from 'react-router-dom';
import queryString from 'query-string';
import Button from 'react-bootstrap/Button';
import FormattedDate from '../../core/components/FormattedDate';

Expand Down Expand Up @@ -39,9 +38,9 @@ export function MetadataHistory () {
};
const compare = (versions) => {
const s = sortVersionsByDate(versions);
const path = `/metadata/${type}/${id}/configuration/compare?${queryString.stringify({versions: s.map(s => s.id)}, {
const path = `/metadata/${type}/${id}/configuration/compare?${createSearchParams({versions: s.map(s => s.id)}, {
skipNull: true,
})}`;
}).toString()}`;
navigate(path);
};

Expand Down

0 comments on commit 9968182

Please sign in to comment.