ApiKeysPage.test.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React from 'react';
  2. import { shallow } from 'enzyme';
  3. import { Props, ApiKeysPage } from './ApiKeysPage';
  4. import { NavModel, ApiKey } from 'app/types';
  5. import { getMultipleMockKeys, getMockKey } from './__mocks__/apiKeysMock';
  6. const setup = (propOverrides?: object) => {
  7. const props: Props = {
  8. navModel: {
  9. main: {
  10. text: 'Configuration',
  11. },
  12. node: {
  13. text: 'Api Keys',
  14. },
  15. } as NavModel,
  16. apiKeys: [] as ApiKey[],
  17. searchQuery: '',
  18. hasFetched: false,
  19. loadApiKeys: jest.fn(),
  20. deleteApiKey: jest.fn(),
  21. setSearchQuery: jest.fn(),
  22. addApiKey: jest.fn(),
  23. apiKeysCount: 0,
  24. };
  25. Object.assign(props, propOverrides);
  26. const wrapper = shallow(<ApiKeysPage {...props} />);
  27. const instance = wrapper.instance() as ApiKeysPage;
  28. return {
  29. wrapper,
  30. instance,
  31. };
  32. };
  33. describe('Render', () => {
  34. it('should render API keys table if there are any keys', () => {
  35. const { wrapper } = setup({
  36. apiKeys: getMultipleMockKeys(5),
  37. apiKeysCount: 5,
  38. });
  39. expect(wrapper).toMatchSnapshot();
  40. });
  41. it('should render CTA if there are no API keys', () => {
  42. const { wrapper } = setup({
  43. apiKeys: getMultipleMockKeys(0),
  44. apiKeysCount: 0,
  45. hasFetched: true,
  46. });
  47. expect(wrapper).toMatchSnapshot();
  48. });
  49. });
  50. describe('Life cycle', () => {
  51. it('should call loadApiKeys', () => {
  52. const { instance } = setup();
  53. instance.componentDidMount();
  54. expect(instance.props.loadApiKeys).toHaveBeenCalled();
  55. });
  56. });
  57. describe('Functions', () => {
  58. describe('Delete team', () => {
  59. it('should call delete team', () => {
  60. const { instance } = setup();
  61. instance.onDeleteApiKey(getMockKey());
  62. expect(instance.props.deleteApiKey).toHaveBeenCalledWith(1);
  63. });
  64. });
  65. describe('on search query change', () => {
  66. it('should call setSearchQuery', () => {
  67. const { instance } = setup();
  68. instance.onSearchQueryChange('test');
  69. expect(instance.props.setSearchQuery).toHaveBeenCalledWith('test');
  70. });
  71. });
  72. });