ApiKeysPage.test.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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: {} as NavModel,
  9. apiKeys: [] as ApiKey[],
  10. searchQuery: '',
  11. hasFetched: false,
  12. loadApiKeys: jest.fn(),
  13. deleteApiKey: jest.fn(),
  14. setSearchQuery: jest.fn(),
  15. addApiKey: jest.fn(),
  16. apiKeysCount: 0,
  17. };
  18. Object.assign(props, propOverrides);
  19. const wrapper = shallow(<ApiKeysPage {...props} />);
  20. const instance = wrapper.instance() as ApiKeysPage;
  21. return {
  22. wrapper,
  23. instance,
  24. };
  25. };
  26. describe('Render', () => {
  27. it('should render API keys table if there are any keys', () => {
  28. const { wrapper } = setup({
  29. apiKeys: getMultipleMockKeys(5),
  30. apiKeysCount: 5,
  31. });
  32. expect(wrapper).toMatchSnapshot();
  33. });
  34. it('should render CTA if there are no API keys', () => {
  35. const { wrapper } = setup({
  36. apiKeys: getMultipleMockKeys(0),
  37. apiKeysCount: 0,
  38. hasFetched: true,
  39. });
  40. expect(wrapper).toMatchSnapshot();
  41. });
  42. });
  43. describe('Life cycle', () => {
  44. it('should call loadApiKeys', () => {
  45. const { instance } = setup();
  46. instance.componentDidMount();
  47. expect(instance.props.loadApiKeys).toHaveBeenCalled();
  48. });
  49. });
  50. describe('Functions', () => {
  51. describe('Delete team', () => {
  52. it('should call delete team', () => {
  53. const { instance } = setup();
  54. instance.onDeleteApiKey(getMockKey());
  55. expect(instance.props.deleteApiKey).toHaveBeenCalledWith(1);
  56. });
  57. });
  58. describe('on search query change', () => {
  59. it('should call setSearchQuery', () => {
  60. const { instance } = setup();
  61. const mockEvent = { target: { value: 'test' } };
  62. instance.onSearchQueryChange(mockEvent);
  63. expect(instance.props.setSearchQuery).toHaveBeenCalledWith('test');
  64. });
  65. });
  66. });