Skip to content

Commit

Permalink
all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTallJerry committed Mar 4, 2024
1 parent 8d807b9 commit 6dacbc5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 51 deletions.
1 change: 0 additions & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"@babel/preset-react": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@testing-library/react": "^14.2.1",
"@testing-library/user-event": "^14.5.2",
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.18",
"babel-jest": "^29.7.0",
Expand Down
1 change: 0 additions & 1 deletion demo/src/MemoryModels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ function MemoryModelsFileInput(props: MemoryModelsInputPropTypes) {
};

const onReapplyBtnClick = () => {
console.log("reached");
props.setTextData(uploadedFileString);
};

Expand Down
56 changes: 21 additions & 35 deletions demo/src/__tests__/MemoryModels.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ describe("MemoryModelsUserInput", () => {

it("calls console error and setTextData when file upload fails", async () => {
const mockErrorMessage = "Mock error message";
const fileReaderSpy = jest
.spyOn(global, "FileReader")
.mockImplementationOnce(() => {
throw new Error(mockErrorMessage);
});
jest.spyOn(global, "FileReader").mockImplementationOnce(() => {
throw new Error(mockErrorMessage);
});
const consoleErrorSpy = jest.spyOn(console, "error");

const file = new File(
Expand All @@ -110,67 +108,55 @@ describe("MemoryModelsUserInput", () => {
}
);
const input: HTMLInputElement = screen.getByTestId("file-input");
fireEvent.change(input, { target: { files: [file] } });
await waitFor(() => {
expect(consoleErrorSpy).toHaveBeenNthCalledWith(
1,
`Error parsing uploaded File as JSON: ${mockErrorMessage}`
);
expect(setTextDataMock).toHaveBeenNthCalledWith(1, null);
// this needs to be awaited because of fileReader.onload being async
fireEvent.change(input, { target: { files: [file] } });
});

expect(consoleErrorSpy).toHaveBeenNthCalledWith(
1,
`Error parsing uploaded File as JSON: ${mockErrorMessage}`
);
expect(setTextDataMock).toHaveBeenNthCalledWith(1, null);
});

describe("when a file is uploaded", () => {
const fileString = JSON.stringify({ id: 1, uuid: 2 });
let input: HTMLInputElement;

beforeEach(() => {
beforeEach(async () => {
const file = new File([fileString], "test.json", {
type: "application/json",
});
input = screen.getByTestId("file-input");
fireEvent.change(input, { target: { files: [file] } });
});

it("calls setTextData", async () => {
await waitFor(() => {
expect(setTextDataMock).toHaveBeenNthCalledWith(
1,
fileString
);
});
});

it("enabled reapply Button", () => {
it("enables reapply Button", async () => {
const reapplyBtn = screen.getByTestId(
"file-input-reapply-button"
);
expect(reapplyBtn).toHaveProperty("disabled", true);
await waitFor(() => {
expect(reapplyBtn).toHaveProperty("disabled", false);
});
});

it("clicking reapply button calls setTextData", async () => {
const reapplyBtn = screen.getByTestId(
"file-input-reapply-button"
);
fireEvent.click(reapplyBtn);

await waitFor(() => {
// reapply original fileString
// wait until the button is enabled to click
expect(reapplyBtn).toHaveProperty("disabled", false);
fireEvent.click(reapplyBtn);
// once from reapplyBtn onChange, once from MemoryModelsTextInput handleTextFieldChange
expect(setTextDataMock).toHaveBeenNthCalledWith(
1,
2,
fileString
);
});
});

it("clicking reapply button doesn't call setTextData if there hasn't been a text change", async () => {
const reapplyBtn = screen.getByTestId(
"file-input-reapply-button"
);
fireEvent.click(reapplyBtn);

expect(setTextDataMock).toHaveBeenNthCalledWith(1, fileString);
});
});
});
});
14 changes: 0 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6dacbc5

Please sign in to comment.