Skip to content

Commit

Permalink
Merge pull request #16 from gokulk16/r-13
Browse files Browse the repository at this point in the history
adding more tests to improve coverage
  • Loading branch information
gokulk16 authored Jun 6, 2024
2 parents 942c7f6 + ccd16d9 commit 57a926e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ jobs:
report-coverage:
needs: test
runs-on: ubuntu-latest
permissions:
# To comment in pull request
pull-requests: write
steps:
- name: "Download Coverage Artifacts"
uses: actions/download-artifact@v4
Expand Down
4 changes: 2 additions & 2 deletions js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function removeMatches(supersetArray, removeArray) {
function replaceFirstXAfterIndex(str, index) {
return str.substring(0, index) + str.substring(index).replace("x", "*");
}
function convertXToMultiplication(lines) {
export function convertXToMultiplication(lines) {
for (let i = 0; i < lines.length; i++) {
// Converting 'x' as a mutiplication operator.
// for these examples: 'data x 2', 'data x2', '2x2', '2 x2', '2x 2', '2 x 2', '2x data', '2 x data', '2x2x2', data x 2 x data', '2x2x2 x2 x x2 x2 x2 x 2 x 22'
Expand Down Expand Up @@ -575,7 +575,7 @@ let saveData = debounce(async function () {
storage.set(`type-to-calculate-${docId}`, currentDocData);
}, 500);

function getTitle(str) {
export function getTitle(str) {
if (str.length <= 0) return str;
let maxLength = 30;
str = str.trim();
Expand Down
58 changes: 58 additions & 0 deletions test/editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
evaluate,
loadPlaceholderData,
copyLastValue,
convertXToMultiplication,
getTitle,
} from "../js/editor";
import { describe, it, expect, vi, test } from "vitest";

Expand Down Expand Up @@ -197,3 +199,59 @@ describe("testing copyLastValue", () => {
expect(navigator.clipboard.writeText).toHaveBeenCalled(21);
});
});

describe("testing convertXToMultiplication", () => {
test("Convert all 'x' to multiplication operator", () => {
const lines = [
"2x3",
"2 x 3",
"2x 3",
"2 x3",
"2x data",
"data x 2",
"2x2x2x4x1.7x0.41",
];
const convertedLines = convertXToMultiplication(lines);

expect(convertedLines).toEqual([
"2*3",
"2 * 3",
"2* 3",
"2 *3",
"2* data",
"data * 2",
"2*2*2*4*1.7*0.41",
]);
});

test("Dont convert some 'x' to multiplication operator in expressions", () => {
const lines = ["2xdata", "0x90 x 2", "0x90x 2", "x22e x2x4x1.7x0.41"];
const convertedLines = convertXToMultiplication(lines);

expect(convertedLines).toEqual([
"2xdata",
"0x90 * 2",
"0x90* 2",
"x22e *2*4*1.7*0.41",
]);
});
});

describe("testing getTitle", () => {
test("Return full string when length is less than or equal to 30", () => {
const result = getTitle("This is a short string");
expect(result).toBe("This is a short string");
});

test("Return substring up to 30 characters with last word when length exceeds 30", () => {
const result = getTitle(
"This is a longer string that exceeds 30 characters"
);
expect(result).toBe("This is a longer string that");
});

test("Return full string when no spaces found within the first 30 characters", () => {
const result = getTitle("ThisIsAStringWithNoSpacesWithinFirst30Characters");
expect(result).toBe("ThisIsAStringWithNoSpacesWithi");
});
});

0 comments on commit 57a926e

Please sign in to comment.