Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
feat(#104): finished App component test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Gasser committed Apr 17, 2019
1 parent 5223f0f commit 9d500bb
Show file tree
Hide file tree
Showing 5 changed files with 8,868 additions and 46 deletions.
58 changes: 12 additions & 46 deletions src/app/NotFound.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import View from '../ui/View';
import { Colors } from '../styles';
import * as Paths from '../paths';

const timer = 5;

const StyledNotFound = styled(View)`
margin-top: 2rem;
Expand All @@ -25,50 +23,18 @@ const StyledNotFound = styled(View)`
}
`;

const NotFound = (props) => {
const [counter, setCounter] = useState(0);

// handle timeout
useEffect(() => {
const timeout = setTimeout(() => {
setCounter(counter + 1);
}, 1000);

if (counter >= timer) {
// move away
props.history.push(Paths.HOME);
}

return () => {
clearTimeout(timeout);
};
});

// move to index after 5 seconds
const showCounter = (timer, counter) => {
const diff = timer - counter;

if (diff <= 0) {
return 'now';
}

return `${diff} seconds`;
}

return (
<StyledNotFound>
<Heading level={1}>
<p>
<strong>Whhooppss</strong>
{' '}
it seems there is no page for route:
<code>{props.location.pathname}</code>
</p>
<p>Forward to home page in: {showCounter(timer, counter)}.</p>
</Heading>
</StyledNotFound>
);
}
const NotFound = ({ location }) => (
<StyledNotFound>
<Heading level={1}>
<p>
<strong>Whhooppss</strong>
{' '}
it seems there is no page for route:
<code>{location.pathname}</code>
</p>
</Heading>
</StyledNotFound>
);

NotFound.propTypes = {
location: PropTypes.shape({
Expand Down
30 changes: 30 additions & 0 deletions src/app/__tests__/NotFound.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* global testUtils */
import React from 'react';
import { act } from 'react-dom/test-utils';
import toJson from 'enzyme-to-json';
import 'jest-styled-components';

import { mount } from 'enzyme';

import NotFound from '../NotFound';

describe('NotFound test suite', () => {
const initialProps = {
location: {
pathname: '/pathname',
},
};

const getNotFound = (props) => mount(
<NotFound
{...initialProps}
/>
);

it('should render NotFound', () => {
const wrapper = getNotFound();
expect(wrapper).toBeTruthy();

expect(toJson(wrapper)).toMatchSnapshot();
});
});
22 changes: 22 additions & 0 deletions src/app/__tests__/Privacy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import toJson from 'enzyme-to-json';
import 'jest-styled-components';

import { MemoryRouter } from 'react-router-dom';

import { mount } from 'enzyme';

import Privacy from '../Privacy';

describe('Privacy test suite', () => {
const getPrivacy = (props) => mount(
<MemoryRouter>
<Privacy />
</MemoryRouter>
);

it('should render', () => {
const wrapper = getPrivacy();
expect(wrapper).toBeTruthy();
});
});
47 changes: 47 additions & 0 deletions src/app/__tests__/PrivateRoute.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* global testUtils */
import React from 'react';
import * as ReactRouterDom from 'react-router-dom';

import { mount } from 'enzyme';

import PrivateRoute from '../PrivateRoute';

describe('PrivateRoute test suite', () => {
let reactRouterDomRedirectMock;
beforeAll(() => {
// orgRedirect = Redirect
reactRouterDomRedirectMock = jest.spyOn(ReactRouterDom, 'Redirect')
.mockImplementation(() => <p>redirect</p>);
});

afterAll(() => {
reactRouterDomRedirectMock.resetMock();
});

const DummyComponent = () => <h1>Test Heading</h1>;

const getPrivateRoute = (props) => mount(
<ReactRouterDom.MemoryRouter>
<PrivateRoute {...props} />
</ReactRouterDom.MemoryRouter>
);

const initialProps = {
component: DummyComponent,
isAuthenticated: true,
};

it('should render component if authenticated', () => {
const wrapper = getPrivateRoute(initialProps);
const component = mount(<DummyComponent />);
expect(wrapper.html()).toEqual(component.html());
});

it('should render component if not authenticated', () => {
const wrapper = getPrivateRoute({
...initialProps,
isAuthenticated: false,
});
expect(wrapper.html()).toEqual('<p>redirect</p>');
});
});
Loading

0 comments on commit 9d500bb

Please sign in to comment.