Skip to content
Merged
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
403 changes: 403 additions & 0 deletions TESTING.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"preview": "vite preview",
"test.e2e": "cypress run",
"test.unit": "vitest",
"test": "vitest",
"test:watch": "vitest --watch",
"test:coverage": "vitest --coverage",
"lint": "eslint",
"generate-pwa-assets": "pwa-assets-generator"
},
Expand Down
122 changes: 31 additions & 91 deletions src/components/InvoiceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ import {
IonFab,
IonFabButton,
} from "@ionic/react";
import { close, save, add, trash, refresh } from "ionicons/icons";
import { close, save, add, trash } from "ionicons/icons";
import {
addInvoiceData,
getInvoiceData,
clearInvoiceData,
} from "./socialcalc/modules/invoice.js";
import "./InvoiceForm.css";
Expand Down Expand Up @@ -96,10 +95,10 @@ const InvoiceForm: React.FC<InvoiceFormProps> = ({ isOpen, onClose }) => {
"success" | "danger" | "warning"
>("success");

// Load existing data when modal opens
// Reset form when modal opens
useEffect(() => {
if (isOpen) {
loadExistingData();
resetForm(false); // Silent reset when modal opens
}
}, [isOpen]);

Expand All @@ -115,85 +114,6 @@ const InvoiceForm: React.FC<InvoiceFormProps> = ({ isOpen, onClose }) => {
}));
}, [formData.items]);

const loadExistingData = () => {
try {
console.log("Loading existing invoice data from spreadsheet...");
const existingData = getInvoiceData();
console.log("Retrieved existing data:", existingData);

if (existingData) {
// Ensure items array has at least one item for the form
let itemsToLoad =
existingData.items && existingData.items.length > 0
? existingData.items
: [{ description: "", amount: "" }];

// Limit to maximum 13 items
if (itemsToLoad.length > 13) {
itemsToLoad = itemsToLoad.slice(0, 13);
showToastMessage(
"Loaded first 13 items only (maximum limit)",
"warning"
);
}

setFormData({
billTo: existingData.billTo || {
name: "",
streetAddress: "",
cityStateZip: "",
phone: "",
email: "",
},
from: existingData.from || {
name: "",
streetAddress: "",
cityStateZip: "",
phone: "",
email: "",
},
invoice: existingData.invoice || {
number: "",
date: new Date().toISOString().split("T")[0],
},
items: itemsToLoad,
total: existingData.total || "",
});

console.log("Form data loaded successfully");
showToastMessage("Data loaded from spreadsheet", "success");
} else {
console.log("No existing data found, using default values");
// Reset to default values if no data found
setFormData({
billTo: {
name: "",
streetAddress: "",
cityStateZip: "",
phone: "",
email: "",
},
from: {
name: "",
streetAddress: "",
cityStateZip: "",
phone: "",
email: "",
},
invoice: {
number: "",
date: new Date().toISOString().split("T")[0],
},
items: [{ description: "", amount: "" }],
total: "",
});
}
} catch (error) {
console.error("Error loading existing invoice data:", error);
showToastMessage("Error loading existing data", "warning");
}
};

const showToastMessage = (
message: string,
color: "success" | "danger" | "warning" = "success"
Expand All @@ -203,6 +123,34 @@ const InvoiceForm: React.FC<InvoiceFormProps> = ({ isOpen, onClose }) => {
setShowToast(true);
};

const resetForm = (showMessage: boolean = true) => {
setFormData({
billTo: {
name: "",
streetAddress: "",
cityStateZip: "",
phone: "",
email: "",
},
from: {
name: "",
streetAddress: "",
cityStateZip: "",
phone: "",
email: "",
},
invoice: {
number: "",
date: new Date().toISOString().split("T")[0],
},
items: [{ description: "", amount: "" }],
total: "",
});
if (showMessage) {
showToastMessage("Form reset to default values", "success");
}
};

const handleInputChange = (
section: keyof InvoiceFormData,
field: string,
Expand Down Expand Up @@ -314,11 +262,6 @@ const InvoiceForm: React.FC<InvoiceFormProps> = ({ isOpen, onClose }) => {
}
};

const handleRefresh = () => {
console.log("Refreshing data from spreadsheet...");
loadExistingData();
};

return (
<>
<IonModal
Expand All @@ -330,9 +273,6 @@ const InvoiceForm: React.FC<InvoiceFormProps> = ({ isOpen, onClose }) => {
<IonToolbar color="primary">
<IonTitle>Invoice Form</IonTitle>
<IonButtons slot="end">
<IonButton onClick={handleRefresh}>
<IonIcon icon={refresh} />
</IonButton>
<IonButton onClick={onClose}>
<IonIcon icon={close} />
</IonButton>
Expand Down
121 changes: 0 additions & 121 deletions src/components/socialcalc/modules/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,127 +218,6 @@ export function addInvoiceData(invoiceData) {
});
}

export function getInvoiceData() {
console.log("=== GET INVOICE DATA START ===");

try {
// Get invoice coordinates
const coordinates = getInvoiceCoordinates();

// Get current sheet
var control = SocialCalc.GetCurrentWorkBookControl();
if (!control || !control.currentSheetButton) {
throw new Error("No current sheet available");
}

var currsheet = control.currentSheetButton.id;
console.log("Current active sheet:", currsheet);

// Read Bill To values
var billToName = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.billTo.name
);
var billToStreetAddress = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.billTo.streetAddress
);
var billToCityStateZip = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.billTo.cityStateZip
);
var billToPhone = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.billTo.phone
);
var billToEmail = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.billTo.email
);

