Skip to content

Commit

Permalink
chore(unit-tests): add minimal unit-test-setup
Browse files Browse the repository at this point in the history
  • Loading branch information
luhmann committed Mar 22, 2018
1 parent 0968d16 commit 1fd2f2d
Show file tree
Hide file tree
Showing 5 changed files with 1,056 additions and 46 deletions.
35 changes: 35 additions & 0 deletions __tests__/Utils_test.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
open Jest;

open Expect;

describe("`keyHasher`", () =>
test("should return the expected hash", () =>
expect(Utils.keyHasher("It was a long and lonely road"))
|> toEqual("2476213412")
)
);

describe("`generateImgUrl`", () =>
test("should build asset-server-url", () =>
expect(Utils.generateImgUrl("test"))
|> toBe(
"https://s3.eu-central-1.amazonaws.com/punk-api-images/resized/test.jpg"
)
)
);

describe("`getFileName`", () => {
describe("with valid url", () =>
test("should extract the filename without extension", () =>
expect(
Utils.getFilename("http:https://www.example.org/v2/tarball/example.png")
)
|> toEqual(Some("example"))
)
);
describe("with empty url", () =>
test("should return `None`", () =>
expect(Utils.getFilename("")) |> toEqual(None)
)
);
});
9 changes: 8 additions & 1 deletion bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
{
"name": "react-template",
"reason": { "react-jsx": 2 },
"sources": ["src"],
"sources": [
"src",
{
"dir": "__tests__",
"type": "dev"
}
],
"package-specs": [
{
"module": "commonjs",
Expand All @@ -15,5 +21,6 @@
"suffix": ".bs.js",
"namespace": true,
"bs-dependencies": ["reason-react", "bs-axios", "re-classnames"],
"bs-dev-dependencies": ["@glennsl/bs-jest"],
"refmt": 3
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"start": "bsb -make-world -w",
"clean": "bsb -clean-world",
"test": "cypress run",
"test:unit": "jest",
"dev": "webpack-serve --mode development",
"host": "hs build -p 8080 --silent",
"ci": "start-server-and-test host http:https://localhost:8080 test"
"ci":
"yarn test:unit && start-server-and-test host http:https://localhost:8080 test"
},
"keywords": ["BuckleScript"],
"author": "",
Expand All @@ -23,12 +25,14 @@
"reason-react": ">=0.3.0"
},
"devDependencies": {
"@glennsl/bs-jest": "^0.4.2",
"autoprefixer": "^8.1.0",
"bs-platform": "^2.2.2",
"css-loader": "^0.28.10",
"cypress": "^2.1.0",
"html-webpack-plugin": "^3.0.4",
"http-server": "^0.11.1",
"jest": "^22.4.3",
"mini-css-extract-plugin": "^0.2.0",
"postcss-loader": "^2.1.1",
"purgecss-webpack-plugin": "^0.20.1",
Expand Down
23 changes: 15 additions & 8 deletions src/Utils.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[@bs.module "murmurhash"] external v3 : string => string = "v3";
[@bs.module "murmurhash"] external v3 : string => int = "v3";

let keyHasher = v3;
let keyHasher = input => v3(input) |> string_of_int;

let jsErrorToExn = jsError => {
let isObject = rawObj =>
Expand Down Expand Up @@ -48,12 +48,19 @@ let logError = err => {
[@bs.scope ("window", "location")] [@bs.val]
external basePath : string = "pathname";

let getFilename = url =>
Js.String.split("/", url)
|> Js.Array.pop
|> Js.Option.getWithDefault("")
|> Js.String.split(".")
|> Js.Array.shift;
let getFilename = url => {
let filename =
url
|> Js.String.split("/")
|> Js.Array.pop
|> Js.Option.getWithDefault("")
|> Js.String.split(".")
|> Js.Array.shift;
switch filename {
| Some(filename) when String.length(filename) > 0 => Some(filename)
| _ => None
};
};

let generateImgUrl = filename =>
"https://s3.eu-central-1.amazonaws.com/punk-api-images/resized/"
Expand Down
Loading

0 comments on commit 1fd2f2d

Please sign in to comment.