diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index eaaf010..b492414 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -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 diff --git a/js/editor.js b/js/editor.js index 9321579..3b41c98 100644 --- a/js/editor.js +++ b/js/editor.js @@ -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' @@ -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(); diff --git a/test/editor.test.js b/test/editor.test.js index 8f59218..80161ee 100644 --- a/test/editor.test.js +++ b/test/editor.test.js @@ -4,6 +4,8 @@ import { evaluate, loadPlaceholderData, copyLastValue, + convertXToMultiplication, + getTitle, } from "../js/editor"; import { describe, it, expect, vi, test } from "vitest"; @@ -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"); + }); +});