Skip to content

Commit

Permalink
Merge pull request #175 from Thiht/quality
Browse files Browse the repository at this point in the history
refact: remove sonar errors + code smells
  • Loading branch information
gwleclerc authored Dec 10, 2020
2 parents 2d56915 + c6664db commit 2dd859b
Show file tree
Hide file tree
Showing 25 changed files with 126 additions and 124 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ default: start

REFLEX=$(GOPATH)/bin/reflex
$(REFLEX):
go get github.com/cespare/reflex
cd /tmp; go get github.com/cespare/reflex

GOLANGCILINTVERSION:=1.32.0
GOLANGCILINT=$(GOPATH)/bin/golangci-lint
Expand All @@ -51,7 +51,7 @@ $(GOCOVMERGE):

CADDY=$(GOPATH)/bin/caddy
$(CADDY):
go get github.com/caddyserver/caddy/v2/...
cd /tmp; go get github.com/caddyserver/caddy/v2/...

.PHONY: start
start: $(REFLEX)
Expand Down
47 changes: 24 additions & 23 deletions client/components/History.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import useLocalStorage from "react-use-localstorage";
import { Dispatch } from "redux";
import { Actions, actions } from "~modules/actions";
import { AppState } from "~modules/reducers";
import { dateFormat, Entry, Error, History } from "~modules/types";
import { dateFormat, Entry, History, SmockerError } from "~modules/types";
import {
cleanupRequest,
cleanupResponse,
Expand All @@ -36,7 +36,14 @@ import {
import Code from "./Code";
import "./History.scss";

const Entry = React.memo(
const TableRow = ([key, values]: [string, string[]]) => (
<tr key={key}>
<td>{key}</td>
<td>{values.join(", ")}</td>
</tr>
);

const EntryComponent = React.memo(
({
value,
handleDisplayNewMock,
Expand Down Expand Up @@ -72,20 +79,17 @@ const Entry = React.memo(
{value.request.headers && (
<table>
<tbody>
{Object.entries(value.request.headers).map(([key, values]) => (
<tr key={key}>
<td>{key}</td>
<td>{values.join(", ")}</td>
</tr>
))}
{Object.entries(value.request.headers).map((entry) =>
TableRow(entry)
)}
</tbody>
</table>
)}
{value.request.body && (
<Code
value={
JSON.stringify(value.request.body, null, " ") ||
value.request.body
`${value.request.body}`
}
language="json"
/>
Expand All @@ -104,9 +108,9 @@ const Entry = React.memo(
<Typography.Text
className="error"
ellipsis
title={value.response.body.message}
title={(value.response.body as SmockerError).message}
>
{value.response.body.message}
{(value.response.body as SmockerError).message}
</Typography.Text>
)}
{value.context.mock_id && (
Expand All @@ -133,20 +137,17 @@ const Entry = React.memo(
{value.response.headers && (
<table>
<tbody>
{Object.entries(value.response.headers).map(([key, values]) => (
<tr key={key}>
<td>{key}</td>
<td>{values.join(", ")}</td>
</tr>
))}
{Object.entries(value.response.headers).map((entry) =>
TableRow(entry)
)}
</tbody>
</table>
)}
{value.response.body && (
<Code
value={
JSON.stringify(value.response.body, null, " ") ||
value.response.body
`${value.response.body}`
}
language="json"
/>
Expand All @@ -161,19 +162,19 @@ const Entry = React.memo(
);
}
);
Entry.displayName = "Entry";
EntryComponent.displayName = "Entry";

interface Props {
sessionID: string;
loading: boolean;
canPoll: boolean;
historyEntry: History;
error: Error | null;
error: SmockerError | null;
fetch: (sessionID: string) => unknown;
setDisplayNewMock: (display: boolean, defaultValue: string) => unknown;
}

const History = ({
const HistoryComponent = ({
sessionID,
historyEntry,
loading,
Expand Down Expand Up @@ -260,7 +261,7 @@ const History = ({
<>
{pagination}
{entries.map((entry, index) => (
<Entry
<EntryComponent
key={`entry-${index}`}
value={entry}
handleDisplayNewMock={handleDisplayNewMock(entry)}
Expand Down Expand Up @@ -349,4 +350,4 @@ export default connect(
setDisplayNewMock: (display: boolean, defaultValue: string) =>
dispatch(actions.openMockEditor([display, defaultValue])),
})
)(History);
)(HistoryComponent);
32 changes: 16 additions & 16 deletions client/components/Mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import { AppState } from "~modules/reducers";
import {
dateFormat,
defaultMatcher,
Error,
Mock,
MockDynamicResponse,
MockRequest,
MockResponse,
Mocks,
SmockerError,
StringMatcher,
StringMatcherMap,
} from "~modules/types";
Expand Down Expand Up @@ -64,7 +64,7 @@ const renderTimes = (count: number, expected?: number) => {

const emptyResponse: unknown = {};

const MockResponse = ({ mock }: { mock: Mock }) => {
const MockResponseComponent = ({ mock }: { mock: Mock }) => {
const { response: resp, context, state } = mock;
const response = resp ? resp : (emptyResponse as MockResponse);

Expand Down Expand Up @@ -93,7 +93,7 @@ const MockResponse = ({ mock }: { mock: Mock }) => {
);
};

const MockDynamicResponse = ({ mock }: { mock: Mock }) => {
const MockDynamicResponseComponent = ({ mock }: { mock: Mock }) => {
const { dynamic_response, context, state } = mock;
const response = dynamic_response
? dynamic_response
Expand All @@ -115,7 +115,7 @@ const MockDynamicResponse = ({ mock }: { mock: Mock }) => {
);
};

const MockProxy = ({ mock }: { mock: Mock }) => {
const MockProxyComponent = ({ mock }: { mock: Mock }) => {
const { proxy, context, state } = mock;
const host = proxy ? proxy.host : "";
return (
Expand All @@ -133,7 +133,7 @@ const MockProxy = ({ mock }: { mock: Mock }) => {
);
};

const MockRequest = ({ request }: { request: MockRequest }) => {
const MockRequestComponent = ({ request }: { request: MockRequest }) => {
const showMethodMatcher = request.method.matcher !== defaultMatcher;
const showPathMatcher = request.path.matcher !== defaultMatcher;
const isBodyStringMatcher = isStringMatcher(request.body);
Expand Down Expand Up @@ -197,7 +197,7 @@ const MockRequest = ({ request }: { request: MockRequest }) => {
);
};

const Mock = ({
const MockComponent = ({
mock,
canPoll,
loading,
Expand Down Expand Up @@ -244,16 +244,16 @@ const Mock = ({
</span>
</div>
<div className="content">
<MockRequest request={mock.request} />
{mock.response && <MockResponse mock={mock} />}
{mock.dynamic_response && <MockDynamicResponse mock={mock} />}
{mock.proxy && <MockProxy mock={mock} />}
<MockRequestComponent request={mock.request} />
{mock.response && <MockResponseComponent mock={mock} />}
{mock.dynamic_response && <MockDynamicResponseComponent mock={mock} />}
{mock.proxy && <MockProxyComponent mock={mock} />}
</div>
</div>
);
};

const NewMock = ({
const NewMockComponent = ({
display,
defaultValue,
onSave,
Expand Down Expand Up @@ -309,15 +309,15 @@ interface Props {
canPoll: boolean;
mocks: Mocks;
mockEditor: [boolean, string];
error: Error | null;
error: SmockerError | null;
fetch: (sessionID: string) => unknown;
addMocks: (mocks: string) => unknown;
lockMock: (mockID: string) => unknown;
unlockMock: (mockID: string) => unknown;
setDisplayNewMock: (display: boolean, defaultValue: string) => unknown;
}

const Mocks = ({
const MocksComponent = ({
sessionID,
loading,
canPoll,
Expand Down Expand Up @@ -392,7 +392,7 @@ const Mocks = ({
<>
{pagination}
{paginatedMocks.map((mock) => (
<Mock
<MockComponent
key={`mock-${mock.state.id}`}
mock={mock}
canPoll={canPoll}
Expand Down Expand Up @@ -454,7 +454,7 @@ const Mocks = ({
</Spin>
</PageHeader>
{displayNewMock && (
<NewMock
<NewMockComponent
display={displayNewMock}
defaultValue={mockEditor[1]}
onSave={handleSaveNewMock}
Expand Down Expand Up @@ -490,4 +490,4 @@ export default connect(
setDisplayNewMock: (display: boolean, defaultValue: string) =>
dispatch(actions.openMockEditor([display, defaultValue])),
})
)(Mocks);
)(MocksComponent);
8 changes: 4 additions & 4 deletions client/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const Navbar = (): JSX.Element => {
>
<Menu.Item key="/pages/history">
<Link
to={(location) => ({
...cleanQueryParams(location),
to={(loc) => ({
...cleanQueryParams(loc),
pathname: "/pages/history",
})}
>
Expand All @@ -33,8 +33,8 @@ const Navbar = (): JSX.Element => {
</Menu.Item>
<Menu.Item key="/pages/mocks">
<Link
to={(location) => ({
...cleanQueryParams(location),
to={(loc) => ({
...cleanQueryParams(loc),
pathname: "/pages/mocks",
})}
>
Expand Down
4 changes: 2 additions & 2 deletions client/components/Visualize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ const Visualize = ({ sessionID, graph, loading, visualize }: Props) => {
const handleEditGraph = () => setEditGraph(true);
const handleChangeGraph = (diag: string) => setDiagram(diag);
const handleCloseEditGraph = () => setEditGraph(false);
const handleChangeSVG = (svg: string) => {
setSVG(svg);
const handleChangeSVG = (content: string) => {
setSVG(content);
};

const onSaveSVG = () => {
Expand Down
28 changes: 16 additions & 12 deletions client/modules/actions.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { ActionType, createAction, createAsyncAction } from "typesafe-actions";
import {
Error,
GraphHistory,
History,
Mocks,
Session,
Sessions,
SmockerError,
} from "./types";

const fetchSessions = createAsyncAction(
"@APP/SESSIONS/FETCH",
"@APP/SESSIONS/FETCH/SUCCESS",
"@APP/SESSIONS/FETCH/FAILURE"
)<void, Sessions, Error>();
)<void, Sessions, SmockerError>();

const newSession = createAsyncAction(
"@APP/SESSIONS/NEW",
"@APP/SESSIONS/NEW/SUCCESS",
"@APP/SESSIONS/NEW/FAILURE"
)<void, Session, Error>();
)<void, Session, SmockerError>();

const updateSession = createAsyncAction(
"@APP/SESSIONS/UPDATE",
"@APP/SESSIONS/UPDATE/SUCCESS",
"@APP/SESSIONS/UPDATE/FAILURE"
)<Session, Session, Error>();
)<Session, Session, SmockerError>();

const uploadSessions = createAsyncAction(
"@APP/SESSIONS/UPLOAD",
"@APP/SESSIONS/UPLOAD/SUCCESS",
"@APP/SESSIONS/UPLOAD/FAILURE"
)<Sessions, Sessions, Error>();
)<Sessions, Sessions, SmockerError>();

const selectSession = createAction("@APP/SESSIONS/SELECT")<string>();

Expand All @@ -42,19 +42,23 @@ const fetchHistory = createAsyncAction(
"@APP/HISTORY/FETCH",
"@APP/HISTORY/FETCH/SUCCESS",
"@APP/HISTORY/FETCH/FAILURE"
)<string, History, Error>();
)<string, History, SmockerError>();

const summarizeHistory = createAsyncAction(
"@APP/HISTORY/SUMMARIZE",
"@APP/HISTORY/SUMMARIZE/SUCCESS",
"@APP/HISTORY/SUMMARIZE/FAILURE"
)<{ sessionID: string; src: string; dest: string }, GraphHistory, Error>();
)<
{ sessionID: string; src: string; dest: string },
GraphHistory,
SmockerError
>();

const fetchMocks = createAsyncAction(
"@APP/MOCKS/FETCH",
"@APP/MOCKS/FETCH/SUCCESS",
"@APP/MOCKS/FETCH/FAILURE"
)<string, Mocks, Error>();
)<string, Mocks, SmockerError>();

export interface NewMocks {
mocks: string;
Expand All @@ -64,25 +68,25 @@ const addMocks = createAsyncAction(
"@APP/MOCKS/ADD",
"@APP/MOCKS/ADD/SUCCESS",
"@APP/MOCKS/ADD/FAILURE"
)<NewMocks, void, Error>();
)<NewMocks, void, SmockerError>();

const lockMocks = createAsyncAction(
"@APP/MOCKS/LOCK",
"@APP/MOCKS/LOCK/SUCCESS",
"@APP/MOCKS/LOCK/FAILURE"
)<string[], Mocks, Error>();
)<string[], Mocks, SmockerError>();

const unlockMocks = createAsyncAction(
"@APP/MOCKS/UNLOCK",
"@APP/MOCKS/UNLOCK/SUCCESS",
"@APP/MOCKS/UNLOCK/FAILURE"
)<string[], Mocks, Error>();
)<string[], Mocks, SmockerError>();

const reset = createAsyncAction(
"@APP/RESET",
"@APP/RESET/SUCCESS",
"@APP/RESET/FAILURE"
)<void, void, Error>();
)<void, void, SmockerError>();

export const actions = {
fetchSessions,
Expand Down
Loading

0 comments on commit 2dd859b

Please sign in to comment.