Skip to content

Commit

Permalink
Merge pull request #36 from fga-eps-mds/test/integration-tests
Browse files Browse the repository at this point in the history
Cria testes de integração
  • Loading branch information
senaarth authored Jun 19, 2023
2 parents f2954db + a3c2e58 commit 89d7c31
Show file tree
Hide file tree
Showing 37 changed files with 3,641 additions and 1,549 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ jobs:

test:
runs-on: ubuntu-latest
env:
VITE_API_SERVICE_URL: ${{ secrets.VITE_API_SERVICE_URL }}
VITE_USER_SERVICE_URL: ${{ secrets.VITE_USER_SERVICE_URL }}
VITE_UNITS_SERVICE_URL: ${{ secrets.VITE_UNITS_SERVICE_URL }}
VITE_STAGES_SERVICE_URL: ${{ secrets.VITE_STAGES_SERVICE_URL }}
VITE_PROCESSES_SERVICE_URL: ${{ secrets.VITE_PROCESSES_SERVICE_URL }}
VITE_FLOWS_SERVICE_URL: ${{ secrets.VITE_FLOWS_SERVICE_URL }}
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down Expand Up @@ -150,6 +157,13 @@ jobs:
name: sonarcloud
runs-on: ubuntu-latest
continue-on-error: true
env:
VITE_API_SERVICE_URL: ${{ secrets.VITE_API_SERVICE_URL }}
VITE_USER_SERVICE_URL: ${{ secrets.VITE_USER_SERVICE_URL }}
VITE_UNITS_SERVICE_URL: ${{ secrets.VITE_UNITS_SERVICE_URL }}
VITE_STAGES_SERVICE_URL: ${{ secrets.VITE_STAGES_SERVICE_URL }}
VITE_PROCESSES_SERVICE_URL: ${{ secrets.VITE_PROCESSES_SERVICE_URL }}
VITE_FLOWS_SERVICE_URL: ${{ secrets.VITE_FLOWS_SERVICE_URL }}
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand All @@ -173,7 +187,7 @@ jobs:
run: npx eslint -f json -o reports/eslint-report.json src || true

- name: 🧪 Testes e cobertura
run: VITEST_REPORTERS=vitest-sonar-reporter yarn test
run: VITEST_REPORTERS=vitest-sonar-reporter yarn test --coverage

- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"check-format": "prettier --check .",
"check-lint": "eslint . --ext ts --ext tsx --ext js",
"format": "prettier --write .",
"test": "vitest"
"test": "vitest --silent"
},
"dependencies": {
"@chakra-ui/form-control": "^2.0.0",
Expand All @@ -29,26 +29,31 @@
"@emotion/styled": "^11.10.6",
"@hookform/resolvers": "^3.1.0",
"@tanstack/react-table": "^8.8.5",
"@testing-library/user-event": "^14.4.3",
"@types/node": "^18.16.0",
"@types/react-table": "^7.7.14",
"@vitejs/plugin-react": "^4.0.0",
"axios": "^1.3.6",
"chakra-react-select": "^4.6.0",
"framer-motion": "^10.12.4",
"lodash": "^4.17.21",
"msw": "^1.2.2",
"nwsapi": "2.2.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.43.9",
"react-icons": "^4.8.0",
"react-input-mask": "^2.0.4",
"react-query": "^3.39.3",
"react-router-dom": "^6.10.0",
"react-select-event": "^5.5.1",
"react-table": "^7.8.0",
"reactflow": "^11.7.2",
"resize-observer-polyfill": "^1.5.1",
"vite": "^4.3.0",
"vite-tsconfig-paths": "^4.2.0",
"yup": "^1.1.1",
"vitest-sonar-reporter": "^0.4.1"
"vitest-sonar-reporter": "^0.4.1",
"yup": "^1.1.1"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
Expand Down
6 changes: 4 additions & 2 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ sonar.projectName=2023-1-CAPJu-Front
sonar.host.url=https://sonarcloud.io
sonar.language=ts

sonar.sources=src/
sonar.sources=src
sonar.exclusions=src/__tests__/**, **/metrics/*.py, vite.config.ts, .vitest.config.js, dist/
sonar.tests= src/__tests__
sonar.tests=src
sonar.test.inclusions=**/*.test.tsx,**/*.test.ts
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.testExecutionReportPaths=coverage/coverage.xml

sonar.typescript.tsconfigPath=tsconfig.json
sonar.sourceEncoding=UTF-8
5 changes: 4 additions & 1 deletion src/hooks/useAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ type AuthContextType = {
const AuthContext = createContext({} as AuthContextType);

export const AuthProvider = ({ children }: { children: ReactNode }) => {
const [user, setUser] = useState<User | null>(null);
const localUser = localStorage.getItem("@CAPJu:user");
const [user, setUser] = useState<User | null>(
localUser ? JSON.parse(localUser) : null
);

const handleLogin = useCallback(
async (credentials: {
Expand Down
1 change: 1 addition & 0 deletions src/pages/Flows/CreationModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function CreationModal({
formState: { errors, isSubmitted },
reset,
} = useForm<FormValues>({
// @ts-ignore
resolver: yupResolver(validationSchema),
reValidateMode: "onChange",
});
Expand Down
1 change: 1 addition & 0 deletions src/pages/Flows/EditionModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function EditionModal({
handleSubmit,
formState: { errors, isSubmitted },
} = useForm<FormValues>({
// @ts-ignore
resolver: yupResolver(validationSchema),
reValidateMode: "onChange",
});
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Processes/CreationModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ type FormValues = {
const validationSchema = yup.object({
record: yup.string().required("Digite o registro do processo."),
nickname: yup.string().required("Dê um apelido para o processo."),
idFlow: yup.string().required("Selecione um fluxo para o processo."),
idFlow: yup.number().required("Selecione um fluxo para o processo."),
hasLegalPriority: yup.bool(),
idPriority: yup.string().when("hasLegalPriority", (hasLegalPriority) => {
idPriority: yup.number().when("hasLegalPriority", (hasLegalPriority) => {
return hasLegalPriority[0]
? yup.string().required("Escolha a prioridade legal do processo.")
: yup.string().notRequired();
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Processes/EditionModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ type FormValues = {
const validationSchema = yup.object({
record: yup.string().required("Digite o registro do processo."),
nickname: yup.string().required("Dê um apelido para o processo."),
idFlow: yup.string().required("Selecione um fluxo para o processo."),
idFlow: yup.number().required("Selecione um fluxo para o processo."),
hasLegalPriority: yup.bool(),
idPriority: yup.string().when("hasLegalPriority", (hasLegalPriority) => {
idPriority: yup.number().when("hasLegalPriority", (hasLegalPriority) => {
return hasLegalPriority[0]
? yup.string().required("Escolha a prioridade legal do processo.")
: yup.string().notRequired();
Expand Down
116 changes: 54 additions & 62 deletions src/pages/Processes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function Processes() {
onOpen: onEditionOpen,
onClose: onEditionClose,
} = useDisclosure();
const { data: flowsData } = useQuery({
const { data: flowsData, isFetched: isFlowsFetched } = useQuery({
queryKey: ["flows"],
queryFn: async () => {
const res = await getFlows();
Expand Down Expand Up @@ -87,35 +87,7 @@ function Processes() {

if (res.type === "error") throw new Error(res.error.message);

return {
...res,
value: res?.value?.reduce((acc: Process[], curr: Process) => {
const currFlow = flowsData?.value?.find(
(item) =>
item?.idFlow === ((curr?.idFlow as number[])[0] || curr?.idFlow)
);
const currIndexInFlow =
currFlow?.stages?.indexOf(curr?.idStage) || -1;
const currentState =
(currFlow?.stages && currIndexInFlow !== -1) ||
curr.status === "notStarted"
? `${currIndexInFlow + 1}/${currFlow?.stages?.length}`
: `${currIndexInFlow + 2}/${currFlow?.stages?.length}`;

return [
...acc,
{
...curr,
currentState: `${
curr.status === "finished"
? `${currFlow?.stages?.length}/${currFlow?.stages?.length}`
: currentState
}`,
flowName: currFlow?.name,
},
];
}, []),
};
return res;
},
onError: () => {
toast({
Expand Down Expand Up @@ -179,7 +151,7 @@ function Processes() {
};

const filteredProcess = useMemo<TableRow<Process>[]>(() => {
if (!isProcessesFetched) return [];
if (!isProcessesFetched || !isFlowsFetched) return [];

let value =
filter !== ""
Expand All @@ -202,39 +174,59 @@ function Processes() {
(
acc: TableRow<Process>[] | Process[],
curr: TableRow<Process> | Process
) => [
...acc,
{
...curr,
tableActions,
actionsProps: {
process: curr,
pathname: `/processos/${curr.record}`,
state: {
) => {
const currFlow = flowsData?.value?.find(
(item) =>
item?.idFlow === ((curr?.idFlow as number[])[0] || curr?.idFlow)
);
const currIndexInFlow =
currFlow?.stages?.indexOf(curr?.idStage) || -1;
const currentState =
(currFlow?.stages && currIndexInFlow !== -1) ||
curr.status === "notStarted"
? `${currIndexInFlow + 1}/${currFlow?.stages?.length}`
: `${currIndexInFlow + 2}/${currFlow?.stages?.length}`;

return [
...acc,
{
...curr,
tableActions,
actionsProps: {
process: curr,
...(state || {}),
pathname: `/processos/${curr.record}`,
state: {
process: curr,
...(state || {}),
},
},
// @ts-ignore
record: curr.idPriority ? (
<Flex flex="1" alignItems="center" gap="1">
{curr.record}
<Tooltip
label="Prioridade legal"
hasArrow
background="blackAlpha.900"
placement="right"
>
<ArrowUpIcon boxSize={3.5} />
</Tooltip>
</Flex>
) : (
curr.record
),
currentState: `${
curr.status === "finished"
? `${currFlow?.stages?.length}/${currFlow?.stages?.length}`
: currentState
}`,
flowName: currFlow?.name,
// @ts-ignore
status: labelByProcessStatus[curr.status],
},
// @ts-ignore
record: curr.idPriority ? (
<Flex flex="1" alignItems="center" gap="1">
{curr.record}
<Tooltip
label="Prioridade legal"
hasArrow
background="blackAlpha.900"
placement="right"
>
<ArrowUpIcon boxSize={3.5} />
</Tooltip>
</Flex>
) : (
curr.record
),
// @ts-ignore
status: labelByProcessStatus[curr.status],
},
],
];
},
[]
) as TableRow<Process>[]) || []
);
Expand Down Expand Up @@ -298,7 +290,7 @@ function Processes() {

useEffect(() => {
refetchProcesses();
}, [flowsData]);
}, [flowsData, isFlowsFetched]);

return (
<PrivateLayout>
Expand Down
6 changes: 3 additions & 3 deletions src/pages/Signup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ const validationSchema = yup.object({
passwordConfirmation: yup
.string()
.required("Confirme sua senha")
.oneOf([yup.ref("password")], "Suas senhas não batem."),
idUnit: yup.string().required("Selecione sua unidade"),
.oneOf([yup.ref("password")], "Suas senhas não batem"),
idUnit: yup.string().required("Selecione uma unidade"),
idRole: yup.string().required("Selecione um perfil"),
});

Expand Down Expand Up @@ -214,7 +214,7 @@ function Signup() {
<Input
type="password"
label="Confirmação de senha"
placeholder="Confirme sua senha"
placeholder="Confirme uma senha"
errors={errors.passwordConfirmation}
{...register("passwordConfirmation")}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Stages/CreationModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type FormValues = {

const validationSchema = yup.object({
name: yup.string().required("Dê um nome à etapa"),
duration: yup.string().required("Dê uma duração para esta etapa"),
duration: yup.number().required("Dê uma duração para esta etapa"),
});

interface CreationModalProps {
Expand Down
4 changes: 0 additions & 4 deletions src/pages/ViewProcess/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,6 @@ function ViewProcess() {
if (!process) navigate(-1);
}, [process]);

useEffect(() => {
console.log(flowData?.value?.sequences[0]?.from);
}, [flowData]);

return (
<PrivateLayout>
<Flex w="90%" maxW={1120} flexDir="column" gap="3">
Expand Down
55 changes: 55 additions & 0 deletions src/pages/__tests__/AccountEdition.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, expect } from "vitest";
import { act, render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { rest } from "msw";
import { setupServer } from "msw/node";
import { QueryClient, QueryClientProvider } from "react-query";
import { ChakraProvider } from "@chakra-ui/react";

import { LoadingProvider } from "hooks/useLoading";
import { AuthProvider } from "hooks/useAuth";
import { mockedUser } from "utils/mocks";
import AccountEdition from "../AccountEdition";

const restHandlers = [
rest.get(
`${import.meta.env.VITE_USER_SERVICE_URL}user/${mockedUser.cpf}`,
async (_req, res, ctx) => res(ctx.status(200), ctx.json(mockedUser))
),
];

const server = setupServer(...restHandlers);
const queryClient = new QueryClient();

describe("AccountEdition page", () => {
beforeAll(async () => {
localStorage.setItem("@CAPJu:user", JSON.stringify(mockedUser));
server.listen({ onUnhandledRequest: "error" });
});

beforeEach(async () => {
await act(async () => {
await render(
<ChakraProvider>
<LoadingProvider>
<QueryClientProvider client={queryClient}>
<AuthProvider>
<MemoryRouter>
<AccountEdition />
</MemoryRouter>
</AuthProvider>
</QueryClientProvider>
</LoadingProvider>
</ChakraProvider>
);
});
});

afterAll(() => server.close());

afterEach(() => server.resetHandlers());

it("renders correctly", () => {
expect(screen).toMatchSnapshot();
});
});
Loading

0 comments on commit 89d7c31

Please sign in to comment.