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,19 +2,17 @@ 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"
@ -26,22 +24,18 @@ describe('Messages', () => {
/> />
); );
it('Messages component should render PageLoader', () => { describe('Initial state', () => {
it('renders PageLoader', () => {
expect( expect(
createMessageComponent({ isFetched: false }).find(PageLoader) shallow(setupWrapper({ isFetched: false })).find(PageLoader)
).toBeTruthy(); ).toBeTruthy();
}); });
describe('Messages component with messages', () => {
it('should render string', () => {
const wrapper = createMessageComponent();
expect(wrapper.text()).toContain('No messages at selected topic');
}); });
it('should render JSONTree', () => { describe('Messages table', () => {
expect( describe('With messages', () => {
createMessageComponent({ const messagesWrapper = shallow(
setupWrapper({
messages: [ messages: [
{ {
partition: 1, partition: 1,
@ -50,11 +44,15 @@ describe('Messages', () => {
content: [1, 2, 3], content: [1, 2, 3],
}, },
], ],
}).find(JSONTree) })
).toBeTruthy(); );
it('renders table', () => {
expect(messagesWrapper.find('TimeStamp')).toBeTruthy();
}); });
it('renders JSONTree', () => {
it('JSON.parse should work correctly', () => { expect(messagesWrapper.find(JSONTree)).toBeTruthy();
});
it('parses message content correctly', () => {
const messages = [ const messages = [
{ {
partition: 1, partition: 1,
@ -67,31 +65,21 @@ describe('Messages', () => {
expect(JSON.parse(content)).toEqual(messages[0].content); expect(JSON.parse(content)).toEqual(messages[0].content);
}); });
}); });
describe('Without messages', () => {
it('input of Search field renders properly', () => { it('renders string', () => {
const query = 20; const wrapper = shallow(setupWrapper());
const mockedUseDebouncedCallback = jest.fn(); expect(wrapper.text()).toContain('No messages at selected topic');
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', () => { describe('Offset field', () => {
const wrapper = createMessageComponent(); const wrapper = shallow(setupWrapper());
it('with defined value', () => { describe('With defined offset value', () => {
it('shows offset value in input', () => {
const offset = '10'; const offset = '10';
console.log(wrapper.find(DatePicker));
wrapper wrapper
.find('#searchOffset') .find('#searchOffset')
.simulate('change', { target: { value: offset } }); .simulate('change', { target: { value: offset } });
@ -100,7 +88,9 @@ describe('Messages', () => {
offset offset
); );
}); });
it('with invalid value', () => { });
describe('With invalid offset value', () => {
it('shows 0 in input', () => {
const offset = null; const offset = null;
wrapper wrapper
@ -111,3 +101,23 @@ describe('Messages', () => {
}); });
}); });
}); });
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
.find('#searchText')
.simulate('change', { target: { value: query } });
expect(wrapper.find('#searchText').first().props().value).toEqual(query);
expect(mockedUseDebouncedCallback).toHaveBeenCalledWith({ q: query });
});
});
});