Skip to content

Commit 1aefa34

Browse files
authored
Merge pull request #655 from chantouchsek/feat/648-vites
Feat/648 vitest
2 parents 32a8fd6 + 400e5b9 commit 1aefa34

File tree

16 files changed

+2888
-4117
lines changed

16 files changed

+2888
-4117
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ jobs:
2727
- run: yarn
2828
- run: yarn lint
2929
- run: yarn build
30-
- run: yarn test
30+
- run: yarn test:cov

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"trailingComma": "all",
44
"semi": false,
55
"endOfLine": "auto",
6-
"singleQuote": true
6+
"singleQuote": true,
7+
"printWidth": 120
78
}

README.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,7 @@ It can be called by `this.$errors.**`
430430

431431
```vue
432432
<template>
433-
<v-form
434-
v-model="valid"
435-
lazy-validation
436-
@keydown.native="$errors.onKeydown"
437-
@submit.prevent="submit"
438-
>
433+
<v-form v-model="valid" lazy-validation @keydown.native="$errors.onKeydown" @submit.prevent="submit">
439434
<v-container>
440435
<v-row>
441436
<v-col cols="12" md="4">
@@ -458,13 +453,7 @@ It can be called by `this.$errors.**`
458453
/>
459454
</v-col>
460455
<v-col cols="12" md="4">
461-
<v-text-field
462-
v-model="email"
463-
:counter="10"
464-
label="Email"
465-
required
466-
:error-messages="$errors.first('email')"
467-
/>
456+
<v-text-field v-model="email" :counter="10" label="Email" required :error-messages="$errors.first('email')" />
468457
</v-col>
469458
<v-col cols="12" md="4">
470459
<v-text-field v-model="email" label="E-mail" required />

jest.config.ts

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

package.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"scripts": {
8-
"test": "jest --passWithNoTests --bail --detectOpenHandles --verbose --env=jsdom",
9-
"test:watch": "jest --watchAll",
10-
"test:watch:jsdom": "jest --watchAll --env=jsdom",
8+
"test": "vitest --run",
9+
"test:cov": "vitest run --coverage",
10+
"test:watch": "yarnr test",
1111
"build": "yarn clean && tsc",
1212
"watch": "tsc -w",
1313
"start": "nodemon",
@@ -50,31 +50,32 @@
5050
"@commitlint/cli": "^17.0.0",
5151
"@commitlint/config-conventional": "^17.0.0",
5252
"@nuxt/types": "^2.15.8",
53-
"@types/jest": "^27.4.0",
5453
"@types/lodash": "^4.14.182",
5554
"@types/node": "^18.0.0",
5655
"@types/qs": "^6.9.7",
57-
"@typescript-eslint/eslint-plugin": "^5.9.0",
56+
"@typescript-eslint/eslint-plugin": "^5.49.0",
5857
"@typescript-eslint/parser": "^5.9.0",
59-
"@vue/test-utils": "^1.2.2",
58+
"@vitest/coverage-c8": "^0.28.1",
59+
"@vue/test-utils": "^1.3.3",
6060
"axios-mock-adapter": "^1.20.0",
6161
"eslint": "^8.13.0",
6262
"eslint-config-prettier": "^8.3.0",
6363
"eslint-plugin-import": "^2.24.2",
6464
"eslint-plugin-prettier": "^4.0.0",
6565
"eslint-plugin-promise": "^6.0.0",
6666
"husky": "^8.0.1",
67-
"jest": "^27.4.5",
67+
"jsdom": "^21.1.0",
6868
"lint-staged": ">=10",
6969
"nodemon": "^2.0.12",
70-
"nuxt-edge": "^2.16.0-27217455.034b9901",
70+
"nuxt": "^2.15.8",
7171
"prettier": "^2.4.1",
72-
"rimraf": "^4.1.1",
72+
"rimraf": "^4.1.2",
7373
"standard-version": "^9.3.1",
74-
"ts-jest": "^27.1.1",
7574
"ts-node": "^10.9.1",
76-
"typescript": "^4.5.3",
77-
"vue": "^2.6.14"
75+
"typescript": "^4.9.4",
76+
"vite": "^4.0.4",
77+
"vitest": "^0.28.1",
78+
"vue": "^2.7.14"
7879
},
7980
"files": [
8081
"dist",

src/__tests__/__snapshots__/package.spec.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Vitest Snapshot v1
22

3-
exports[`Vue Api Queries Get errors from vue component 1`] = `
4-
Object {
3+
exports[`Vue Api Queries > Get errors from vue component 1`] = `
4+
{
55
"$_vueTestUtils_original": [Circular],
6-
"_Ctor": Object {},
6+
"_Ctor": {},
77
"data": [Function],
88
"name": undefined,
99
"render": [Function],
10-
"staticRenderFns": Array [],
10+
"staticRenderFns": [],
1111
"template": "
1212
<div>
1313
<input type=\\"checkbox\\" name=\\"t1\\" class=\\"foo\\" v-model=\\"t1\\" />
@@ -18,7 +18,7 @@ Object {
1818
}
1919
`;
2020
21-
exports[`Vue Api Queries Get plugin installed 1`] = `
21+
exports[`Vue Api Queries > Get plugin installed 1`] = `
2222
<body>
2323
<div
2424
data-app="true"

src/__tests__/base-service.test.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ describe('BaseService', () => {
8989
{ first_name: 'Chantouch', last_name: 'Sek', id: 2 },
9090
]
9191
mockAdapter.onGet('/posts?id=1&first_name=Dara').reply(200, { data: items })
92-
const { data } = await service
93-
.setParameter('id', 1)
94-
.setParameters({ first_name: 'Dara' })
95-
.all()
92+
const { data } = await service.setParameter('id', 1).setParameters({ first_name: 'Dara' }).all()
9693
expect(data).toEqual(items)
9794
})
9895

