Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(O3 2501): write unit tests for the components #9

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require("path");

module.exports = {
transform: {
"^.+\\.tsx?$": ["@swc/jest"],
"^.+\\.tsx?$": "@swc/jest",
},
transformIgnorePatterns: ["/node_modules/(?!@openmrs)"],
moduleNameMapper: {
"@openmrs/esm-framework": "@openmrs/esm-framework/mock",
"\\.(s?css)$": "identity-obj-proxy",
"@openmrs/esm-framework": "@openmrs/esm-framework/mock",
"^lodash-es/(.*)$": "lodash/$1",
"^dexie$": require.resolve("dexie"),
},
setupFilesAfterEnv: ["<rootDir>/src/setup-tests.ts"],
setupFilesAfterEnv: [path.resolve(__dirname, "tools", "setup-tests.ts")],
testEnvironment: "jsdom",
testEnvironmentOptions: {
url: "http://localhost/",
Expand Down
23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"lint": "TIMING=1 eslint src --ext js,jsx,ts,tsx",
"prettier": "prettier --write \"src/**/*.{ts,tsx}\"",
"typescript": "tsc",
"test": "jest --config jest.config.js --passWithNoTests",
"test": "jest --config jest.config.js --verbose",
"verify": "turbo lint typescript coverage ",
"coverage": "yarn test -- --coverage",
"prepare": "husky install",
Expand Down Expand Up @@ -45,11 +45,15 @@
},
"dependencies": {
"@carbon/react": "^1.33.0",
"lodash-es": "^4.17.21",
"react-image-annotate": "^1.8.0"
"@openmrs/esm-patient-common-lib": "^5.0.0",
"@openmrs/openmrs-form-engine-lib": "latest",
"canvas-to-image": "^2.2.5",
"fabric": "^5.3.0",
"lodash-es": "^4.17.21"
},
"peerDependencies": {
"@openmrs/esm-framework": "*",
"@openmrs/esm-patient-common-lib": "5.x",
"dayjs": "1.x",
"react": "18.x",
"react-i18next": "11.x",
Expand All @@ -63,10 +67,10 @@
"@swc/core": "^1.3.68",
"@swc/jest": "^0.2.26",
"@testing-library/dom": "^8.20.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^28.1.8",
"@testing-library/jest-dom": "^6.1.4",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.5.1",
"@types/jest": "^29.5.6",
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6",
"@types/react-router": "^5.1.20",
Expand All @@ -82,7 +86,7 @@
"husky": "^8.0.3",
"identity-obj-proxy": "^3.0.0",
"imports-loader": "^4.0.1",
"jest": "^28.1.3",
"jest": "^29.7.0",
"jest-cli": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
"openmrs": "next",
Expand All @@ -95,8 +99,9 @@
"react-router-dom": "^6.14.1",
"rxjs": "^6.6.7",
"swc-loader": "^0.2.3",
"ts-jest": "^29.1.1",
"turbo": "^1.10.7",
"typescript": "^4.9.5",
"typescript": "latest",
"webpack": "^5.88.1",
"webpack-cli": "^5.1.4"
},
Expand Down
24 changes: 24 additions & 0 deletions src/attachments-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export interface UploadedFile {
file?: File;
base64Content: string;
fileName: string;
fileType: string;
fileDescription: string;
status?: "uploading" | "complete";
}
export interface Attachment {
id: string;
src: string;
title: string;
description: string;
dateTime: string;
bytesMimeType: string;
bytesContentFamily: string;
}
export interface AttachmentResponse {
bytesContentFamily: string;
bytesMimeType: string;
comment: string;
dateTime: string;
uuid: string;
}
88 changes: 88 additions & 0 deletions src/attachments/attachments.resource.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { useMemo } from "react";
import useSWR from "swr";
import { FetchResponse, openmrsFetch } from "@openmrs/esm-framework";
import { AttachmentResponse, UploadedFile } from "../attachments-types";

export const attachmentUrl = "/ws/rest/v1/attachment";

export function getAttachmentByUuid(
attachmentUuid: string,
abortController: AbortController
) {
return openmrsFetch(`${attachmentUrl}/${attachmentUuid}`, {
signal: abortController.signal,
});
}

export function useAttachments(
patientUuid: string,
includeEncounterless: boolean
) {
const { data, error, mutate, isLoading, isValidating } = useSWR<
FetchResponse<{ results: Array<AttachmentResponse> }>
>(
`${attachmentUrl}?patient=${patientUuid}&includeEncounterless=${includeEncounterless}`,
openmrsFetch
);

const results = useMemo(
() => ({
isLoading,
data: data?.data.results ?? [],
error,
mutate,
isValidating,
}),
[data, error, isLoading, isValidating, mutate]
);

return results;
}

export function getAttachments(
patientUuid: string,
includeEncounterless: boolean,
abortController: AbortController
) {
return openmrsFetch(
`${attachmentUrl}?patient=${patientUuid}&includeEncounterless=${includeEncounterless}`,
{
signal: abortController.signal,
}
);
}

export async function createAttachment(
patientUuid: string,
fileToUpload: UploadedFile
) {
const formData = new FormData();

formData.append("fileCaption", fileToUpload.fileName);
formData.append("patient", patientUuid);

if (fileToUpload.file) {
formData.append("file", fileToUpload.file);
} else {
formData.append(
"file",
new File([""], fileToUpload.fileName),
fileToUpload.fileName
);
formData.append("base64Content", fileToUpload.base64Content);
}
return openmrsFetch(attachmentUrl, {
method: "POST",
body: formData,
});
}

export function deleteAttachmentPermanently(
attachmentUuid: string,
abortController: AbortController
) {
return openmrsFetch(`${attachmentUrl}/${attachmentUuid}`, {
method: "DELETE",
signal: abortController.signal,
});
}
Loading
Loading