grafana/public/app/features/api-keys/ApiKeysPage.test.tsx

82 lines
2.0 KiB
TypeScript
Raw Normal View History

2018-10-10 18:17:00 -05:00
import React from 'react';
2018-09-26 07:58:27 -05:00
import { shallow } from 'enzyme';
import { Props, ApiKeysPage } from './ApiKeysPage';
import { NavModel, ApiKey } from 'app/types';
import { getMultipleMockKeys, getMockKey } from './__mocks__/apiKeysMock';
const setup = (propOverrides?: object) => {
const props: Props = {
navModel: {} as NavModel,
apiKeys: [] as ApiKey[],
searchQuery: '',
2018-10-11 04:49:34 -05:00
hasFetched: false,
2018-09-26 07:58:27 -05:00
loadApiKeys: jest.fn(),
deleteApiKey: jest.fn(),
setSearchQuery: jest.fn(),
addApiKey: jest.fn(),
2018-10-10 18:17:00 -05:00
apiKeysCount: 0,
2018-09-26 07:58:27 -05:00
};
Object.assign(props, propOverrides);
const wrapper = shallow(<ApiKeysPage {...props} />);
const instance = wrapper.instance() as ApiKeysPage;
return {
wrapper,
instance,
};
};
describe('Render', () => {
2018-10-10 18:17:00 -05:00
it('should render API keys table if there are any keys', () => {
const { wrapper } = setup({
apiKeys: getMultipleMockKeys(5),
apiKeysCount: 5,
});
2018-09-26 07:58:27 -05:00
expect(wrapper).toMatchSnapshot();
});
it('should render CTA if there are no API keys', () => {
2018-09-26 07:58:27 -05:00
const { wrapper } = setup({
2018-10-10 18:17:00 -05:00
apiKeys: getMultipleMockKeys(0),
apiKeysCount: 0,
hasFetched: true,
2018-09-26 07:58:27 -05:00
});
expect(wrapper).toMatchSnapshot();
});
});
describe('Life cycle', () => {
it('should call loadApiKeys', () => {
const { instance } = setup();
instance.componentDidMount();
expect(instance.props.loadApiKeys).toHaveBeenCalled();
});
});
describe('Functions', () => {
describe('Delete team', () => {
it('should call delete team', () => {
const { instance } = setup();
instance.onDeleteApiKey(getMockKey());
expect(instance.props.deleteApiKey).toHaveBeenCalledWith(1);
});
});
describe('on search query change', () => {
it('should call setSearchQuery', () => {
const { instance } = setup();
const mockEvent = { target: { value: 'test' } };
instance.onSearchQueryChange(mockEvent);
expect(instance.props.setSearchQuery).toHaveBeenCalledWith('test');
});
});
});