@@ -110,9 +107,7 @@ describe('BaseService', () => {
110107
pc: null,
111108
})
112109
const items = [user2, { first_name: 'Chantouch', last_name: 'Sek', id: 2 }]
113-
mockAdapter
114-
.onGet('/posts?id=1&last_name=Hok&search[name]=hello&first_name=Dara')
115-
.reply(200, { data: items })
110+
mockAdapter.onGet('/posts?id=1&last_name=Hok&search[name]=hello&first_name=Dara').reply(200, { data: items })
116111
const { data } = await service
117112
.setParameter('id=1&last_name=Hok&search[name]=hello')
118113
.setParameters({ first_name: 'Dara' })
@@ -148,18 +143,13 @@ describe('BaseService', () => {
148143
{ first_name: 'Dara', last_name: 'Hok', id: 1 },
149144
{ first_name: 'Chantouch', last_name: 'Sek', id: 2 },
150145
]
151-
mockAdapter
152-
.onGet('/posts?search[id]=1&first_name=Dara')
153-
.reply(200, { data: items })
146+
mockAdapter.onGet('/posts?search[id]=1&first_name=Dara').reply(200, { data: items })
154147
const params = {
155148
search: { id: 1 },
156149
first_name: 'Dara',
157150
last_name: 'Hok',
158151
}
159-
const { data } = await service
160-
.setParameters(params)
161-
.removeParameters(['last_name'])
162-
.all()
152+
const { data } = await service.setParameters(params).removeParameters(['last_name']).all()
163153
expect(data).toEqual(items)
164154
expect(service.parameters).toEqual({
165155
search: { id: 1 },
@@ -266,12 +256,7 @@ describe('BaseService', () => {
266256
expect(request.data.get('files[0]')).toEqual(file)
267257
expect(request.data.get('__proto__')).toEqual(null)
268258

269-
expect(getFormDataKeys(request.data)).toEqual([
270-
'field1[foo]',
271-
'field1[bar]',
272-
'field2',
273-
'files[0]',
274-
])
259+
expect(getFormDataKeys(request.data)).toEqual(['field1[foo]', 'field1[bar]', 'field2', 'files[0]'])
275260
return [200, {}]
276261
})
277262

@@ -359,7 +344,5 @@ describe('BaseService', () => {
359344

360345
function getFormDataKeys(formData: any) {
361346
// This is because the FormData.keys() is missing from the jsdom implementations.
362-
return formData[Object.getOwnPropertySymbols(formData)[0]]._entries.map(
363-
(e: any) => e.name,
364-
)
347+
return formData[Object.getOwnPropertySymbols(formData)[0]]._entries.map((e: any) => e.name)
365348
}

src/__tests__/validator.test.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ describe('Validator', () => {
3333
test('Check if error has "name" key.', () => {
3434
validator.add('name', 'The name field is required.')
3535
expect(validator.has('name')).toBeTruthy()
36-
expect(validator.first(['name', 'form.name'])).toBe(
37-
'The name field is required.',
38-
)
36+
expect(validator.first(['name', 'form.name'])).toBe('The name field is required.')
3937
})
4038
test('Check if has error by multi key', () => {
4139
validator.add('name', 'The name field is required.')
@@ -88,7 +86,8 @@ describe('Validator', () => {
8886
})
8987
test('Clear all errors by flush', () => {
9088
const errors = {
91-
name: ['The name field is required.'],
89+
nameKh: ['The name field is required.'],
90+
name_kh: ['The name field is required.'],
9291
email: ['The email field is required.'],
9392
}
9493
validator.fill(errors)
@@ -201,9 +200,7 @@ describe('Validator', () => {
201200
age: ['This age field is required'],
202201
}
203202

204-
expect(validator.firstBy(errors, 'age')).toEqual(
205-
'This age field is required',
206-
)
203+
expect(validator.firstBy(errors, 'age')).toEqual('This age field is required')
207204
})
208205

209206
it('get first by without any field', () => {
@@ -213,9 +210,7 @@ describe('Validator', () => {
213210
age: ['This age field is required'],
214211
}
215212

216-
expect(validator.firstBy(errors)).toEqual(
217-
'This fist name field is required',
218-
)
213+
expect(validator.firstBy(errors)).toEqual('This fist name field is required')
219214
})
220215

221216
it('get first array by nested array', () => {
@@ -229,8 +224,19 @@ describe('Validator', () => {
229224
const errors = { name: [{ kh: ['This fist name field is required'] }] }
230225
validator.fill(errors)
231226

232-
expect(validator.first(['name[0].kh'])).toEqual(
233-
'This fist name field is required',
234-
)
227+
expect(validator.first(['name[0].kh'])).toEqual('This fist name field is required')
228+
})
229+
230+
it('should reflect the field name by camel or snake case', () => {
231+
const errors = { firstName: ['This fist name field is required'] }
232+
validator.fill(errors)
233+
234+
expect(validator.has(['first_name'])).toBeTruthy()
235+
expect(validator.first(['first_name'])).toEqual('This fist name field is required')
236+
expect(validator.first(['firstName'])).toEqual('This fist name field is required')
237+
238+
validator.clear(['first_name'])
239+
240+
expect(validator.has(['firstName'])).toBeFalsy()
235241
})
236242
})

src/core/BaseService.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import type {
2-
AxiosError,
3-
AxiosInstance,
4-
AxiosResponse,
5-
Method,
6-
AxiosRequestConfig,
7-
} from 'axios'
1+
import type { AxiosError, AxiosInstance, AxiosResponse, Method, AxiosRequestConfig } from 'axios'
82
import type { Errors } from '..'
93
import Validator from './Validator'
104
import { hasFiles, objectToFormData, removeDoubleSlash } from '../util'
@@ -48,23 +42,23 @@ class BaseService {
4842
return BaseService.$parsedQs
4943
}
5044

51-
all<T>() {
45+
all<T = any>() {
5246
return this.submit<T>('get')
5347
}
5448

55-
find<T>(id: number | string) {
49+
find<T = any>(id: number | string) {
5650
return this.submit<T>('get', id)
5751
}
5852

59-
post<T>(payload: any, config?: AxiosRequestConfig) {
53+
post<T = any>(payload: any, config?: AxiosRequestConfig) {
6054
return this.submit<T>('post', '', payload, config)
6155
}
6256

63-
store<T>(payload: any, config?: AxiosRequestConfig) {
57+
store<T = any>(payload: any, config?: AxiosRequestConfig) {
6458
return this.post<T>(payload, config)
6559
}
6660

67-
put<T>(id: any, payload?: any, config?: AxiosRequestConfig) {
61+
put<T = any>(id: any, payload?: any, config?: AxiosRequestConfig) {
6862
const parameter = id && !isObject(id) ? `/${id}` : ''
6963
const body = isObject(id) ? id : payload
7064
const requestType: Method = hasFiles(body) ? 'post' : 'put'
@@ -74,43 +68,36 @@ class BaseService {
7468
return this.submit<T>(requestType, parameter, body, config)
7569
}
7670

77-
patch<T>(id: any, payload?: any, config?: AxiosRequestConfig) {
71+
patch<T = any>(id: any, payload?: any, config?: AxiosRequestConfig) {
7872
const parameter = id && !isObject(id) ? `/${id}` : ''
7973
const body = isObject(id) ? id : payload
8074
return this.submit<T>('patch', parameter, body, config)
8175
}
8276

83-
update<T>(id: string | number, payload: any) {
77+
update<T = any>(id: string | number, payload: any) {
8478
return this.patch<T>(id, payload)
8579
}
8680

87-
delete<T>(id: string | number) {
81+
delete<T = any>(id: string | number) {
8882
return this.submit<T>('delete', `/${id}`)
8983
}
9084

91-
remove<T>(id: string | number) {
85+
remove<T = any>(id: string | number) {
9286
return this.delete<T>(id)
9387
}
9488

95-
submit<T = any>(
96-
method: Method,
97-
parameter?: string | number,
98-
form?: T,
99-
config?: AxiosRequestConfig,
100-
): Promise<T> {
89+
submit<T = any>(method: Method, parameter?: string | number, form?: T, config?: AxiosRequestConfig): Promise<T> {
10190
BaseService.__validateRequestType(method)
10291
this.beforeSubmit()
10392
return new Promise((resolve, reject) => {
10493
const data = hasFiles(form) ? objectToFormData(form) : form
105-
const endpoint = parameter
106-
? `/${this.endpoint}/${parameter}`
107-
: `/${this.endpoint}`
94+
const endpoint = parameter ? `/${this.endpoint}/${parameter}` : `/${this.endpoint}`
10895
const url = this.__getParameterString(removeDoubleSlash(endpoint))
10996
config = Object.assign({}, config, { url, data, method })
11097
this.$http(config)
11198
.then((response: AxiosResponse) => {
11299
this.onSuccess()
113-
resolve(response.data || {})
100+
resolve(response.data)
114101
})
115102
.catch((error: AxiosError<AxiosResponseData>) => {
116103
this.errors.processing = false
@@ -155,8 +142,7 @@ class BaseService {
155142
]
156143
if (!requestTypes.includes(requestType)) {
157144
throw new Error(
158-
`\`${requestType}\` is not a valid request type, ` +
159-
`must be one of: \`${requestTypes.join('`, `')}\`.`,
145+
`\`${requestType}\` is not a valid request type, ` + `must be one of: \`${requestTypes.join('`, `')}\`.`,
160146
)
161147
}
162148
return requestType

0 commit comments

Comments
 (0)