ApiKeysPage.test.tsx 1.8 KB

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