ApiKeysPage.test.tsx 2.1 KB

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