Details.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. subject={schema.subject}
  26. schema={schema}
  27. clusterName="Test cluster"
  28. fetchSchemaVersions={jest.fn()}
  29. isFetched
  30. versions={[]}
  31. {...props}
  32. />
  33. );
  34. describe('Initial state', () => {
  35. let useEffect: jest.SpyInstance<
  36. void,
  37. [effect: React.EffectCallback, deps?: React.DependencyList | undefined]
  38. >;
  39. const mockedFn = jest.fn();
  40. const mockedUseEffect = () => {
  41. useEffect.mockImplementationOnce(mockedFn);
  42. };
  43. beforeEach(() => {
  44. useEffect = jest.spyOn(React, 'useEffect');
  45. mockedUseEffect();
  46. shallow(setupWrapper({ fetchSchemaVersions: mockedFn }));
  47. });
  48. it('should call fetchSchemaVersions every render', () => {
  49. expect(mockedFn).toHaveBeenCalled();
  50. });
  51. it('matches snapshot', () => {
  52. expect(
  53. shallow(setupWrapper({ fetchSchemaVersions: mockedFn }))
  54. ).toMatchSnapshot();
  55. });
  56. });
  57. describe('when page with schema versions is loading', () => {
  58. const wrapper = shallow(setupWrapper({ isFetched: false }));
  59. it('renders PageLoader', () => {
  60. expect(wrapper.exists('PageLoader')).toBeTruthy();
  61. });
  62. it('matches snapshot', () => {
  63. expect(shallow(setupWrapper({ isFetched: false }))).toMatchSnapshot();
  64. });
  65. });
  66. describe('when page with schema versions loaded', () => {
  67. describe('when versions are empty', () => {
  68. it('renders table heading without SchemaVersion', () => {
  69. const wrapper = shallow(setupWrapper());
  70. expect(wrapper.exists('LatestVersionItem')).toBeTruthy();
  71. expect(wrapper.exists('button')).toBeTruthy();
  72. expect(wrapper.exists('thead')).toBeTruthy();
  73. expect(wrapper.exists('SchemaVersion')).toBeFalsy();
  74. });
  75. it('matches snapshot', () => {
  76. expect(shallow(setupWrapper())).toMatchSnapshot();
  77. });
  78. });
  79. describe('when schema has versions', () => {
  80. const wrapper = shallow(setupWrapper({ versions }));
  81. it('renders table heading with SchemaVersion', () => {
  82. expect(wrapper.exists('LatestVersionItem')).toBeTruthy();
  83. expect(wrapper.exists('button')).toBeTruthy();
  84. expect(wrapper.exists('thead')).toBeTruthy();
  85. expect(wrapper.find('SchemaVersion').length).toEqual(2);
  86. });
  87. it('matches snapshot', () => {
  88. expect(shallow(setupWrapper({ versions }))).toMatchSnapshot();
  89. });
  90. });
  91. describe('when the readonly flag is set', () => {
  92. it('does not render update & delete buttons', () => {
  93. expect(
  94. mount(
  95. <StaticRouter>
  96. <ClusterContext.Provider value={{ isReadOnly: true }}>
  97. {setupWrapper({ versions })}
  98. </ClusterContext.Provider>
  99. </StaticRouter>
  100. ).exists('.level-right')
  101. ).toBeFalsy();
  102. });
  103. });
  104. });
  105. });
  106. });