Details.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import React from 'react';
  2. import { Provider } from 'react-redux';
  3. import { shallow, mount } from 'enzyme';
  4. import configureStore from 'redux/store/configureStore';
  5. import { StaticRouter } from 'react-router';
  6. import ClusterContext from 'components/contexts/ClusterContext';
  7. import DetailsContainer from '../DetailsContainer';
  8. import Details, { DetailsProps } from '../Details';
  9. import { schema, versions } from './fixtures';
  10. describe('Details', () => {
  11. describe('Container', () => {
  12. const store = configureStore();
  13. it('renders view', () => {
  14. const component = shallow(
  15. <Provider store={store}>
  16. <DetailsContainer />
  17. </Provider>
  18. );
  19. expect(component.exists()).toBeTruthy();
  20. });
  21. });
  22. describe('View', () => {
  23. const setupWrapper = (props: Partial<DetailsProps> = {}) => (
  24. <Details
  25. schema={schema}
  26. clusterName="Test cluster"
  27. fetchSchemaVersions={jest.fn()}
  28. isFetched
  29. versions={[]}
  30. {...props}
  31. />
  32. );
  33. describe('Initial state', () => {
  34. let useEffect: jest.SpyInstance<
  35. void,
  36. [effect: React.EffectCallback, deps?: React.DependencyList | undefined]
  37. >;
  38. const mockedFn = jest.fn();
  39. const mockedUseEffect = () => {
  40. useEffect.mockImplementationOnce(mockedFn);
  41. };
  42. beforeEach(() => {
  43. useEffect = jest.spyOn(React, 'useEffect');
  44. mockedUseEffect();
  45. shallow(setupWrapper({ fetchSchemaVersions: mockedFn }));
  46. });
  47. it('should call fetchSchemaVersions every render', () => {
  48. expect(mockedFn).toHaveBeenCalled();
  49. });
  50. it('matches snapshot', () => {
  51. expect(
  52. shallow(setupWrapper({ fetchSchemaVersions: mockedFn }))
  53. ).toMatchSnapshot();
  54. });
  55. });
  56. describe('when page with schema versions is loading', () => {
  57. const wrapper = shallow(setupWrapper({ isFetched: false }));
  58. it('renders PageLoader', () => {
  59. expect(wrapper.exists('PageLoader')).toBeTruthy();
  60. });
  61. it('matches snapshot', () => {
  62. expect(shallow(setupWrapper({ isFetched: false }))).toMatchSnapshot();
  63. });
  64. });
  65. describe('when page with schema versions loaded', () => {
  66. describe('when versions are empty', () => {
  67. it('renders table heading without SchemaVersion', () => {
  68. const wrapper = shallow(setupWrapper());
  69. expect(wrapper.exists('LatestVersionItem')).toBeTruthy();
  70. expect(wrapper.exists('button')).toBeTruthy();
  71. expect(wrapper.exists('thead')).toBeTruthy();
  72. expect(wrapper.exists('SchemaVersion')).toBeFalsy();
  73. });
  74. it('matches snapshot', () => {
  75. expect(shallow(setupWrapper())).toMatchSnapshot();
  76. });
  77. });
  78. describe('when schema has versions', () => {
  79. const wrapper = shallow(setupWrapper({ versions }));
  80. it('renders table heading with SchemaVersion', () => {
  81. expect(wrapper.exists('LatestVersionItem')).toBeTruthy();
  82. expect(wrapper.exists('button')).toBeTruthy();
  83. expect(wrapper.exists('thead')).toBeTruthy();
  84. expect(wrapper.find('SchemaVersion').length).toEqual(2);
  85. });
  86. it('matches snapshot', () => {
  87. expect(shallow(setupWrapper({ versions }))).toMatchSnapshot();
  88. });
  89. });
  90. describe('when the readonly flag is set', () => {
  91. it('does not render update & delete buttons', () => {
  92. expect(
  93. mount(
  94. <StaticRouter>
  95. <ClusterContext.Provider value={{ isReadOnly: true }}>
  96. {setupWrapper({ versions })}
  97. </ClusterContext.Provider>
  98. </StaticRouter>
  99. ).exists('.level-right')
  100. ).toBeFalsy();
  101. });
  102. });
  103. });
  104. });
  105. });