Skip to content

Commit 6fb1d12

Browse files
workshurwhotake
andauthored
[CHORE] Specs and Test env improvements (#198)
* [CHORE] Update dependencies * [CHORE] Configure Test env. Add enzyme-to-json snapshot serializer * [CHORE] Specs for Topic Messages component * [CHORE] Mock Date-fns * [CHORE] Refactor keys * Run related tests on commit * [CHORE] Stub date Co-authored-by: Azat Mutigullin <[email protected]>
1 parent 6ec5163 commit 6fb1d12

File tree

18 files changed

+1313
-840
lines changed

18 files changed

+1313
-840
lines changed

kafka-ui-react-app/.eslintrc.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
},
1616
"ecmaVersion": 2018,
1717
"sourceType": "module",
18-
"project": ["./tsconfig.json", "./src/setupTests.ts"]
18+
"project": [
19+
"./tsconfig.json",
20+
"./src/setupTests.ts"
21+
]
1922
},
2023
"plugins": ["@typescript-eslint", "prettier"],
2124
"extends": [
@@ -30,7 +33,8 @@
3033
"@typescript-eslint/explicit-module-boundary-types": "off",
3134
"jsx-a11y/label-has-associated-control": "off",
3235
"import/prefer-default-export": "off",
33-
"@typescript-eslint/no-explicit-any": "error"
36+
"@typescript-eslint/no-explicit-any": "error",
37+
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }]
3438
},
3539
"overrides": [
3640
{

kafka-ui-react-app/jest.config.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

kafka-ui-react-app/package-lock.json

Lines changed: 809 additions & 629 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kafka-ui-react-app/package.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@types/react-datepicker": "^3.1.1",
7-
"@types/uuid": "^8.3.0",
86
"bulma": "^0.8.2",
97
"bulma-switch": "^2.0.0",
108
"classnames": "^2.2.6",
@@ -33,7 +31,7 @@
3331
"*.{js,ts,jsx,tsx}": [
3432
"eslint -c .eslintrc.json --fix",
3533
"git add",
36-
"jest --bail --findRelatedTests"
34+
"npm test -- --bail --findRelatedTests --watchAll=false"
3735
]
3836
},
3937
"scripts": {
@@ -42,6 +40,7 @@
4240
"lint": "eslint --ext .tsx,.ts src/",
4341
"lint:fix": "eslint --ext .tsx,.ts src/ --fix",
4442
"test": "react-scripts test",
43+
"test:CI": "CI=true npm test --watchAll=false",
4544
"eject": "react-scripts eject",
4645
"tsc": "tsc"
4746
},
@@ -66,6 +65,7 @@
6665
]
6766
},
6867
"devDependencies": {
68+
"@jest/types": "^26.6.2",
6969
"@testing-library/jest-dom": "^5.11.9",
7070
"@testing-library/react": "^9.5.0",
7171
"@testing-library/user-event": "^7.1.2",
@@ -75,16 +75,19 @@
7575
"@types/lodash": "^4.14.165",
7676
"@types/node": "^12.19.8",
7777
"@types/react": "^17.0.0",
78+
"@types/react-datepicker": "^3.1.1",
7879
"@types/react-dom": "^17.0.0",
7980
"@types/react-redux": "^7.1.11",
8081
"@types/react-router-dom": "^5.1.6",
8182
"@types/redux": "^3.6.0",
8283
"@types/redux-thunk": "^2.1.0",
84+
"@types/uuid": "^8.3.0",
8385
"@typescript-eslint/eslint-plugin": "^4.9.0",
8486
"@typescript-eslint/parser": "^4.9.0",
8587
"@wojtekmaj/enzyme-adapter-react-17": "^0.4.1",
8688
"dotenv": "^8.2.0",
8789
"enzyme": "^3.11.0",
90+
"enzyme-to-json": "^3.6.1",
8891
"eslint": "^7.14.0",
8992
"eslint-config-airbnb": "^18.2.1",
9093
"eslint-config-airbnb-typescript": "^12.0.0",
@@ -99,12 +102,16 @@
99102
"lint-staged": "^10.5.2",
100103
"node-sass": "^4.14.1",
101104
"prettier": "^2.2.1",
102-
"react-scripts": "4.0.1",
105+
"react-scripts": "4.0.2",
103106
"ts-jest": "^26.4.4",
107+
"ts-node": "^9.1.1",
104108
"typescript": "~4.1.2"
105109
},
106110
"engines": {
107111
"node": ">=14.15.4"
108112
},
109-
"proxy": "http://localhost:8080"
113+
"proxy": "http://localhost:8080",
114+
"jest": {
115+
"snapshotSerializers": ["enzyme-to-json/serializer"]
116+
}
110117
}

