Skip to content

Commit

Permalink
Add Jest configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
silentsoft committed Mar 6, 2022
1 parent 69ee894 commit e7e2faa
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
16 changes: 16 additions & 0 deletions config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ module.exports = {
paths.appBuild = path.resolve(__dirname, 'target/classes/public');
return paths;
},
jest: function(config) {
config.rootDir = path.resolve(__dirname, 'src/main/react');
config.roots = [
'<rootDir>'
];
config.testMatch = [
'<rootDir>/**/__tests__/**/*.{js,jsx,ts,tsx}',
'<rootDir>/**/*.{spec,test}.{js,jsx,ts,tsx}'
];
config.collectCoverage = true;
config.collectCoverageFrom = [
'<rootDir>/**/*.{js,jsx,ts,tsx}'
];
config.coverageDirectory = path.resolve(__dirname, 'coverage');
return config;
},
webpack: override(
addPostcssPlugins([
require('tailwindcss'),
Expand Down
2 changes: 1 addition & 1 deletion src/main/react/utils/UniformedResourceNameUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source : src/main/java/org/silentsoft/hits/utils/UniformedResourceNameUtils.java
*/
export const normalize = (uri) => {
if (uri.length === 0) {
if (uri == null || uri.length === 0) {
return "";
}

Expand Down
42 changes: 42 additions & 0 deletions src/main/react/utils/UniformedResourceNameUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
source : src/test/java/org/silentsoft/hits/utils/UniformedResourceNameUtilsTest.java
*/
import {normalize} from "./UniformedResourceNameUtils";

test("normalize", () => {
expect(normalize(null)).toBe("");
expect(normalize(undefined)).toBe("");
expect(normalize("")).toBe("");
expect(normalize("/")).toBe("");
expect(normalize("http:/")).toBe("");
expect(normalize("http://")).toBe("");

expect(normalize("github.com/silentsoft/hits")).toBe("github.com/silentsoft/hits");
expect(normalize("github.com/silentsoft/hits/")).toBe("github.com/silentsoft/hits");
expect(normalize("/github.com/silentsoft/hits")).toBe("github.com/silentsoft/hits");
expect(normalize("/github.com/silentsoft/hits/")).toBe("github.com/silentsoft/hits");
expect(normalize("github.com/silentsoft/hits?")).toBe("github.com/silentsoft/hits");
expect(normalize("github.com/silentsoft/hits?a=b")).toBe("github.com/silentsoft/hits?a=b");
expect(normalize("github.com/silentsoft/hits#")).toBe("github.com/silentsoft/hits");
expect(normalize("github.com/silentsoft/hits#readme")).toBe("github.com/silentsoft/hits");

expect(normalize("http:/github.com/silentsoft/hits")).toBe("github.com/silentsoft/hits");
expect(normalize("http://github.com/silentsoft/hits")).toBe("github.com/silentsoft/hits");
expect(normalize("https:/github.com/silentsoft/hits")).toBe("github.com/silentsoft/hits");
expect(normalize("https://github.com/silentsoft/hits")).toBe("github.com/silentsoft/hits");

expect(normalize("https://github.com/silentsoft/hits########")).toBe("github.com/silentsoft/hits");
expect(normalize("https://github.com/silentsoft/hits????####")).toBe("github.com/silentsoft/hits");
expect(normalize("https://github.com/silentsoft/hits??//####")).toBe("github.com/silentsoft/hits");
expect(normalize("https://github.com/silentsoft/hits#?//####")).toBe("github.com/silentsoft/hits");
expect(normalize("https://github.com/silentsoft/hits????????")).toBe("github.com/silentsoft/hits");
expect(normalize("https://github.com/silentsoft/hits////////")).toBe("github.com/silentsoft/hits");
expect(normalize("https://github.com/silentsoft/hits//??//??")).toBe("github.com/silentsoft/hits");
expect(normalize("https://github.com/silentsoft/hits??//??//")).toBe("github.com/silentsoft/hits");

expect(normalize("https://한글도메인.com/가나다라")).toBe("한글도메인.com/가나다라");
expect(normalize("https://한글도메인.com/가나다라/")).toBe("한글도메인.com/가나다라");
expect(normalize("https://한글도메인.com/가나다라#")).toBe("한글도메인.com/가나다라");
expect(normalize("https://한글도메인.com/가나다라?")).toBe("한글도메인.com/가나다라");
expect(normalize("https://한글도메인.com/가나다라?a=b")).toBe("한글도메인.com/가나다라?a=b");
});
4 changes: 4 additions & 0 deletions src/main/react/utils/Utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export default class Utils {
static toQueryString(object) {
if (object == null) {
return "";
}

let queryString = "";

const defaultViewType = "total";
Expand Down
55 changes: 55 additions & 0 deletions src/main/react/utils/Utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Utils from "./Utils";

test("toQueryString", () => {
expect(Utils.toQueryString(null)).toBe("");
expect(Utils.toQueryString(undefined)).toBe("");
expect(Utils.toQueryString({})).toBe("");
expect(Utils.toQueryString({foo: "bar"})).toBe("");

expect(Utils.toQueryString({view: "today-total"})).toBe("?view=today-total");
expect(Utils.toQueryString({style: "flat-square"})).toBe("?style=flat-square");
expect(Utils.toQueryString({label: "foo"})).toBe("?label=foo");
expect(Utils.toQueryString({extraCount: 1000})).toBe("?extraCount=1000");
expect(Utils.toQueryString({color: "black"})).toBe("?color=black");
expect(Utils.toQueryString({color: "123"})).toBe("?color=123");
expect(Utils.toQueryString({color: "#123"})).toBe("?color=123");
expect(Utils.toQueryString({color: "123456"})).toBe("?color=123456");
expect(Utils.toQueryString({color: "#123456"})).toBe("?color=123456");
expect(Utils.toQueryString({labelColor: "black"})).toBe("?labelColor=black");
expect(Utils.toQueryString({labelColor: "123"})).toBe("?labelColor=123");
expect(Utils.toQueryString({labelColor: "#123"})).toBe("?labelColor=123");
expect(Utils.toQueryString({labelColor: "123456"})).toBe("?labelColor=123456");
expect(Utils.toQueryString({labelColor: "#123456"})).toBe("?labelColor=123456");
expect(Utils.toQueryString({link: "https://hits.sh"})).toBe("?link=https%3A%2F%2Fhits.sh");
expect(Utils.toQueryString({link: ["https://hits.sh", "https://hits.sh"]})).toBe("?link=https%3A%2F%2Fhits.sh&link=https%3A%2F%2Fhits.sh");
expect(Utils.toQueryString({logo: "logo"})).toBe("?logo=logo");
expect(Utils.toQueryString({logoWidth: 48})).toBe("?logoWidth=48");
expect(Utils.toQueryString({
view: "today-total",
style: "flat-square",
label: "foo",
extraCount: 1000,
color: "black",
labelColor: "black",
link: "https://hits.sh",
logo: "logo",
logoWidth: 48
})).toBe("?view=today-total&style=flat-square&label=foo&extraCount=1000&color=black&labelColor=black&link=https%3A%2F%2Fhits.sh&logo=logo&logoWidth=48");
});

test("toQueryString with default value", () => {
expect(Utils.toQueryString({view: "total"})).toBe("");
expect(Utils.toQueryString({style: "flat"})).toBe("");
expect(Utils.toQueryString({label: "hits"})).toBe("");
expect(Utils.toQueryString({color: "4c1"})).toBe("");
expect(Utils.toQueryString({color: "#4c1"})).toBe("");
expect(Utils.toQueryString({color: "44cc11"})).toBe("");
expect(Utils.toQueryString({color: "#44cc11"})).toBe("");
expect(Utils.toQueryString({color: "brightgreen"})).toBe("");
expect(Utils.toQueryString({labelColor: "555"})).toBe("");
expect(Utils.toQueryString({labelColor: "#555"})).toBe("");
expect(Utils.toQueryString({labelColor: "555555"})).toBe("");
expect(Utils.toQueryString({labelColor: "#555555"})).toBe("");
expect(Utils.toQueryString({labelColor: "grey"})).toBe("");
expect(Utils.toQueryString({labelColor: "gray"})).toBe("");
});

0 comments on commit e7e2faa

Please sign in to comment.