// Read From values
var fromName = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.from.name
);
var fromStreetAddress = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.from.streetAddress
);
var fromCityStateZip = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.from.cityStateZip
);
var fromPhone = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.from.phone
);
var fromEmail = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.from.email
);

// Read Invoice values
var invoiceNumber = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.invoice.number
);
var invoiceDate = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.invoice.date
);

// Read Items
var items = [];
for (
let row = coordinates.items.startRow;
row <= coordinates.items.endRow;
row++
) {
var description = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.items.descriptionColumn + row
);
var amount = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.items.amountColumn + row
);

// Only add items that have at least a description or amount
if (description || amount) {
items.push({
description: description || "",
amount: amount || "",
});
}
}

// Read Total
var total = SocialCalc.GetCellDataValue(
currsheet + "!" + coordinates.total.sum
);

const data = {
billTo: {
name: billToName || "",
streetAddress: billToStreetAddress || "",
cityStateZip: billToCityStateZip || "",
phone: billToPhone || "",
email: billToEmail || "",
},
from: {
name: fromName || "",
streetAddress: fromStreetAddress || "",
cityStateZip: fromCityStateZip || "",
phone: fromPhone || "",
email: fromEmail || "",
},
invoice: {
number: invoiceNumber || "",
date: invoiceDate || "",
},
items: items,
total: total || "",
};

console.log("Retrieved invoice data:", data);
console.log("=== GET INVOICE DATA SUCCESS ===");

return data;
} catch (error) {
console.error("=== GET INVOICE DATA ERROR ===");
console.error("Error details:", error);
console.error("Stack trace:", error.stack);
return null;
}
}

export function clearInvoiceData() {
return new Promise(function (resolve, reject) {
console.log("=== CLEAR INVOICE DATA START ===");
Expand Down
76 changes: 76 additions & 0 deletions src/test/components/InvoiceForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from "react";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import InvoiceForm from "../../components/InvoiceForm";
import { InvoiceProvider } from "../../contexts/InvoiceContext";

// Mock the SocialCalc invoice module with factory function
vi.mock("../../components/socialcalc/modules/invoice.js", () => ({
addInvoiceData: vi.fn(() => true),
clearInvoiceData: vi.fn(() => true),
}));

describe("InvoiceForm", () => {
const defaultProps = {
isOpen: true,
onClose: vi.fn(),
};

beforeEach(() => {
vi.clearAllMocks();
});

const renderWithProvider = (props = defaultProps) => {
return render(
<InvoiceProvider>
<InvoiceForm {...props} />
</InvoiceProvider>
);
};

it("renders when open", () => {
renderWithProvider();

expect(screen.getByTestId("ion-modal")).toBeInTheDocument();
expect(screen.getByText("Invoice Form")).toBeInTheDocument();
});

it("does not render when closed", () => {
renderWithProvider({ ...defaultProps, isOpen: false });

expect(screen.queryByTestId("ion-modal")).not.toBeInTheDocument();
});

it("displays form fields", () => {
renderWithProvider();

// Check for some key form fields that should exist
expect(screen.getByTestId("ion-modal")).toBeInTheDocument();
expect(screen.getByText("Invoice Form")).toBeInTheDocument();
});

it("closes modal when close button is clicked", () => {
const mockOnClose = vi.fn();

renderWithProvider({ ...defaultProps, onClose: mockOnClose });

// Find and click close button (looking for close icon)
const closeButtons = screen.getAllByTestId("ion-button");
// The first button should be the close button
fireEvent.click(closeButtons[0]);

expect(mockOnClose).toHaveBeenCalled();
});

// Simplified test for basic functionality
it("handles form interaction", () => {
renderWithProvider();

// Just verify the modal renders and we can interact with it
expect(screen.getByTestId("ion-modal")).toBeInTheDocument();

// Try to find any input or interactive element
const inputs = screen.getAllByTestId("ion-input");
expect(inputs.length).toBeGreaterThan(0);
});
});
Loading