浏览代码

Tests for Messages component

Guzel738 4 年之前
父节点
当前提交
79033de3ea

文件差异内容过多而无法显示
+ 237 - 646
kafka-ui-react-app/package-lock.json


+ 2 - 1
kafka-ui-react-app/package.json

@@ -45,7 +45,8 @@
     "lint:fix": "eslint --ext .tsx,.ts src/ --fix",
     "lint:fix": "eslint --ext .tsx,.ts src/ --fix",
     "test": "react-scripts test",
     "test": "react-scripts test",
     "eject": "react-scripts eject",
     "eject": "react-scripts eject",
-    "tsc": "tsc"
+    "tsc": "tsc",
+    "test:coverage": "jest --coverage"
   },
   },
   "husky": {
   "husky": {
     "hooks": {
     "hooks": {

+ 85 - 16
kafka-ui-react-app/src/tests/Topics/Details/Messages/Messages.spec.tsx

@@ -1,15 +1,20 @@
 import React from 'react';
 import React from 'react';
-import { shallow, mount } 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 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';
-// import { messages } from './MessagesTx';
 
 
 describe('Messages', () => {
 describe('Messages', () => {
+  beforeEach(() => {
+    jest.restoreAllMocks();
+  });
+
   const createMessageComponent = (props: Partial<Props> = {}) =>
   const createMessageComponent = (props: Partial<Props> = {}) =>
-    mount(
+    shallow(
       <Messages
       <Messages
         clusterName="Test cluster"
         clusterName="Test cluster"
         topicName="Cluster topic"
         topicName="Cluster topic"
@@ -21,24 +26,88 @@ describe('Messages', () => {
       />
       />
     );
     );
 
 
-  describe('Messages component initial', () => {
-    it('renders properly', () => {
-      expect(
-        createMessageComponent({ isFetched: false }).find(PageLoader)
-      ).toBeTruthy();
-    });
+  it('Messages component should render PageLoader', () => {
+    expect(
+      createMessageComponent({ isFetched: false }).find(PageLoader)
+    ).toBeTruthy();
   });
   });
 
 
-  describe('Messages component renders MessagesTable', () => {
-    it('MessagesTable renders properly', () => {
+  describe('Messages component with messages', () => {
+    it('should render string', () => {
       const wrapper = createMessageComponent();
       const wrapper = createMessageComponent();
+
       expect(wrapper.text()).toContain('No messages at selected topic');
       expect(wrapper.text()).toContain('No messages at selected topic');
     });
     });
+
+    it('should render JSONTree', () => {
+      expect(
+        createMessageComponent({
+          messages: [
+            {
+              partition: 1,
+              offset: 2,
+              timestamp: new Date('05-05-1994'),
+              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);
+    });
   });
   });
 
 
-  // describe('Message content renders', () => {
-  //   it('Message content renders properly', () => {
-  //     expect(createMessageComponent({ messages }).find(JSONTree));
-  //   });
-  // });
+  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('with invalid value', () => {
+      const offset = null;
+
+      wrapper
+        .find('#searchOffset')
+        .simulate('change', { target: { value: offset } });
+
+      expect(wrapper.find('#searchOffset').first().props().value).toBe('0');
+    });
+  });
 });
 });

+ 0 - 9
kafka-ui-react-app/src/tests/Topics/Details/Messages/MessagesTx.ts

@@ -1,9 +0,0 @@
-import React from 'react';
-import { TopicMessage } from 'generated-sources';
-
-export const messages: TopicMessage = {
-  partition: 1,
-  offset: 2,
-  timestamp: new Date('05-05-1994'),
-  content: [1, 2, 3],
-};

部分文件因为文件数量过多而无法显示