Changes made

This commit is contained in:
Guzel738 2021-02-01 14:11:09 +03:00
parent a818168eaa
commit fe1929ebe8
2 changed files with 91 additions and 81 deletions

View file

@ -2,46 +2,40 @@ import React from 'react';
import { mount, shallow } from 'enzyme'; import { mount, shallow } from 'enzyme';
import JSONTree from 'react-json-tree'; import JSONTree from 'react-json-tree';
import * as useDebounce from 'use-debounce'; import * as useDebounce from 'use-debounce';
import DatePicker from 'react-datepicker';
import Messages, { import Messages, {
Props, Props,
} from '../../../../components/Topics/Details/Messages/Messages'; } from '../../../../components/Topics/Details/Messages/Messages';
import PageLoader from '../../../../components/common/PageLoader/PageLoader'; import PageLoader from '../../../../components/common/PageLoader/PageLoader';
describe('Messages', () => { describe('Messages component', () => {
beforeEach(() => { beforeEach(() => {
jest.restoreAllMocks(); jest.restoreAllMocks();
}); });
const createMessageComponent = (props: Partial<Props> = {}) => const setupWrapper = (props: Partial<Props> = {}) => (
shallow( <Messages
<Messages clusterName="Test cluster"
clusterName="Test cluster" topicName="Cluster topic"
topicName="Cluster topic" isFetched
isFetched fetchTopicMessages={jest.fn()}
fetchTopicMessages={jest.fn()} messages={[]}
messages={[]} partitions={[]}
partitions={[]} {...props}
{...props} />
/> );
);
it('Messages component should render PageLoader', () => { describe('Initial state', () => {
expect( it('renders PageLoader', () => {
createMessageComponent({ isFetched: false }).find(PageLoader) expect(
).toBeTruthy(); shallow(setupWrapper({ isFetched: false })).find(PageLoader)
).toBeTruthy();
});
}); });
describe('Messages component with messages', () => { describe('Messages table', () => {
it('should render string', () => { describe('With messages', () => {
const wrapper = createMessageComponent(); const messagesWrapper = shallow(
setupWrapper({
expect(wrapper.text()).toContain('No messages at selected topic');
});
it('should render JSONTree', () => {
expect(
createMessageComponent({
messages: [ messages: [
{ {
partition: 1, partition: 1,
@ -50,64 +44,80 @@ describe('Messages', () => {
content: [1, 2, 3], content: [1, 2, 3],
}, },
], ],
}).find(JSONTree) })
).toBeTruthy();
});
it('JSON.parse should work correctly', () => {
const messages = [
{
partition: 1,
offset: 2,
timestamp: new Date('05-05-1994'),
content: [1, 2, 3],
},
];
const content = JSON.stringify(messages[0].content);
expect(JSON.parse(content)).toEqual(messages[0].content);
});
});
it('input of Search field renders properly', () => {
const query = 20;
const mockedUseDebouncedCallback = jest.fn();
jest
.spyOn(useDebounce, 'useDebouncedCallback')
.mockImplementationOnce(() => [mockedUseDebouncedCallback]);
const wrapper = createMessageComponent();
wrapper
.find('#searchText')
.simulate('change', { target: { value: query } });
expect(wrapper.find('#searchText').first().props().value).toEqual(query);
expect(mockedUseDebouncedCallback).toHaveBeenCalledWith({ q: query });
});
describe('input of Offset field renders properly', () => {
const wrapper = createMessageComponent();
it('with defined value', () => {
const offset = '10';
console.log(wrapper.find(DatePicker));
wrapper
.find('#searchOffset')
.simulate('change', { target: { value: offset } });
expect(wrapper.find('#searchOffset').first().props().value).toEqual(
offset
); );
it('renders table', () => {
expect(messagesWrapper.find('TimeStamp')).toBeTruthy();
});
it('renders JSONTree', () => {
expect(messagesWrapper.find(JSONTree)).toBeTruthy();
});
it('parses message content correctly', () => {
const messages = [
{
partition: 1,
offset: 2,
timestamp: new Date('05-05-1994'),
content: [1, 2, 3],
},
];
const content = JSON.stringify(messages[0].content);
expect(JSON.parse(content)).toEqual(messages[0].content);
});
}); });
it('with invalid value', () => { describe('Without messages', () => {
const offset = null; it('renders string', () => {
const wrapper = shallow(setupWrapper());
expect(wrapper.text()).toContain('No messages at selected topic');
});
});
});
describe('Offset field', () => {
const wrapper = shallow(setupWrapper());
describe('With defined offset value', () => {
it('shows offset value in input', () => {
const offset = '10';
wrapper
.find('#searchOffset')
.simulate('change', { target: { value: offset } });
expect(wrapper.find('#searchOffset').first().props().value).toEqual(
offset
);
});
});
describe('With invalid offset value', () => {
it('shows 0 in input', () => {
const offset = null;
wrapper
.find('#searchOffset')
.simulate('change', { target: { value: offset } });
expect(wrapper.find('#searchOffset').first().props().value).toBe('0');
});
});
});
describe('Search field', () => {
it('renders input correctly', () => {
const query = 20;
const mockedUseDebouncedCallback = jest.fn();
jest
.spyOn(useDebounce, 'useDebouncedCallback')
.mockImplementationOnce(() => [mockedUseDebouncedCallback]);
const wrapper = shallow(setupWrapper());
wrapper wrapper
.find('#searchOffset') .find('#searchText')
.simulate('change', { target: { value: offset } }); .simulate('change', { target: { value: query } });
expect(wrapper.find('#searchOffset').first().props().value).toBe('0'); expect(wrapper.find('#searchText').first().props().value).toEqual(query);
expect(mockedUseDebouncedCallback).toHaveBeenCalledWith({ q: query });
}); });
}); });
}); });