Skip to content

Commit

Permalink
Merge pull request #18 from gokulk16/r-15
Browse files Browse the repository at this point in the history
refactoring help overlay page and increasing tests coverage +5%
  • Loading branch information
gokulk16 authored Jun 10, 2024
2 parents 94bcd02 + 0a46e0e commit 97ed1bc
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 69 deletions.
62 changes: 2 additions & 60 deletions js/editor.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
"use strict";
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 helpOperators from "./help_operators.json";
import helpShortcuts from "./help_shortcuts.json";
import helpUnits from "./help_units.json";
import showToast from "show-toast";
import { LocalStorage } from "web-browser-storage";
import { createUnit, unit, evaluate as mathjs_evaluate } from "mathjs";

const storage = new LocalStorage();
import showToast from "show-toast";
var _ = require("lodash");

let editor;
Expand Down Expand Up @@ -87,60 +83,6 @@ function onOverlayClick() {
focusEditor();
}

async function createHelpTables() {
// Operator Table
const operatorColumns = ["Name", "Operator", "Example"];
const operatorsDataa = [...helpOperators.operators];
const operatorTableHtml = createTable(operatorColumns, operatorsDataa);
document.getElementById("operator-table-container").innerHTML =
operatorTableHtml;

// Keyboard shortcut table
const shortcutColumns = ["Action", "Shortcut"];
const shortcutsData = [...helpShortcuts.shortcuts];
const shortcutTableHtml = createTable(shortcutColumns, shortcutsData);
document.getElementById("shortcut-table-container").innerHTML =
shortcutTableHtml;

// Units table
const unitColumns = ["Value", "Units"];
const unitsData = [...helpUnits.units];
const unitTableHtml = createTable(unitColumns, unitsData);
document.getElementById("unit-table-container").innerHTML = unitTableHtml;
}

function createTable(columns, data) {
let table = "<table>";

// Create table header
table += "<thead><tr>";
columns.forEach((column) => {
table += `<th>${column}</th>`;
});
table += "</tr></thead>";

// Create table body
table += "<tbody>";
data.forEach((row) => {
table += "<tr>";
columns.forEach((column) => {
table += `<td>`;

if (Array.isArray(row[column])) {
row[column].forEach((item) => {
table += `${item}</br>`;
});
} else {
table += `${row[column]}`;
}
table += "</td>";
});
table += "</tr>";
});
table += "</tbody></table>";
return table;
}

export async function setupEvaluator() {
homeCurrency = await setupHomeCurrency();
createUnit(homeCurrency.toLowerCase());
Expand Down
60 changes: 60 additions & 0 deletions js/help_page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Help page related activities happen in this File. Including DOM modifications

"use strict";
import helpOperators from "./help_operators.json";
import helpShortcuts from "./help_shortcuts.json";
import helpUnits from "./help_units.json";

export async function createHelpTables() {
// Operator Table
const operatorColumns = ["Name", "Operator", "Example"];
const operatorsDataa = [...helpOperators.operators];
const operatorTableHtml = createTable(operatorColumns, operatorsDataa);
document.getElementById("operator-table-container").innerHTML =
operatorTableHtml;

// Keyboard shortcut table
const shortcutColumns = ["Action", "Shortcut"];
const shortcutsData = [...helpShortcuts.shortcuts];
const shortcutTableHtml = createTable(shortcutColumns, shortcutsData);
document.getElementById("shortcut-table-container").innerHTML =
shortcutTableHtml;

// Units table
const unitColumns = ["Value", "Units"];
const unitsData = [...helpUnits.units];
const unitTableHtml = createTable(unitColumns, unitsData);
document.getElementById("unit-table-container").innerHTML = unitTableHtml;
}

function createTable(columns, data) {
let table = "<table>";

// Create table header
table += "<thead><tr>";
columns.forEach((column) => {
table += `<th>${column}</th>`;
});
table += "</tr></thead>";

// Create table body
table += "<tbody>";
data.forEach((row) => {
table += "<tr>";
columns.forEach((column) => {
table += `<td>`;

if (Array.isArray(row[column])) {
row[column].forEach((item) => {
table += `${item}</br>`;
});
} else {
table += `${row[column]}`;
}
table += "</td>";
});
table += "</tr>";
});
table += "</tbody></table>";
return table;
}
9 changes: 0 additions & 9 deletions js/localize.js

This file was deleted.

33 changes: 33 additions & 0 deletions test/help_page.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createHelpTables } from "../js/help_page";
import { describe, it, expect } from "vitest";
import path from "path";
import fs from "fs";

describe("createHelpTables tests", () => {
const indexPath = path.resolve(__dirname, "../html/index.html");
const indexHtml = fs.readFileSync(indexPath, "utf-8");
document.body.innerHTML = indexHtml;

createHelpTables();

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>"
);
});

it("should create operator tables contents", () => {
const operatorTables = document.getElementById("operator-table-container");
expect(operatorTables.querySelectorAll("td")[2].outerHTML).toEqual(
"<td>2 + 3 = 5</td>"
);
});

it("should create unit tables contents", () => {
const unitTables = document.getElementById("unit-table-container");
expect(unitTables.querySelectorAll("td")[5].outerHTML).toEqual(
"<td>m2, sqin, sqft, sqyd, sqmi, sqrd, sqch, sqmil, acre, hectare</td>"
);
});
});

0 comments on commit 97ed1bc

Please sign in to comment.