shlink-web-client/test/app/App.test.tsx

81 lines
2.3 KiB
TypeScript
Raw Normal View History

import { shallow, ShallowWrapper } from 'enzyme';
2018-08-26 20:48:21 +03:00
import { Route } from 'react-router-dom';
import { Mock } from 'ts-mockery';
import { match } from 'react-router';
import { History, Location } from 'history';
2021-09-19 11:31:53 +03:00
import { Settings } from '../../src/settings/reducers/settings';
import appFactory from '../../src/app/App';
import { AppUpdateBanner } from '../../src/common/AppUpdateBanner';
2018-08-26 20:48:21 +03:00
describe('<App />', () => {
let wrapper: ShallowWrapper;
const MainHeader = () => null;
2021-06-06 19:54:55 +03:00
const ShlinkVersions = () => null;
const App = appFactory(
MainHeader,
() => null,
() => null,
() => null,
() => null,
() => null,
() => null,
ShlinkVersions,
);
const createWrapper = (pathname = '') => {
2021-06-06 19:54:55 +03:00
wrapper = shallow(
<App
fetchServers={() => {}}
servers={{}}
settings={Mock.all<Settings>()}
appUpdated={false}
resetAppUpdate={() => {}}
match={Mock.all<match>()}
history={Mock.all<History>()}
location={Mock.of<Location>({ pathname })}
2021-06-06 19:54:55 +03:00
/>,
);
2018-08-26 20:48:21 +03:00
return wrapper;
};
2018-08-26 20:48:21 +03:00
afterEach(() => wrapper.unmount());
2021-06-06 19:54:55 +03:00
it('renders children components', () => {
const wrapper = createWrapper();
expect(wrapper.find(MainHeader)).toHaveLength(1);
expect(wrapper.find(ShlinkVersions)).toHaveLength(1);
expect(wrapper.find(AppUpdateBanner)).toHaveLength(1);
});
2021-06-06 19:54:55 +03:00
2018-08-26 20:48:21 +03:00
it('renders app main routes', () => {
const wrapper = createWrapper();
2018-08-26 20:48:21 +03:00
const routes = wrapper.find(Route);
2019-03-03 13:02:29 +03:00
const expectedPaths = [
'/',
'/settings',
2021-10-17 20:13:06 +03:00
'/manage-servers',
2020-03-15 11:56:16 +03:00
'/server/create',
'/server/:serverId/edit',
2019-03-03 13:02:29 +03:00
'/server/:serverId',
];
2018-08-26 20:48:21 +03:00
2019-03-03 13:02:29 +03:00
expect.assertions(expectedPaths.length + 1);
2020-03-15 11:56:16 +03:00
expect(routes).toHaveLength(expectedPaths.length + 1);
2019-03-03 13:02:29 +03:00
expectedPaths.forEach((path, index) => {
expect(routes.at(index).prop('path')).toEqual(path);
});
2018-08-26 20:48:21 +03:00
});
it.each([
[ '/foo', 'shlink-wrapper' ],
[ '/bar', 'shlink-wrapper' ],
[ '/', 'shlink-wrapper d-flex d-md-block align-items-center' ],
])('renders expected classes on shlink-wrapper based on current pathname', (pathname, expectedClasses) => {
const wrapper = createWrapper(pathname);
const shlinkWrapper = wrapper.find('.shlink-wrapper');
expect(shlinkWrapper.prop('className')).toEqual(expectedClasses);
});
2018-08-26 20:48:21 +03:00
});