Skip to content

Commit f4ca956

Browse files
Chantouch SekChantouch Sek
authored andcommitted
test: ✅ fixed test
1 parent f9b5c00 commit f4ca956

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

src/__tests__/base-proxy.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('BaseProxy', () => {
1919
BaseProxy.$http = axios
2020
proxy = new PostProxy()
2121
mockAdapter = new MockAdapter(axios)
22+
mockAdapter.reset()
2223
})
2324

2425
it('check if http was installed', async () => {
@@ -72,6 +73,15 @@ describe('BaseProxy', () => {
7273
expect(data).toEqual(items)
7374
})
7475

76+
it('Check network server return 500', async () => {
77+
mockAdapter.onGet('/posts').networkError()
78+
try {
79+
await proxy.all()
80+
} catch (e) {
81+
expect(e.message).toBe('Network Error')
82+
}
83+
})
84+
7585
it('will fetch all records with query params', async () => {
7686
const items = [
7787
{ first_name: 'Dara', last_name: 'Hok', id: 1 },
@@ -165,13 +175,14 @@ describe('BaseProxy', () => {
165175

166176
it('transforms the data to a FormData object if there is a File', async () => {
167177
const file = new File(['hello world!'], 'myfile')
168-
const form = { field1: {}, field2: {} }
178+
const form = { field1: {}, field2: {}, files: [] }
169179
form.field1 = {
170180
foo: 'testFoo',
171181
bar: ['testBar1', 'testBar2'],
172182
baz: new Date(Date.UTC(2012, 3, 13, 2, 12)),
173183
}
174184
form.field2 = file
185+
form.files = [{ file }]
175186

176187
mockAdapter.onPost('/posts').reply((request) => {
177188
expect(request.data).toBeInstanceOf(FormData)
@@ -180,13 +191,15 @@ describe('BaseProxy', () => {
180191
expect(request.data.get('field1[bar][1]')).toBe('testBar2')
181192
expect(request.data.get('field1[baz]')).toBe('2012-04-13T02:12:00.000Z')
182193
expect(request.data.get('field2')).toEqual(file)
194+
expect(request.data.get('files[0][file]')).toEqual(file)
183195

184196
expect(getFormDataKeys(request.data)).toEqual([
185197
'field1[foo]',
186198
'field1[bar][0]',
187199
'field1[bar][1]',
188200
'field1[baz]',
189201
'field2',
202+
'files[0][file]',
190203
])
191204
return [200, {}]
192205
})

src/__tests__/validator.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ describe('Validator', () => {
4444
expect(validator.missed('name')).toBeTruthy()
4545
expect(validator.nullState('name')).toBeNull()
4646
})
47+
test('Check nullState function', () => {
48+
validator.add('email', 'The email field is required.')
49+
expect(validator.nullState('email')).toBeFalsy()
50+
})
51+
test('Get an none exist error messages by param name', () => {
52+
expect(validator.get('email')).toHaveLength(0)
53+
})
4754
test('Get all errors by key', () => {
4855
validator.add('email', 'The email field is required.')
4956
expect(validator.get('email').length).toBeGreaterThan(0)
@@ -68,6 +75,13 @@ describe('Validator', () => {
6875
expect(validator.first('email')).toBe('The email field is required.')
6976
expect(Object.keys(validator.all()).length).toEqual(2)
7077
})
78+
it('should not allow protopath overwrite', () => {
79+
const errors = {
80+
__proto__: ['Set __proto__ to overwrite.'],
81+
}
82+
validator.fill(errors)
83+
expect(Object.keys(validator.all()).length).toEqual(0)
84+
})
7185
test('Clear all errors by flush', () => {
7286
const errors = {
7387
name: ['The name field is required.'],

src/core/BaseProxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class BaseProxy {
9494
}
9595
reject(error)
9696
} else {
97-
reject()
97+
reject(error)
9898
}
9999
})
100100
})

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"types": [
1212
"@types/node",
1313
"@nuxt/types",
14-
"@types/jest"
14+
"@types/jest",
15+
"axios"
1516
]
1617
},
1718
"include": ["src"],

0 commit comments

Comments
 (0)