App.spec.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React from 'react';
  2. import { mount, shallow } from 'enzyme';
  3. import { Provider } from 'react-redux';
  4. import { StaticRouter } from 'react-router-dom';
  5. import configureStore from 'redux/store/configureStore';
  6. import App, { AppProps } from '../App';
  7. const fetchClustersList = jest.fn();
  8. const store = configureStore();
  9. describe('App', () => {
  10. const setupComponent = (props: Partial<AppProps> = {}) => (
  11. <App
  12. isClusterListFetched
  13. alerts={[]}
  14. fetchClustersList={fetchClustersList}
  15. {...props}
  16. />
  17. );
  18. it('handles fetchClustersList', () => {
  19. const wrapper = mount(
  20. <Provider store={store}>
  21. <StaticRouter>{setupComponent()}</StaticRouter>
  22. </Provider>
  23. );
  24. expect(wrapper.exists()).toBeTruthy();
  25. expect(fetchClustersList).toHaveBeenCalledTimes(1);
  26. });
  27. it('shows PageLoader until cluster list is fetched', () => {
  28. const component = shallow(setupComponent({ isClusterListFetched: false }));
  29. expect(component.exists('.Layout__container PageLoader')).toBeTruthy();
  30. expect(component.exists('.Layout__container Switch')).toBeFalsy();
  31. component.setProps({ isClusterListFetched: true });
  32. expect(component.exists('.Layout__container PageLoader')).toBeFalsy();
  33. expect(component.exists('.Layout__container Switch')).toBeTruthy();
  34. });
  35. it('correctly renders alerts', () => {
  36. const alert = {
  37. id: 'alert-id',
  38. type: 'success',
  39. title: 'My Custom Title',
  40. message: 'My Custom Message',
  41. createdAt: 1234567890,
  42. };
  43. const wrapper = shallow(setupComponent());
  44. expect(wrapper.exists('.Layout__alerts')).toBeTruthy();
  45. expect(wrapper.exists('Alert')).toBeFalsy();
  46. wrapper.setProps({ alerts: [alert] });
  47. expect(wrapper.exists('Alert')).toBeTruthy();
  48. expect(wrapper.find('Alert').length).toEqual(1);
  49. });
  50. it('matches snapshot', () => {
  51. const component = shallow(setupComponent());
  52. expect(component).toMatchSnapshot();
  53. });
  54. });