Skip to content

Commit

Permalink
feat: add jest annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
apurv-wednesday committed Apr 26, 2024
1 parent 2a52566 commit 0a2f036
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 91 deletions.
30 changes: 27 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"plugin:@typescript-eslint/recommended",
"next/core-web-vitals"
],
"plugins": ["react", "@typescript-eslint"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
Expand All @@ -18,17 +19,17 @@
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["react", "@typescript-eslint"],
"rules": {
"react/prop-types": "off",
"react/react-in-jsx-scope": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-inferrable-types": "error",
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"import/prefer-default-export": "off",
"import/prefer-default-export": "off", // check for relevance here
"eslint-comments/no-use": 0,
"import/no-cycle": "off",
"no-multi-assign": "off",
// "prettier/prettier": "error",
"no-console": 1,
"import/no-extraneous-dependencies": "off",
"import/no-unresolved": "error",
Expand All @@ -45,6 +46,29 @@
{
"patterns": ["@mui/*/*/*"]
}
],
"max-lines-per-function": ["error", 250],
"max-params": ["error", 3],
"complexity": ["error", 5],
"max-lines": ["error", 350],
"no-else-return": "error",
"require-jsdoc": [
"error",
{
"require": {
"FunctionDeclaration": true,
"MethodDefinition": false,
"ClassDeclaration": false,
"ArrowFunctionExpression": false,
"FunctionExpression": false
}
}
],
"no-shadow": "error",
"no-empty": "error",
"import/order": [
"error",
{ "groups": [["builtin", "external", "internal", "parent", "sibling", "index"]] }
]
}
}
41 changes: 41 additions & 0 deletions .github/workflows/jest-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Jest Coverage Report with Annotations (CI)
on:
pull_request:
branches: [main]
push:
branches: [main]
jobs:
coverage_report:
name: Jest Coverage Report
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'

# could not find the threshold value in jest.config.js
# therefore hardcoded for now
# - name: Get Threshold
# id: threshold
# uses: notiz-dev/github-action-json-property@release
# with:
# path: 'jest.config.js'
# prop_path: 'coverageThreshold.global.statements'

- name: Install dependencies
run: yarn

