From 684345e28a2f3048072836f142799e77431bef73 Mon Sep 17 00:00:00 2001 From: Iddan Aaronsohn Date: Thu, 28 Sep 2023 23:40:22 +0300 Subject: [PATCH] Add more tests to active cell --- src/ActiveCell.test.tsx | 57 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/ActiveCell.test.tsx b/src/ActiveCell.test.tsx index eeca07bd..a424d704 100644 --- a/src/ActiveCell.test.tsx +++ b/src/ActiveCell.test.tsx @@ -3,12 +3,14 @@ */ import * as React from "react"; -import { render } from "@testing-library/react"; +import { fireEvent, render } from "@testing-library/react"; import * as Point from "./point"; import * as Types from "./types"; +import * as Actions from "./actions"; import context from "./context"; import { INITIAL_STATE } from "./reducer"; import ActiveCell from "./ActiveCell"; +import DataEditor from "./DataEditor"; const MOCK_DATA_EDITOR = jest.fn(() => null); const DISPATCH_MOCK = jest.fn(); @@ -30,6 +32,9 @@ const STATE_WITH_ACTIVE: Types.StoreState = { }; describe("", () => { + beforeEach(() => { + jest.clearAllMocks(); + }); test("renders hidden when active is not defined", () => { render( @@ -73,4 +78,54 @@ describe("", () => { {} ); }); + test("calls setCellData if value changed", () => { + const { rerender } = render( + + + + ); + const activeCell = document.querySelector(".Spreadsheet__active-cell"); + expect(activeCell).not.toBeNull(); + expect(activeCell).toHaveClass("Spreadsheet__active-cell--edit"); + const input = activeCell?.querySelector("input"); + if (!input) throw new Error("input not found"); + fireEvent.change(input, { target: { value: "test" } }); + expect(DISPATCH_MOCK).toBeCalledTimes(1); + expect(DISPATCH_MOCK).toBeCalledWith( + Actions.setCellData(Point.ORIGIN, { + value: "test", + }) + ); + rerender( + + + + ); + expect(activeCell).not.toHaveClass("Spreadsheet__active-cell--edit"); + }); + test("doesn't call setCellData if value not changed", () => { + const { rerender } = render( + + + + ); + const activeCell = document.querySelector(".Spreadsheet__active-cell"); + expect(activeCell).not.toBeNull(); + expect(activeCell).toHaveClass("Spreadsheet__active-cell--edit"); + rerender( + + + + ); + expect(DISPATCH_MOCK).toBeCalledTimes(0); + expect(activeCell).not.toHaveClass("Spreadsheet__active-cell--edit"); + }); });