Skip to content

Commit

Permalink
adding tests for the changed logics
Browse files Browse the repository at this point in the history
  • Loading branch information
Gokul K authored and Gokul K committed Jun 14, 2024
1 parent e2d55c8 commit f0c2588
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
17 changes: 9 additions & 8 deletions js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,24 +505,25 @@ function findLastValue(values) {
return lastValue ? lastValue.result : null;
}

export async function copyLastValue(values, paste = false) {
export async function copyLastValue(values) {
// copy the last result to clipboard
const lastValue = findLastValue(values);
if (_.isNumber(lastValue)) {
copyValueToClipboard(lastValue);
if (paste) {
insertNode(lastValue);
}
return lastValue;
} else {
showToastMessage(`No result to copy`);
}
}

function onEditorKeydown(e) {
export function onEditorKeydown(e) {
let key = e.key;
// Order of below conditions matter since there is subset condition
if (key === "Enter" && e.shiftKey && (e.metaKey || e.ctrlKey)) {
copyLastValue(evaluatedValues, true);
let lastValue = copyLastValue(evaluatedValues);
if (lastValue) {
insertNode(lastValue);
}
} else if (key === "Enter" && (e.metaKey || e.ctrlKey)) {
copyLastValue(evaluatedValues);
} else if (key === "/" && (e.metaKey || e.ctrlKey)) {
Expand All @@ -542,7 +543,7 @@ function is_total_keywords(word) {
return ["total", "sum", "="].includes(word.toLowerCase().trim());
}

function post_process(inputs, outputs) {
export function postProcess(inputs, outputs) {
if (inputs.length === 0) {
return outputs;
}
Expand Down Expand Up @@ -571,7 +572,7 @@ export function evaluate(value) {
let lines = value.split("\n");
evaluatedValues = [];
let results = useMathJs(lines);
results = post_process(lines, results);
results = postProcess(lines, results);
for (let index = 0; index < results.length; index++) {
const result_expression = {
type: "expression",
Expand Down
50 changes: 48 additions & 2 deletions js/editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
copyLastValue,
getTitle,
initSentry,
postProcess,
parse,
} from "./editor";
import { describe, it, expect, vi, test, beforeEach } from "vitest";
Expand Down Expand Up @@ -137,6 +138,21 @@ describe("testing evaluate", () => {
const evalValues = await evaluate("data=10+10-1\ndata +2.5-0.50001x1.1");
expect(evalValues[1].result).toBeCloseTo(20.949);
});

test("Evaluate multi-line with total keyword 1: = ", async () => {
const evalValues = await evaluate("data=10\n+10-1\ndata +2.5-0.50\n=");
expect(evalValues[3].result).toBeCloseTo(31);
});

test("Evaluate multi-line with total keyword 2: total ", async () => {
const evalValues = await evaluate("10\n+10-1\n10x3\ntotal");
expect(evalValues[3].result).toBeCloseTo(49);
});

test("Evaluate multi-line with total keyword 3: sum ", async () => {
const evalValues = await evaluate("data=10\n+10-1\ndata +2.5\nsum");
expect(evalValues[3].result).toBeCloseTo(31.5);
});
});

// Mocking necessary parts of the global scope and DOM
Expand Down Expand Up @@ -228,8 +244,8 @@ vi.mock("@sentry/browser", () => ({
}));

describe("Testing initSentry", () => {
it("should initialize Sentry with the correct configuration", () => {
initSentry();
it("should initialize Sentry with the correct configuration", async () => {
await initSentry();

expect(Sentry.init).toHaveBeenCalledWith({
dsn: "https://f6181e1f6794abaf08674441d2c08403@o4507406315159552.ingest.de.sentry.io/4507406320992336",
Expand Down Expand Up @@ -263,3 +279,33 @@ describe("testing parse", () => {
expect(output.querySelectorAll("button")[0].innerText).toBe("4");
});
});

describe("testing postProcess", () => {
it("should return outputs when inputs and outputs lengths are equal", () => {
const inputs = ["2", "total", "3"];
const outputs = [2, "", 3];
const result = postProcess(inputs, outputs);
expect(result).toEqual([2, 2, 3]);
});

it("should return outputs adding previous inputs when inputs and outputs lengths are equal", () => {
const inputs = ["2", "5", "total", "3"];
const outputs = [2, 5, "", 3];
const result = postProcess(inputs, outputs);
expect(result).toEqual([2, 5, 7, 3]);
});

it("should handle different lengths of inputs and outputs", () => {
const inputs = ["2", "total", "3"];
const outputs = [2, 5];
const result = postProcess(inputs, outputs);
expect(result).toEqual([2, 5]);
});

it("should handle empty inputs", () => {
const inputs = [];
const outputs = [2, 5];
const result = postProcess(inputs, outputs);
expect(result).toEqual([2, 5]);
});
});
2 changes: 1 addition & 1 deletion js/help_page.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("createHelpTables tests", () => {
it("should create shortcut tables contents", () => {
const shortcutTables = document.getElementById("shortcut-table-container");
expect(shortcutTables.querySelectorAll("td")[4].outerHTML).toEqual(
"<td>Open/Close calculator History</td>"
"<td>Open/Close help</td>"
);
});

Expand Down

0 comments on commit f0c2588

Please sign in to comment.