Skip to content
This repository was archived by the owner on Dec 9, 2022. It is now read-only.

Commit 5ad6d91

Browse files
committed
added tests
1 parent 2ece68b commit 5ad6d91

File tree

6 files changed

+277
-61
lines changed

6 files changed

+277
-61
lines changed

.babelrc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
{
22
"presets": ["@babel/env"],
3-
"plugins": ["@babel/plugin-syntax-object-rest-spread"]
3+
"plugins": ["@babel/plugin-syntax-object-rest-spread"],
4+
"env": {
5+
"test": {
6+
"presets": [
7+
[
8+
"@babel/preset-env",
9+
{
10+
"targets": {
11+
"node": "current"
12+
}
13+
}
14+
],
15+
"@babel/preset-typescript"
16+
]
17+
}
18+
}
419
}

package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",
77
"scripts": {
8-
"build": "webpack --env dev && webpack --env build"
8+
"build": "webpack --env dev && webpack --env build",
9+
"test": "jest"
910
},
1011
"repository": {
1112
"type": "git",
@@ -26,13 +27,15 @@
2627
"author": "Martin Badin",
2728
"license": "MIT",
2829
"devDependencies": {
29-
"@babel/cli": "^7.8.3",
30-
"@babel/core": "^7.8.3",
31-
"@babel/parser": "^7.8.3",
30+
"@babel/cli": "^7.8.4",
31+
"@babel/core": "^7.8.4",
32+
"@babel/parser": "^7.8.4",
33+
"@babel/plugin-proposal-export-default-from": "^7.8.3",
3234
"@babel/plugin-syntax-object-rest-spread": "^7.8.3",
33-
"@babel/preset-env": "^7.8.3",
35+
"@babel/preset-env": "^7.8.4",
3436
"@babel/preset-typescript": "^7.8.3",
3537
"@babel/types": "^7.8.3",
38+
"@types/jest": "^25.1.1",
3639
"axios": "^0.19.2",
3740
"babel-eslint": "^10.0.3",
3841
"babel-loader": "^8.0.6",
@@ -46,5 +49,6 @@
4649
"vue-test-utils": "^1.0.0-beta.11",
4750
"webpack": "^4.41.5",
4851
"webpack-cli": "^3.3.10"
49-
}
52+
},
53+
"jest": {}
5054
}

src/plugin.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Vue from "vue";
2+
import plugin, { PluginOptions } from "./index";
3+
import axios from "axios";
4+
5+
const options: PluginOptions = {
6+
axios: axios
7+
};
8+
9+
Vue.use(plugin, options);
10+
11+
describe("plugin", () => {
12+
const app = new Vue();
13+
14+
it("is defined", () => {
15+
expect(app.$october).toBeDefined();
16+
});
17+
18+
it("has request function", () => {
19+
expect(app.$october.request).toBeInstanceOf(Function);
20+
});
21+
});

src/request.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { request, RequestProps, ERROR_MESSAGES } from "./request";
2+
import axios from "axios";
3+
4+
const onError = jest.fn();
5+
const onLoading = jest.fn();
6+
const onSuccess = jest.fn();
7+
8+
export const options: RequestProps = {
9+
formData: new FormData(),
10+
handler: "onSave",
11+
instance: axios,
12+
onError: onError,
13+
onLoading: onLoading,
14+
onSuccess: onSuccess,
15+
prevent: true
16+
// redirect?: string,
17+
};
18+
19+
describe("request", () => {
20+
it("missing handler", () => {
21+
expect(() => {
22+
// tslint:disable-next-line
23+
request({ handler: "" });
24+
}).toThrow(ERROR_MESSAGES["handle.required"]);
25+
});
26+
27+
it("invalid handler", () => {
28+
expect(() => {
29+
// tslint:disable-next-line
30+
request({ handler: "Save" });
31+
}).toThrow(ERROR_MESSAGES["handle.invalid"]);
32+
});
33+
34+
it("formData aren't an instance of FormData", () => {
35+
expect(() => {
36+
// tslint:disable-next-line
37+
request({ handler: options.handler, formData: {} });
38+
}).toThrow(ERROR_MESSAGES["formData.invalid"]);
39+
});
40+
41+
it("onLoading function isn't defined", () => {
42+
expect(() => {
43+
// tslint:disable-next-line
44+
request({ handler: options.handler, formData: null });
45+
}).toThrow(ERROR_MESSAGES["event.not.defined"]("onLoading"));
46+
});
47+
48+
it("onLoading function should be emited with true", () => {
49+
request(options);
50+
expect(onLoading).toBeCalledWith(true);
51+
});
52+
53+
it("onError function should be emited", () => {
54+
request(options);
55+
setTimeout(() => {
56+
expect(onError).toBeCalled();
57+
}, 1000);
58+
});
59+
60+
it("onLoading function should be emited with false", () => {
61+
request(options);
62+
setTimeout(() => {
63+
expect(onLoading).toBeCalledWith(false);
64+
}, 1000);
65+
});
66+
});

src/request.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface ErrorResponse {
1010
}
1111

1212
export interface RequestProps {
13-
readonly formData: FormData;
13+
readonly formData: FormData | null;
1414
readonly handler: string;
1515
readonly instance: AxiosInstance;
1616
readonly onError?: (value: any) => void;
@@ -24,29 +24,42 @@ export type RequestFunction = {
2424
(options: RequestProps): () => void;
2525
};
2626

27+
export const ERROR_MESSAGES = {
28+
"handle.required": "Handler name is required",
29+
"handle.invalid":
30+
'Invalid handler name. The correct handler name format is: "onEvent".',
31+
32+
"formData.invalid": "The formData is not instance of FormData",
33+
34+
"event.not.defined": (value: string) =>
35+
`Event ${value} must be type of function.`
36+
};
37+
2738
export function request({
2839
formData,
2940
handler,
3041
instance,
3142
redirect,
3243
...bag
3344
}: RequestProps) {
34-
if (!handler || (handler && !handler.match(/^(?:\w+\:{2})?on*/))) {
35-
throw new Error(
36-
'Invalid handler name. The correct handler name format is: "onEvent".'
37-
);
45+
if (!handler) {
46+
throw new Error(ERROR_MESSAGES["handle.required"]);
47+
}
48+
49+
if (handler && !handler.match(/^(?:\w+\:{2})?on*/)) {
50+
throw new Error(ERROR_MESSAGES["handle.invalid"]);
3851
}
3952

4053
if (formData && !(formData instanceof FormData)) {
41-
throw new Error("The formData is not instance of FormData");
54+
throw new Error(ERROR_MESSAGES["formData.invalid"]);
4255
}
4356

44-
function emit<T>(name: "Loading" | "Error" | "Success", data: T) {
57+
function emit<T>(name: "Loading" | "Error" | "Success", data: T | undefined) {
4558
const eventName = `on${name}`;
4659
const func = (bag as Record<string, any>)[eventName];
4760

48-
if (func && typeof func !== "function") {
49-
throw new Error(`Event ${eventName} must be type of function.`);
61+
if (typeof func !== "function") {
62+
throw new Error(ERROR_MESSAGES["event.not.defined"](eventName));
5063
}
5164

5265
func(data);
@@ -55,7 +68,7 @@ export function request({
5568
emit("Loading", true);
5669

5770
instance
58-
.post("", formData || null, {
71+
.post("", formData, {
5972
headers: {
6073
"X-OCTOBER-REQUEST-HANDLER": handler
6174
}

0 commit comments

Comments
 (0)