Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing test coverage badge workflow #19

Merged
merged 4 commits into from
Jun 11, 2024
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
11 changes: 10 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ on:
jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]
Expand All @@ -36,6 +35,16 @@ jobs:
run: npm run build
- name: "Run Tests & Coverage"
run: npm run test
- name: Publish Results Badge
uses: wjervis7/[email protected]
if: success() || failure() # run whether steps succeed or not
with:
result-type: lines
gist-token: ${{ secrets.GIST_SECRET }} # if you want to upload badge to gist
gist-url: https://gist.github.com/gokulk16/eaf6c29242b70728224cc81c3f9ba750
upload-badge: true
badge-text: "coverage"
badge-path: "badge-${{ github.event.repository.name }}-${{ matrix.branch }}.svg"
- name: "Upload Coverage"
uses: actions/upload-artifact@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion test/currency.test.js → js/currency.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrency, getConversionRates } from "../js/currency";
import { getCurrency, getConversionRates } from "./currency";
import { expect, test } from "vitest";

test("get Home Currency given the country", () => {
Expand Down
68 changes: 11 additions & 57 deletions js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as Sentry from "@sentry/browser";
import * as currency from "./currency.js";
import { createHelpTables } from "./help_page.js";
import * as regex from "./regex.js";
import { convertXToMultiplication } from "./utils/convert_x_to_multiply.js";
import showToast from "show-toast";
import { LocalStorage } from "web-browser-storage";
import { createUnit, unit, evaluate as mathjs_evaluate } from "mathjs";
Expand Down Expand Up @@ -125,52 +125,6 @@ export async function setupEvaluator() {
return currencyUnitsAdded;
}

function removeMatches(supersetArray, removeArray) {
let result = supersetArray.filter((match) => !removeArray.includes(match));
return result;
}

function replaceFirstXAfterIndex(str, index) {
return str.substring(0, index) + str.substring(index).replace("x", "*");
}
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'
// then convert the 'x' to '*' in that line
// for these examples: '0x90 x 2', '0x90 x2', '0x90 x 2'
// then convert them to '0x90 * 2', '0x90 *2', '0x90 * 2' since here 0x represents hexadecimal value

let matchesOfX = [...lines[i].matchAll(regex.X_IN_EXPRESSION)];
let matchesOfHexa = [...lines[i].matchAll(regex.HEXADECIMAL_VALUE)];

let XIndices = matchesOfX.map((ele) => ele.index);
let HexaIndices = matchesOfHexa.map((ele) => ele.index);

let filteredMatches = removeMatches(XIndices, HexaIndices);
// filteredMatches contains all the indexes where the x will be present on or after the index

for (let index = 0; index < filteredMatches.length; index++) {
lines[i] = replaceFirstXAfterIndex(lines[i], filteredMatches[index]);
}

// if line still matches with regex.X_IN_EXPRESSION then go through the same operations once again
let confirmMatchesOfX = [...lines[i].matchAll(regex.X_IN_EXPRESSION)];

if (!_.isEmpty(confirmMatchesOfX)) {
matchesOfHexa = [...lines[i].matchAll(regex.HEXADECIMAL_VALUE)];

XIndices = confirmMatchesOfX.map((ele) => ele.index);
HexaIndices = matchesOfHexa.map((ele) => ele.index);
filteredMatches = removeMatches(XIndices, HexaIndices);
for (let index = 0; index < filteredMatches.length; index++) {
lines[i] = replaceFirstXAfterIndex(lines[i], filteredMatches[index]);
}
}
}
return lines;
}