- name: Test and generate coverage report
uses: artiomtr/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
# threshold: ${{steps.threshold.outputs.prop}}
package-manager: yarn
custom-title: Jest Coverage Report
46 changes: 23 additions & 23 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ const createJestConfig = nextJest({ dir: "./" });
const jestConfig: Config = {
preset: "ts-jest",
testEnvironment: "jsdom",
collectCoverageFrom: [
"./common/**/*.{js,jsx,ts,tsx}",
"./containers/**/*.{js,jsx,ts,tsx}",
"./features/**/*.{js,jsx,ts,tsx}",
"./pages/**/*.{js,jsx,ts,tsx}",
"./store/**/*.{js,jsx,ts,tsx}",
"./styles/**/*.{js,jsx,ts,tsx}",
"./themes/**/*.{js,jsx,ts,tsx}",
"./utils/**/*.{js,jsx,ts,tsx}",
],
// comment coverage for now, to make the CI pipeline green
// coverageThreshold: {
// global: {
// statements: 90,
// branches: 90,
// functions: 90,
// lines: 90,
// },
// },
collectCoverageFrom: ["./src/**/*.{js,jsx,ts,tsx}"],
reporters: [
"default",
[
Expand All @@ -42,19 +42,19 @@ const jestConfig: Config = {
// Handle image imports
// https://jestjs.io/docs/webpack#handling-static-assets
"^.+\\.(jpg|jpeg|png|gif|webp|avif|svg)$": `<rootDir>/__mocks__/fileMock.js`,
"@styles(.*)": "<rootDir>/styles",
"@logger(.*)": "<rootDir>/logger",
"@constants(.*)": "<rootDir>/constants",
"services(.*)": "<rootDir>/services",
"^@features(.*)": "<rootDir>/features/$1",
"^@store(.*)": "<rootDir>/store/$1",
"^@containers(.*)": "<rootDir>/containers/$1",
"^@hooks(.*)": "<rootDir>/hooks/$1",
"^@shared(.*)": "<rootDir>/features/sharedComponents/$1",
"^@themes(.*)": "<rootDir>/themes/$1",
"^@utils(.*)": "<rootDir>/utils/$1",
"^@slices(.*)": "<rootDir>/store/slices/$1",
"^@app(.*)": "<rootDir>/$1",
"@styles(.*)": "<rootDir>/src/styles",
"@logger(.*)": "<rootDir>/src/logger",
"@constants(.*)": "<rootDir>/src/constants",
"services(.*)": "<rootDir>/src/services",
"^@features(.*)": "<rootDir>/src/features/$1",
"^@store(.*)": "<rootDir>/src/store/$1",
"^@containers(.*)": "<rootDir>/src/containers/$1",
"^@hooks(.*)": "<rootDir>/src/hooks/$1",
"^@shared(.*)": "<rootDir>/src/features/sharedComponents/$1",
"^@themes(.*)": "<rootDir>/src/themes/$1",
"^@utils(.*)": "<rootDir>/src/utils/$1",
"^@slices(.*)": "<rootDir>/src/store/slices/$1",
"^@app(.*)": "<rootDir>/src/$1",
},
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
testPathIgnorePatterns: [
Expand Down
6 changes: 3 additions & 3 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sonar.projectKey=wednesday-solutions_next-bulletproof-ts_AY6yu6eKB2n8RRmGoUz4
sonar.language=ts

# Source directory
sonar.sources=.
sonar.sources=src

# Test directory
sonar.tests=src
Expand All @@ -21,7 +21,7 @@ sonar.test.inclusions=**/*.test.tsx,**/*.test.js,**/*.test.ts
sonar.sourceEncoding=UTF-8

# Coverage report path
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.typescript.lcov.reportPaths=.coverage/lcov.info

# Test execution report path
sonar.testExecutionReportPaths=./reports/test-report.xml
sonar.testExecutionReportPaths=.reports/test-report.xml
15 changes: 10 additions & 5 deletions src/common/For/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ const For = ({
renderItem,
noParent,
}: ForProps<typeof of[0], typeof ParentComponent>) => {
const list = () => of.map((item, index) => ({ ...renderItem(item, index), key: index }));
const children = () => (
const renderList = () => of.map((item, index) => ({ ...renderItem(item, index), key: index }));

const renderChildren = () => (
<ParentComponent {...parentProps} data-testid="for">
{list()}
{renderList()}
</ParentComponent>
);

const renderWithoutParent = () => (of || []).length ? renderList() : null;

if (noParent) {
return (of || []).length ? list() : null;
return renderWithoutParent();
}
return (of || []).length ? children() : null;

return renderChildren();
};

For.defaultProps = {
Expand Down
4 changes: 2 additions & 2 deletions src/containers/Repos/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const Repos: React.FC<RepoContainerProps> = ({ maxwidth }) => {
});
};

const handleRepoSearch = debounce((repoName: string) => {
setRepoName(repoName);
const handleRepoSearch = debounce((repoNameValue: string) => {
setRepoName(repoNameValue);
setPage(1);
}, 500);

Expand Down
14 changes: 4 additions & 10 deletions src/features/info/components/RepoInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,22 @@ interface RepoInfoProps {

const RepoInfo: React.FC<RepoInfoProps> = ({ repoinfo }) => {
const { name, description, forks, watchers, owner, stargazersCount } = repoinfo;

const router = useRouter();
const renderChip = (label, value) => (value ? <Chip label={label} color="primary" /> : null);

return (
<CustomCard>
<Button color="primary" size="small" onClick={() => router.push("/")}>
<Trans>Back to Search</Trans>
</Button>
{name ? <T variant="subtitle1">{name}</T> : null}

{owner.login ? <T variant="subtitle2">{owner.login}</T> : null}

{description ? <T>{description}</T> : null}

<Stack direction="row" justifyContent="space-between" alignItems="center" textAlign="center">
{forks ? <Chip label={<Trans>Forks: {forks}</Trans>} color="primary" /> : null}

{watchers ? <Chip label={<Trans>Watchers: {watchers}</Trans>} color="primary" /> : null}

{stargazersCount ? (
<Chip label={<Trans>Stars: {stargazersCount}</Trans>} color="primary" />
) : null}
{renderChip(<Trans>Forks: {forks}</Trans>, forks)}
{renderChip(<Trans>Watchers: {watchers}</Trans>, watchers)}
{renderChip(<Trans>Stars: {stargazersCount}</Trans>, stargazersCount)}
</Stack>
</CustomCard>
);
Expand Down
1 change: 1 addition & 0 deletions src/features/repos/components/ErrorState/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ErrorStateProps {
reposError: string | undefined;
}

// eslint-disable-next-line complexity
const ErrorState: React.FC<ErrorStateProps> = ({ reposData, reposError, loading }) => {
let repoError: string | undefined;
if (reposError) {
Expand Down
6 changes: 6 additions & 0 deletions src/pages/api/hello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ type Data = {
name: string;
};

/**
* Request handler function for the API route.
* @param {import("next").NextApiRequest} req - The request object.
* @param {import("next").NextApiResponse<Data>} res - The response object.
* @returns {void}
*/
export default function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
res.status(200).json({ name: "John Doe" });
}
42 changes: 27 additions & 15 deletions src/themes/styles.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { css } from "@mui/material/styles";
import { theme } from "./mui";

export const configureFlex = (
export const configureFlex = ({
direction = "row",
justifyContent = "center",
alignItems = "center",
alignContent = "center",
flexBasis = 0,
flexGrow = 1,
flexShrink = 0
) => css`
flexShrink = 0,
}) => css`
${direction === "row" ? row() : column()}
flex-direction: ${direction};
justify-content: ${justifyContent};
Expand All @@ -32,19 +32,32 @@ const column = () => css`
`;

const rowCenter = () => css`
${configureFlex("row", "center", "center", "center")};
${configureFlex({
direction: "row",
justifyContent: "center",
alignItems: "center",
alignContent: "center",
})};
`;

const unequalColumns = () => css`
${configureFlex("column", "", "", "", 0, 0, 0)};
${configureFlex({
direction: "column",
justifyContent: "",
alignContent: "",
alignItems: "",
flexBasis: 0,
flexGrow: 0,
flexShrink: 0,
})};
`;

const height = (height = 4) => css`
height: ${height}rem;
const height = (value = 4) => css`
height: ${value}rem;
`;

const viewHeight = (height = 0) => css`
height: ${height}vh;
const viewHeight = (value = 0) => css`
height: ${value}vh;
`;

const top = (marginTop = 0) =>
Expand Down Expand Up @@ -88,22 +101,21 @@ const borderRadiusTop = (topRadius = 0) => css`
`;

const borderRadius = (radius: string | number) => {
const unit = typeof radius === 'string' ? '' : 'px';
const unit = typeof radius === "string" ? "" : "px";
return css`
border-radius: ${radius}${unit};
`;
};

const borderWithRadius = (width = 1, type = "solid", color = "#ccc", radius = 0) =>
const borderWithRadius = ({ width = 1, type = "solid", color = "#ccc", radius = 0 }= {}) =>
css`
border: ${width}px ${type} ${color};
${borderRadius(radius)}
`;

const boxShadow = (hOffset = 0, vOffset = 0, blur = 0, spread = 0, color = "#ccc") =>
css`
box-shadow: ${hOffset}px ${vOffset}px ${blur}px ${spread}px ${color};
`;
const boxShadow = ({ hOffset = 0, vOffset = 0, blur = 0, spread = 0, color = "#ccc" } = {}) => css`
box-shadow: ${hOffset}px ${vOffset}px ${blur}px ${spread}px ${color};
`;

const primaryBackgroundColor = () =>
css`
Expand Down
Loading

0 comments on commit 0a2f036

Please sign in to comment.