Skip to content

Commit b158399

Browse files
authored
Merge pull request #1 from mahdikhashan/test/unit
Setup test and cover validation with unit-tests
2 parents 775a308 + 0e6bacc commit b158399

File tree

5 files changed

+988
-2
lines changed

5 files changed

+988
-2
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@
3030
"build-watch": "rollup -c -w --bundleConfigAsCjs",
3131
"playground": "cd playground && yarn dev",
3232
"build-website": "cd website && yarn build && rm -rf ../docs && mkdir ../docs && mv ./dist/* ../docs",
33-
"install-all": "yarn install & cd playground && yarn install"
33+
"install-all": "yarn install & cd playground && yarn install",
34+
"test": "vitest --config vitest.config.js"
3435
},
3536
"devDependencies": {
3637
"@types/react": "^18.0.34",
3738
"@types/react-dom": "^18.0.11",
39+
"jsdom": "^21.1.1",
3840
"rollup": "^3.20.2",
3941
"rollup-plugin-delete": "^2.0.0",
4042
"rollup-plugin-typescript2": "^0.34.1",
41-
"typescript": "^5.0.4"
43+
"typescript": "^5.0.4",
44+
"vitest": "^0.30.1"
4245
},
4346
"peerDependencies": {
4447
"react": "^18.2.0",

src/tests/setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

src/tests/validation.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { it, expect } from 'vitest';
2+
import { isValidEmail, isValidUrl } from '../utils/validation';
3+
4+
it("an email name can contain a-zA-Z and numbers with - and .", () => {
5+
expect(isValidEmail("my-email.familyNAME1234@ymail.ae")).toBe(true);
6+
})
7+
8+
it("an email name can be short", () => {
9+
expect(isValidEmail("a@mail.com")).toBe(true);
10+
})
11+
12+
it("an email name can be long", () => {
13+
expect(isValidEmail("mygmailusernameistoolongtoberemembered@mail.com")).toBe(true);
14+
})
15+
16+
it("an email name can not contain special characters", () => {
17+
expect(isValidEmail("my@email.familyName1234@ymail.ae")).toBe(false);
18+
expect(isValidEmail("my&email@ymail.ae")).toBe(false);
19+
expect(isValidEmail("my$email@ymail.ae")).toBe(false);
20+
})
21+
22+
it("an email name can not contain persian or arabic characters", () => {
23+
expect(isValidEmail("مهدي@email.ir")).toBe(false);
24+
expect(isValidEmail("پیمان@gmail.ir")).toBe(false);
25+
})
26+
27+
it("a valid email should follow the username@server.domain format", () => {
28+
expect(isValidEmail("myname.mysurename.q")).toBe(false);
29+
})
30+
31+
it("valid url should have two slash after http[s]:", () => {
32+
expect(isValidUrl("https:/www.google.com")).toBe(false);
33+
})
34+
35+
it("valid url is formatted like http[s]://[www].name.domain", () => {
36+
expect(isValidUrl("https://www.github.com")).toBe(true);
37+
})
38+
39+
it("valid url can either be secured (https) or not (http)", () => {
40+
expect(isValidUrl("http://www.mo.nanoman.space/")).toBe(true);
41+
expect(isValidUrl("https://www.mo.nanoman.space/")).toBe(true);
42+
})
43+
44+
it("valid url should have http or https identifier", () => {
45+
expect(isValidUrl("www.github.com")).toBe(false);
46+
})
47+
48+
it("valid url can have persian or arabic name and domain", () => {
49+
expect(isValidUrl("www.ایران.ربات")).toBe(true);
50+
})

vitest.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
globals: true,
6+
environment: "jsdom",
7+
setupFiles: ["./src/tests/setup.ts"],
8+
}
9+
});

0 commit comments

Comments
 (0)