function useMathJs(lines) {
var mjs_results = [];

Expand Down Expand Up @@ -421,17 +375,17 @@ function setupListeners() {
}

async function onEditorInput() {
parse(editor.innerText);
parse(editor.innerText, output);
saveData();
}

export function parse(value) {
output.innerText = "";
export function parse(value, outputElement) {
outputElement.innerText = "";
evaluate(value);
updateOutputDisplay();
updateOutputDisplay(outputElement);
}

function updateOutputDisplay() {
function updateOutputDisplay(outputElement) {
let results = getResultTokens();

for (const [i, result] of results.entries()) {
Expand All @@ -455,19 +409,19 @@ function updateOutputDisplay() {
button.classList.add("result-btn");
button.classList.add(result.type);
button.dataset.value = result.value;
output.appendChild(button);
outputElement.appendChild(button);
break;
case "error":
span = document.createElement("span");
span.innerText = chrome.i18n.getMessage("error");
span.setAttribute("title", value);
span.classList.add(result.type);
output.appendChild(span);
outputElement.appendChild(span);
break;
}

if (len > i + 1) {
output.appendChild(br);
outputElement.appendChild(br);
}
}
}
Expand Down Expand Up @@ -683,7 +637,7 @@ async function copyValueToClipboard(value) {
}
}

function initSentry() {
export function initSentry() {
Sentry.init({
dsn: "https://f6181e1f6794abaf08674441d2c08403@o4507406315159552.ingest.de.sentry.io/4507406320992336",
integrations: [
Expand All @@ -710,6 +664,6 @@ export async function init() {
focusEditor();
setupListeners();
evaluate(editor.innerText);
updateOutputDisplay();
updateOutputDisplay(output);
removeOverlay();
}
90 changes: 49 additions & 41 deletions test/editor.test.js → js/editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
evaluate,
loadPlaceholderData,
copyLastValue,
convertXToMultiplication,
getTitle,
} from "../js/editor";
import { describe, it, expect, vi, test } from "vitest";
initSentry,
parse,
} from "./editor";
import { describe, it, expect, vi, test, beforeEach } from "vitest";
import * as Sentry from "@sentry/browser";

// AAA Principle to write a test case: Arrange, Act, Assert

Expand Down Expand Up @@ -193,50 +195,13 @@ describe("testing copyLastValue", () => {
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(42);
});

it("should not attempt to copy when no numeric results are available", async () => {
it("should copy the recent non empty vaue if available", async () => {
global.evaluatedValues = [{ result: 21 }, { result: "" }];
await copyLastValue(global.evaluatedValues);
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");
Expand All @@ -255,3 +220,46 @@ describe("testing getTitle", () => {
expect(result).toBe("ThisIsAStringWithNoSpacesWithi");
});
});

vi.mock("@sentry/browser", () => ({
init: vi.fn(),
browserTracingIntegration: vi.fn(),
replayIntegration: vi.fn(),
}));

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

expect(Sentry.init).toHaveBeenCalledWith({
dsn: "https://f6181e1f6794abaf08674441d2c08403@o4507406315159552.ingest.de.sentry.io/4507406320992336",
integrations: [
Sentry.browserTracingIntegration(),
Sentry.replayIntegration(),
],
tracesSampleRate: 1.0,
tracePropagationTargets: ["localhost", /^https:\/\/typetocalculate\.in/],
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
});
});
});

describe("testing parse", () => {
beforeEach(() => {
document.body.innerHTML = `
<div id="editor" contenteditable="true">1+1</div>
<div id="output">3</div>
`;
global.editor = document.getElementById("editor");
global.output = document.getElementById("output");
});

it("should update the output value to 4", async () => {
editor.innerText = "2+2";

parse(editor.innerText, output);
await new Promise(setImmediate); // Wait for async operations
expect(output.querySelectorAll("button")[0].innerText).toBe("4");
});
});
2 changes: 1 addition & 1 deletion test/help_page.test.js → js/help_page.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHelpTables } from "../js/help_page";
import { createHelpTables } from "./help_page";
import { describe, it, expect } from "vitest";
import path from "path";
import fs from "fs";
Expand Down
5 changes: 0 additions & 5 deletions js/regex.js

This file was deleted.

50 changes: 50 additions & 0 deletions js/utils/convert_x_to_multiply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict";
import * as regex from "./regex.js";
var _ = require("lodash");

function removeMatches(supersetArray, removeArray) {
let result = supersetArray.filter((match) => !removeArray.includes(match));
return result;
}

function replaceFirstXAfterIndex(str, index) {
return str.substring(0, index) + str.substring(index).replace("x", "*");
}

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'
// then convert the 'x' to '*' in that line
// for these examples: '0x90 x 2', '0x90 x2', '0x90 x 2'
// then convert them to '0x90 * 2', '0x90 *2', '0x90 * 2' since here 0x represents hexadecimal value

let matchesOfX = [...lines[i].matchAll(regex.X_IN_EXPRESSION)];
let matchesOfHexa = [...lines[i].matchAll(regex.HEXADECIMAL_VALUE)];

let XIndices = matchesOfX.map((ele) => ele.index);
let HexaIndices = matchesOfHexa.map((ele) => ele.index);

let filteredMatches = removeMatches(XIndices, HexaIndices);
// filteredMatches contains all the indexes where the x will be present on or after the index

for (let index = 0; index < filteredMatches.length; index++) {
lines[i] = replaceFirstXAfterIndex(lines[i], filteredMatches[index]);
}

// if line still matches with regex.X_IN_EXPRESSION then go through the same operations once again
let confirmMatchesOfX = [...lines[i].matchAll(regex.X_IN_EXPRESSION)];

if (!_.isEmpty(confirmMatchesOfX)) {
matchesOfHexa = [...lines[i].matchAll(regex.HEXADECIMAL_VALUE)];

XIndices = confirmMatchesOfX.map((ele) => ele.index);
HexaIndices = matchesOfHexa.map((ele) => ele.index);
filteredMatches = removeMatches(XIndices, HexaIndices);
for (let index = 0; index < filteredMatches.length; index++) {
lines[i] = replaceFirstXAfterIndex(lines[i], filteredMatches[index]);
}
}
}
return lines;
}
39 changes: 39 additions & 0 deletions js/utils/convert_x_to_multiply.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, it, expect } from "vitest";
import { convertXToMultiplication } from "./convert_x_to_multiply";

describe("testing convertXToMultiplication", () => {
it("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",
]);
});

it("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",
]);
});
});
4 changes: 4 additions & 0 deletions js/utils/regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";

export const HEXADECIMAL_VALUE = /\b0x[0-9a-fA-F]/g;
export const X_IN_EXPRESSION = /[^a-zA-Z]\s*(x)\s*[^a-zA-Z]/g;
Loading