kafka-ui-react-app/src/components/Topics/Details/Messages/MessageItem.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { format } from 'date-fns';
33
import JSONTree from 'react-json-tree';
44
import { TopicMessage } from 'generated-sources';
55

6-
interface MessageItemProp {
6+
export interface MessageItemProp {
77
partition: TopicMessage['partition'];
88
offset: TopicMessage['offset'];
99
timestamp: TopicMessage['timestamp'];
10-
content: TopicMessage['content'];
10+
content?: TopicMessage['content'];
1111
}
1212

1313
const MessageItem: React.FC<MessageItemProp> = ({
@@ -16,13 +16,11 @@ const MessageItem: React.FC<MessageItemProp> = ({
1616
timestamp,
1717
content,
1818
}) => (
19-
<tr key="{timestamp}">
20-
<td style={{ width: 200 }}>
21-
{timestamp ? format(timestamp, 'yyyy-MM-dd HH:mm:ss') : null}
22-
</td>
19+
<tr>
20+
<td style={{ width: 200 }}>{format(timestamp, 'yyyy-MM-dd HH:mm:ss')}</td>
2321
<td style={{ width: 150 }}>{offset}</td>
2422
<td style={{ width: 100 }}>{partition}</td>
25-
<td key="{content}" style={{ wordBreak: 'break-word' }}>
23+
<td style={{ wordBreak: 'break-word' }}>
2624
{content && (
2725
<JSONTree
2826
data={content}

kafka-ui-react-app/src/components/Topics/Details/Messages/Messages.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import 'react-datepicker/dist/react-datepicker.css';
12
import React, { useCallback, useEffect, useRef } from 'react';
3+
import { groupBy, map, concat, maxBy } from 'lodash';
4+
import MultiSelect from 'react-multi-select-component';
5+
import { Option } from 'react-multi-select-component/dist/lib/interfaces';
6+
import { useDebouncedCallback } from 'use-debounce';
27
import {
38
ClusterName,
49
TopicMessageQueryParams,
@@ -7,13 +12,6 @@ import {
712
import { TopicMessage, Partition, SeekType } from 'generated-sources';
813
import PageLoader from 'components/common/PageLoader/PageLoader';
914
import DatePicker from 'react-datepicker';
10-
import 'react-datepicker/dist/react-datepicker.css';
11-
12-
import MultiSelect from 'react-multi-select-component';
13-
14-
import * as _ from 'lodash';
15-
import { useDebouncedCallback } from 'use-debounce';
16-
import { Option } from 'react-multi-select-component/dist/lib/interfaces';
1715
import MessagesTable from './MessagesTable';
1816

1917
export interface Props {
@@ -81,17 +79,17 @@ const Messages: React.FC<Props> = ({
8179
offset: 0,
8280
partition: p.partition,
8381
}));
84-
const messageUniqs: FilterProps[] = _.map(
85-
_.groupBy(messages, 'partition'),
86-
(v) => _.maxBy(v, 'offset')
82+
const messageUniqs: FilterProps[] = map(
83+
groupBy(messages, 'partition'),
84+
(v) => maxBy(v, 'offset')
8785
).map((v) => ({
8886
offset: v ? v.offset : 0,
8987
partition: v ? v.partition : 0,
9088
}));
9189

92-
return _.map(
93-
_.groupBy(_.concat(partitionUniqs, messageUniqs), 'partition'),
94-
(v) => _.maxBy(v, 'offset') as FilterProps
90+
return map(
91+
groupBy(concat(partitionUniqs, messageUniqs), 'partition'),
92+
(v) => maxBy(v, 'offset') as FilterProps
9593
);
9694
}, [messages, partitions]);
9795

kafka-ui-react-app/src/components/Topics/Details/Messages/MessagesContainer.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { connect } from 'react-redux';
2-
import {
3-
ClusterName,
4-
RootState,
5-
TopicMessageQueryParams,
6-
TopicName,
7-
} from 'redux/interfaces';
2+
import { ClusterName, RootState, TopicName } from 'redux/interfaces';
83
import { RouteComponentProps, withRouter } from 'react-router-dom';
94
import { fetchTopicMessages } from 'redux/actions';
105
import {
@@ -38,11 +33,7 @@ const mapStateToProps = (
3833
});
3934

4035
const mapDispatchToProps = {
41-
fetchTopicMessages: (
42-
clusterName: ClusterName,
43-
topicName: TopicName,
44-
queryParams: Partial<TopicMessageQueryParams>
45-
) => fetchTopicMessages(clusterName, topicName, queryParams),
36+
fetchTopicMessages,
4637
};
4738

4839
export default withRouter(

kafka-ui-react-app/src/components/Topics/Details/Messages/MessagesTable.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TopicMessage } from 'generated-sources';
33
import CustomParamButton from 'components/Topics/shared/Form/CustomParams/CustomParamButton';
44
import MessageItem from './MessageItem';
55

6-
interface MessagesTableProp {
6+
export interface MessagesTableProp {
77
messages: TopicMessage[];
88
onNext(event: React.MouseEvent<HTMLButtonElement>): void;
99
}
@@ -14,7 +14,7 @@ const MessagesTable: React.FC<MessagesTableProp> = ({ messages, onNext }) => {
1414
}
1515

1616
return (
17-
<div>
17+
<>
1818
<table className="table is-striped is-fullwidth">
1919
<thead>
2020
<tr>
@@ -28,7 +28,7 @@ const MessagesTable: React.FC<MessagesTableProp> = ({ messages, onNext }) => {
2828
{messages.map(
2929
({ partition, offset, timestamp, content }: TopicMessage) => (
3030
<MessageItem
31-
key={timestamp.toString()}
31+
key={`message-${timestamp.getTime()}`}
3232
partition={partition}
3333
offset={offset}
3434
timestamp={timestamp}
@@ -48,7 +48,7 @@ const MessagesTable: React.FC<MessagesTableProp> = ({ messages, onNext }) => {
4848
/>
4949
</div>
5050
</div>
51-
</div>
51+
</>
5252
);
5353
};
5454

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import { shallow } from 'enzyme';
3+
import MessageItem from 'components/Topics/Details/Messages/MessageItem';
4+
import { messages } from './fixtures';
5+
6+
jest.mock('date-fns', () => ({
7+
format: () => `mocked date`,
8+
}));
9+
10+
describe('MessageItem', () => {
11+
describe('when content is defined', () => {
12+
it('renders table row with JSONTree', () => {
13+
const wrapper = shallow(<MessageItem {...messages[0]} />);
14+
15+
expect(wrapper.find('tr').length).toEqual(1);
16+
expect(wrapper.find('td').length).toEqual(4);
17+
expect(wrapper.find('JSONTree').length).toEqual(1);
18+
});
19+
20+
it('matches snapshot', () => {
21+
expect(shallow(<MessageItem {...messages[0]} />)).toMatchSnapshot();
22+
});
23+
});
24+
25+
describe('when content is undefined', () => {
26+
it('renders table row without JSONTree', () => {
27+
const wrapper = shallow(<MessageItem {...messages[1]} />);
28+
29+
expect(wrapper.find('tr').length).toEqual(1);
30+
expect(wrapper.find('td').length).toEqual(4);
31+
expect(wrapper.find('JSONTree').length).toEqual(0);
32+
});
33+
34+
it('matches snapshot', () => {
35+
expect(shallow(<MessageItem {...messages[1]} />)).toMatchSnapshot();
36+
});
37+
});
38+
});

0 commit comments

Comments
 (0)