ApiKeysPage.test.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. };
  17. Object.assign(props, propOverrides);
  18. const wrapper = shallow(<ApiKeysPage {...props} />);
  19. const instance = wrapper.instance() as ApiKeysPage;
  20. return {
  21. wrapper,
  22. instance,
  23. };
  24. };
  25. describe('Render', () => {
  26. it('should render component', () => {
  27. const { wrapper } = setup();
  28. expect(wrapper).toMatchSnapshot();
  29. });
  30. it('should render API keys table', () => {
  31. const { wrapper } = setup({
  32. apiKeys: getMultipleMockKeys(5),
  33. hasFetched: true,
  34. });
  35. expect(wrapper).toMatchSnapshot();
  36. });
  37. });
  38. describe('Life cycle', () => {
  39. it('should call loadApiKeys', () => {
  40. const { instance } = setup();
  41. instance.componentDidMount();
  42. expect(instance.props.loadApiKeys).toHaveBeenCalled();
  43. });
  44. });
  45. describe('Functions', () => {
  46. describe('Delete team', () => {
  47. it('should call delete team', () => {
  48. const { instance } = setup();
  49. instance.onDeleteApiKey(getMockKey());
  50. expect(instance.props.deleteApiKey).toHaveBeenCalledWith(1);
  51. });
  52. });
  53. describe('on search query change', () => {
  54. it('should call setSearchQuery', () => {
  55. const { instance } = setup();
  56. const mockEvent = { target: { value: 'test' } };
  57. instance.onSearchQueryChange(mockEvent);
  58. expect(instance.props.setSearchQuery).toHaveBeenCalledWith('test');
  59. });
  60. });
  61. });