Skip to content

Commit

Permalink
test: add ut for utils (NodeSecure#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly committed Jun 20, 2022
1 parent aad66ad commit 3a4f40c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"exports": "./index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "tap --node-arg='--no-warnings' --node-arg='--experimental-loader' --node-arg='@istanbuljs/esm-loader-hook' test/*test.js",
"start": "npm run build && http-server ./dist",
"build": "node esbuild.config.js"
},
Expand Down Expand Up @@ -33,9 +33,16 @@
"vis-network": "^9.1.2"
},
"devDependencies": {
"@istanbuljs/esm-loader-hook": "^0.1.2",
"@nodesecure/flags": "^2.3.0",
"@nodesecure/scanner": "^3.4.1",
"esbuild": "^0.14.42",
"http-server": "^14.1.0"
"http-server": "^14.1.0",
"tap": "^16.3.0"
},
"tap": {
"test-env": {
"NODE_OPTIONS": "--experimental-loader @istanbuljs/esm-loader-hook"
}
}
}
62 changes: 62 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import tap from "tap";
import { getFlagsEmojisInlined, getJSON, getNodeColor } from "../src/utils.js";
import * as CONSTANTS from "../src/constants.js";

const response = { message: "test should works" };
global.fetch = () => Promise.resolve({ json: () => response });
const json = await getJSON("random");

tap.equal(json, response, "getJSON should works");

const FLAGS = [
{
emoji: "🌍",
title: "hasExternalCapacity",
},
{
emoji: "🚧",
title: "hasWarnings",
},
{
emoji: "🐲",
title: "hasNativeCode",
},
];

const titles = FLAGS.map((flag) => flag.title);

tap.equal(
getFlagsEmojisInlined(titles, new Set()),
" 🌍 🚧 🐲",
"getFlagsEmojisInlined without flag to ignore"
);

tap.equal(
getFlagsEmojisInlined(titles, new Set(["hasWarnings"])),
" 🌍 🐲",
"getFlagsEmojisInlined with flag to ignore"
);

tap.equal(
getNodeColor(0),
CONSTANTS.COLORS["LIGHT"].SELECTED,
"id 0 is the root package (so by default he is highlighted as selected)."
);

tap.equal(
getNodeColor(1, true),
CONSTANTS.COLORS["LIGHT"].WARN,
"hasWarnings is true, so the node is highlighted as warning."
);

tap.equal(
getNodeColor(1, false),
CONSTANTS.COLORS["LIGHT"].DEFAULT,
"the node is highlighted as default."
);

tap.equal(
getNodeColor(1, false, "DARK"),
CONSTANTS.COLORS["DARK"].DEFAULT,
"the node is highlighted as default and the theme is DARK."
);

0 comments on commit 3a4f40c

Please sign in to comment.