Skip to content

Commit 79a5e50

Browse files
committed
fix(test): ✅ fixed and update some types
1 parent 40a9d75 commit 79a5e50

15 files changed

+84
-62
lines changed

jest.config.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ module.exports = {
55
collectCoverageFrom: ['src/**/*.ts', 'src/**/*.js'],
66
coverageThreshold: {
77
global: {
8-
// branches: 100,
9-
// functions: 100,
10-
// lines: 100,
11-
// statements: 100,
12-
branches: 65,
13-
functions: 70,
14-
lines: 50,
15-
statements: 50,
8+
lines: 100,
9+
functions: 100,
10+
branches: 89.44,
11+
statements: 99.36,
1612
},
1713
},
1814
testEnvironment: 'node',

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@
2929
"rest",
3030
"query",
3131
"builder",
32-
"laravel"
32+
"laravel",
33+
"queries",
34+
"vue-api-queries",
35+
"vue api queries",
36+
"api request"
3337
],
3438
"author": {
3539
"name": "Chantouch Sek",
36-
"email": "chantouchsek.cs83@gmail.com"
40+
"email": "chantouchsek.cs83@gmail.com",
41+
"url": "https://chantouch.me"
3742
},
3843
"license": "ISC",
3944
"bugs": {

src/__tests__/base-proxy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Axios from 'axios'
22
import BaseProxy from '../core/BaseProxy'
33
import MockAdapter from 'axios-mock-adapter'
4-
import PostProxy from '../core/PostPorxy'
4+
import PostProxy from '../util/PostPorxy'
55
import type { ValidatorType } from '../core/Validator'
66
import Validator from '../core/Validator'
77
import BaseTransformer from '../core/BaseTransformer'

src/__tests__/form-data.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { objectToFormData } from '../util/formData'
1+
import { objectToFormData } from '../util'
22

33
describe('FormData', () => {
44
it('if object is null, undefined or array length is zero', async () => {
@@ -11,4 +11,18 @@ describe('FormData', () => {
1111
expect(formData.get('items1')).toHaveLength(0)
1212
expect(formData.get('name')).toBe('')
1313
})
14+
it('window is undefined', async () => {
15+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
16+
// @ts-ignore
17+
delete global.window
18+
const formData = objectToFormData({
19+
null: '',
20+
items1: [],
21+
name: null,
22+
__proto__: 'Hi',
23+
})
24+
expect(formData.get('null')).toBe('')
25+
expect(formData.get('items1')).toHaveLength(0)
26+
expect(formData.get('name')).toBe('')
27+
})
1428
})

src/__tests__/object.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isFile, cloneDeep } from '../util/objects'
1+
import { isFile, cloneDeep } from '../util'
22

33
describe('Object Test', () => {
44
// const { window, File } = global

src/__tests__/post-proxy.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Axios from 'axios'
22
import BaseProxy from '../core/BaseProxy'
3-
import PostProxy from '../core/PostPorxy'
3+
import PostProxy from '../util/PostPorxy'
44
import MockAdapter from 'axios-mock-adapter'
55
import BaseTransformer from '../core/BaseTransformer'
66
import PaginationTransformer from '../core/PaginationTransformer'
@@ -35,4 +35,21 @@ describe('PostProxy', () => {
3535
expect(data.length).toEqual(1)
3636
expect(all.pagination.page).toEqual(1)
3737
})
38+
it('it should throw exception if method is not inlist', async () => {
39+
const items = {
40+
data: [{ name: 'Chantouch', post_id: 1 }],
41+
meta: {
42+
pagination: { count: 1, current_page: 1, perPage: 20 },
43+
include: [],
44+
},
45+
}
46+
mockAdapter.onGet('/posts/1/tags').reply(500, items)
47+
try {
48+
await proxy.throwException(1)
49+
} catch (e: any) {
50+
expect(e.message).toEqual(
51+
'`unlink` is not a valid request type, must be one of: `get`, `delete`, `head`, `post`, `put`, `patch`.',
52+
)
53+
}
54+
})
3855
})

src/__tests__/validator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ describe('Validator', () => {
152152
}
153153
validator.fill(errors)
154154
validator.onKeydown(event, 'form')
155-
expect(validator.has(['form.name'])).toBeFalsy()
155+
expect(validator.has(['form.name', 'name'])).toBeFalsy()
156156
})
157157

158158
it('can pass array of keys to any method and get back error of specified key', () => {

src/core/BaseProxy.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class BaseProxy {
187187
* @param {Object|string} form
188188
* @param {AxiosRequestConfig} config
189189
*/
190-
submit<T>(
190+
submit<T = any>(
191191
requestType: Method,
192192
parameter?: string | number,
193193
form?: T,
@@ -201,12 +201,11 @@ class BaseProxy {
201201
? `/${this.endpoint}/${parameter}`
202202
: `/${this.endpoint}`
203203
const endpoint = this.__getParameterString(removeDoubleSlash(url))
204-
const axiosConfig: AxiosRequestConfig = { data, method }
205-
this.$http(endpoint, Object.assign({}, config, axiosConfig))
204+
config = Object.assign({}, config, { data, method })
205+
this.$http(endpoint, config)
206206
.then((response: AxiosResponse) => {
207207
this.onSuccess()
208-
const { data } = response || {}
209-
resolve(data)
208+
resolve(response.data)
210209
})
211210
.catch((error: AxiosError) => {
212211
this.errors.processing = false

src/core/PaginationTransformer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ export interface PaginationOptions {
1717
pageStop?: number
1818
include?: string | string[]
1919
}
20-
2120
export interface MetaOptions {
2221
pagination?: PaginationOptions
2322
include?: string | string[]
2423
}
2524

2625
class PaginationTransformer extends BaseTransformer {
27-
static fetch(meta: MetaOptions | any) {
26+
static fetch(meta: MetaOptions | Record<string, any>) {
2827
if (!meta) {
2928
meta = { pagination: {}, include: [] }
3029
}

src/core/Validator.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { is, isArray } from '../util'
1+
import { hasOwnProperty, is, isArray } from '../util'
22

33
class Validator {
4-
public errors: any
4+
public errors: Record<string, any>
55
public successful: boolean
66
public processing: boolean
77

@@ -24,7 +24,7 @@ class Validator {
2424
if (isArray(field)) {
2525
return is(Object.keys(this.errors), field)
2626
}
27-
let hasError: boolean = this.errors.hasOwnProperty(field)
27+
let hasError = hasOwnProperty(this.errors, field)
2828
if (!hasError) {
2929
const errors = Object.keys(this.errors).filter(
3030
(e: string) => e.startsWith(`${field}.`) || e.startsWith(`${field}[`),
@@ -37,7 +37,7 @@ class Validator {
3737
first(field: any | any[]): string {
3838
if (field instanceof Array) {
3939
for (let i = 0; i < field.length; i++) {
40-
if (!this.errors.hasOwnProperty(field[i])) {
40+
if (!hasOwnProperty(this.errors, field[i])) {
4141
continue
4242
}
4343
return this.first(field[i])
@@ -46,7 +46,7 @@ class Validator {
4646
return this.get(field)[0]
4747
}
4848

49-
firstBy(obj: Record<string, any>, field = '') {
49+
firstBy(obj: Record<string, any>, field?: string) {
5050
let value
5151
if (!field) {
5252
value = obj[Object.keys(obj)[0]]
@@ -69,7 +69,7 @@ class Validator {
6969

7070
any(fields: string[] = [], returnObject?: boolean): boolean | string[] | any {
7171
if (returnObject) {
72-
const errors: any = {}
72+
const errors: Record<string, any> = {}
7373
if (!fields.length) {
7474
return {}
7575
}
@@ -79,7 +79,7 @@ class Validator {
7979
if (!fields.length) {
8080
return Object.keys(this.errors).length > 0
8181
}
82-
const errors: any = {}
82+
const errors: Record<string, any> = {}
8383
fields.forEach((key: string) => (errors[key] = this.get(key)))
8484
return Object.keys(errors).length > 0
8585
}
@@ -96,9 +96,9 @@ class Validator {
9696
return Object.keys(this.errors).length
9797
}
9898

99-
fill(errors: any) {
99+
fill(errors: Record<string, any>) {
100100
for (const error in errors) {
101-
if (!errors.hasOwnProperty(error)) {
101+
if (!hasOwnProperty(errors, error)) {
102102
continue
103103
}
104104
if (!(errors[error] instanceof Array)) {
@@ -148,8 +148,8 @@ class Validator {
148148
onKeydown(event: any, prefix?: string) {
149149
const { name } = event.target
150150
if (!name) return
151-
const name2 = prefix ? `${prefix}.${name}` : null
152-
this.clear([name, name2])
151+
const nameWithPrefix = prefix ? `${prefix}.${name}` : null
152+
this.clear([name, nameWithPrefix])
153153
}
154154
}
155155

0 commit comments

Comments
 (0)