Skip to content

Commit 185b337

Browse files
committed
test: 🔥 test more file
1 parent b44078c commit 185b337

File tree

11 files changed

+12762
-13347
lines changed

11 files changed

+12762
-13347
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"eslint-config-prettier": "^6.13.0",
6767
"eslint-plugin-import": "^2.22.1",
6868
"eslint-plugin-prettier": "^3.1.4",
69+
"eslint-plugin-promise": "^4.3.1",
6970
"husky": "^4.3.0",
7071
"jest": "^26.5.3",
7172
"nodemon": "^2.0.5",
@@ -84,7 +85,6 @@
8485
"dependencies": {
8586
"axios": "^0.21.1",
8687
"camelcase-keys": "^6.2.2",
87-
"escape-string-regexp": "^4.0.0",
8888
"lodash.merge": "^4.6.2",
8989
"qs": "^6.9.6",
9090
"snakecase-keys": "^3.2.0"

src/__tests__/base-proxy.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let validator: ValidatorType
1515
describe('BaseProxy', () => {
1616
beforeEach(() => {
1717
validator = Validator
18-
const axios = Axios.create({ baseURL: 'http://mock-api.test' })
18+
const axios = Axios.create({ baseURL: 'https://mock-api.test' })
1919
BaseProxy.$http = axios
2020
proxy = new PostProxy()
2121
mockAdapter = new MockAdapter(axios)
@@ -333,8 +333,8 @@ describe('BaseProxy', () => {
333333
})
334334

335335
it('can accept a custom http instance in options', () => {
336-
BaseProxy.$http = Axios.create({ baseURL: 'http://another-example.com' })
337-
expect(proxy.$http.defaults.baseURL).toBe('http://another-example.com')
336+
BaseProxy.$http = Axios.create({ baseURL: 'https://another-example.com' })
337+
expect(proxy.$http.defaults.baseURL).toBe('https://another-example.com')
338338

339339
BaseProxy.$http = Axios.create()
340340
expect(proxy.$http.defaults.baseURL).toBe(undefined)

src/__tests__/form-data.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { objectToFormData } from '../util/formData'
2+
3+
describe('FormData', () => {
4+
it('if object is null, undefined or array length is zero', async () => {
5+
const formData = objectToFormData({
6+
null: '',
7+
items1: [],
8+
name: null,
9+
})
10+
expect(formData.get('null')).toBe('')
11+
expect(formData.get('items1')).toHaveLength(0)
12+
expect(formData.get('name')).toBe('')
13+
})
14+
})

src/__tests__/object.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { isFile, cloneDeep } from '../util/objects'
2+
3+
describe('Object Test', () => {
4+
const { window, File } = global
5+
afterEach(() => {
6+
global.window = window
7+
global.File = File
8+
})
9+
it('check if object is a file Instance', () => {
10+
const file = new File(['hello world!'], 'myfile')
11+
expect(isFile(file)).toBeTruthy()
12+
})
13+
it('check if window is undefined', () => {
14+
delete global.window
15+
const file = new File(['hello world!'], 'myfile')
16+
expect(isFile(file)).toBeFalsy()
17+
})
18+
it('check if File is not function', () => {
19+
const file = new File(['hello world!'], 'myfile')
20+
delete global.File
21+
expect(isFile(file)).toBeFalsy()
22+
})
23+
})
24+
describe('cloneDeep', () => {
25+
it('Object is null', () => {
26+
expect(cloneDeep(null)).toBe(null)
27+
})
28+
it('Object is typeof File', () => {
29+
const file = new File(['hello world!'], 'myfile')
30+
expect(cloneDeep(file)).toBeInstanceOf(File)
31+
})
32+
it('Object is typeof Array', () => {
33+
const obj = [{ name: 'Chantouch' }]
34+
expect(cloneDeep(obj)).toBeInstanceOf(Object)
35+
})
36+
})

src/core/BaseProxy.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class BaseProxy {
7070
}
7171

7272
submit(requestType: Method, url: string, form?: any): Promise<any> {
73-
this.__validateRequestType(requestType)
73+
BaseProxy.__validateRequestType(requestType)
7474
this.beforeSubmit()
7575
return new Promise((resolve, reject) => {
7676
const data = this.__hasFiles(form) ? objectToFormData(form) : form
@@ -100,12 +100,16 @@ class BaseProxy {
100100
})
101101
}
102102

103-
__getParameterString(url: string): string {
104-
const query = qs.stringify(this.parameters, { encode: false })
105-
return query ? `${url}?${query}` : url
103+
private __getParameterString(url: string): string {
104+
const query = qs.stringify(this.parameters, {
105+
encode: false,
106+
skipNulls: true,
107+
addQueryPrefix: true,
108+
})
109+
return `${url}${query}`
106110
}
107111

108-
__validateRequestType(requestType: Method): void {
112+
private static __validateRequestType(requestType: Method): void {
109113
const requestTypes: Array<string> = [
110114
'get',
111115
'delete',
@@ -122,7 +126,7 @@ class BaseProxy {
122126
}
123127
}
124128

125-
__hasFiles(form: any): boolean {
129+
private __hasFiles(form: any): boolean {
126130
for (const property in form) {
127131
if (!form.hasOwnProperty(property)) {
128132
return false
@@ -137,7 +141,7 @@ class BaseProxy {
137141
return false
138142
}
139143

140-
__hasFilesDeep(object: any): boolean {
144+
private __hasFilesDeep(object: any): boolean {
141145
if (object === null) {
142146
return false
143147
}

src/core/PaginationTransformer.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import BaseTransformer from './BaseTransformer'
22
import snakecaseKeys from 'snakecase-keys'
33

44
export interface PaginationOptions {
5-
per_page?: string | number
6-
total_pages?: string | number
7-
current_page?: string | number
8-
total?: string | number
5+
perPage: number
6+
totalPages: number
7+
totalCount: number
8+
limit: number
9+
currentPage: number
10+
total: number
911
links?: string | number
10-
count?: string | number
11-
page?: string | number
12-
items_per_page?: string | number
13-
page_count?: string | number
14-
items_length?: string | number
15-
page_start?: string | number
16-
page_stop?: string | number
12+
count: number
13+
page: number
14+
itemsPerPage?: number
15+
pageCount: number
16+
itemsLength: number
17+
pageStart?: number
18+
pageStop?: number
1719
include?: string | string[]
1820
}
1921

@@ -24,7 +26,7 @@ export interface MetaOptions {
2426

2527
class PaginationTransformer extends BaseTransformer {
2628
static fetch(meta: MetaOptions | any) {
27-
if (!meta.hasOwnProperty('pagination')) {
29+
if (!Object.prototype.hasOwnProperty.call(meta, 'pagination')) {
2830
return snakecaseKeys(meta, { deep: true })
2931
}
3032
const { pagination = {}, include } = meta

src/util/formData.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ export function objectToFormData(
22
object,
33
formData = new FormData(),
44
parent = null,
5-
): FormData | void {
5+
): FormData {
66
if (object === null || object === 'undefined' || object.length === 0) {
7-
return formData.append(parent, object)
8-
}
9-
for (const property in object) {
10-
if (!object.hasOwnProperty(property)) {
11-
continue
7+
formData.append(parent, object)
8+
} else {
9+
for (const property in object) {
10+
if (Object.hasOwnProperty.call(object, property)) {
11+
appendToFormData(formData, getKey(parent, property), object[property])
12+
}
1213
}
13-
appendToFormData(formData, getKey(parent, property), object[property])
1414
}
1515
return formData
1616
}

src/util/matcher.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/util/objects.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { isMatch } from './matcher'
2-
31
export function isArray(object: any): boolean {
42
return Object.prototype.toString.call(object) === '[object Array]'
53
}
@@ -16,10 +14,9 @@ export function isFile(object: any): boolean {
1614

1715
export function merge(a: any, b: any): string[] {
1816
for (const key in b) {
19-
if (!b.hasOwnProperty(key)) {
20-
continue
17+
if (Object.prototype.hasOwnProperty.call(b, key)) {
18+
a[key] = cloneDeep(b[key])
2119
}
22-
a[key] = cloneDeep(b[key])
2320
}
2421
return a
2522
}
@@ -37,7 +34,7 @@ export function cloneDeep(object: any): any {
3734
const clone: string[] = []
3835

3936
for (const key in object) {
40-
if (object.hasOwnProperty(key)) {
37+
if (Object.prototype.hasOwnProperty.call(object, key)) {
4138
clone[key] = cloneDeep(object[key])
4239
}
4340
}
@@ -49,7 +46,7 @@ export function cloneDeep(object: any): any {
4946
const clone = {}
5047

5148
for (const key in object) {
52-
if (object.hasOwnProperty(key)) {
49+
if (Object.prototype.hasOwnProperty.call(object, key)) {
5350
clone[key] = cloneDeep(object[key])
5451
}
5552
}
@@ -61,9 +58,6 @@ export function cloneDeep(object: any): any {
6158
}
6259

6360
export function is(errors: any, error: any): boolean {
64-
if (typeof error === 'string' && error.match(/[\*\!]/)) {
65-
return errors.filter((w: any) => isMatch(w, error)).length > 0
66-
}
6761
return isArray(error)
6862
? error.some((w) => is(errors, w))
6963
: errors.includes(error)

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"@types/node",
1313
"@nuxt/types",
1414
"@types/jest",
15-
"axios"
15+
"axios",
16+
"axios-mock-adapter"
1617
]
1718
},
1819
"include": ["src"],

0 commit comments

Comments
 (0)