Skip to content

Commit 9dc8b3e

Browse files
committed
fix tests, simplify requests
1 parent fc33a80 commit 9dc8b3e

File tree

2 files changed

+15
-28
lines changed

2 files changed

+15
-28
lines changed

src/main.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,17 @@ module.exports = function (public_key, config) {
206206
fingerprint: ['{{ default }}', public_key],
207207
})
208208
},
209-
request (url, callback, method, data) {
209+
request (url, callback, expected_status, method, data) {
210210
const xhr = new window.XMLHttpRequest()
211211
xhr.open(method || 'GET', url)
212212
xhr.setRequestHeader('Accept', 'application/json')
213213
xhr.onload = () => {
214214
try {
215-
callback(xhr)
215+
if (xhr.status !== (expected_status || 200)) {
216+
throw Error(`bad response status ${xhr.status} not 200`)
217+
} else {
218+
callback(xhr)
219+
}
216220
} catch (e) {
217221
this.handle_error(`\
218222
${e.toString()}
@@ -234,12 +238,7 @@ ${xhr.responseText}`)
234238
},
235239
request_list (url, data_property, callback) {
236240
this.request(url, (xhr) => {
237-
let items
238-
if (xhr.status !== 200) {
239-
throw Error(`bad response status ${xhr.status} not 200`)
240-
} else {
241-
items = JSON.parse(xhr.responseText)
242-
}
241+
const items = JSON.parse(xhr.responseText)
243242
data_property.splice(0)
244243
items.forEach(con => data_property.push(con))
245244
callback && callback(this)
@@ -278,12 +277,8 @@ ${xhr.responseText}`)
278277
return false
279278
}
280279
this.request(url, (xhr) => {
281-
if (xhr.status !== 200) {
282-
throw Error(`bad response status ${xhr.status} not 200`)
283-
} else {
284-
const con = JSON.parse(xhr.responseText)
285-
Vue.set(this.contractors_extra, link, con)
286-
}
280+
const con = JSON.parse(xhr.responseText)
281+
Vue.set(this.contractors_extra, link, con)
287282
})
288283
return true
289284
},
@@ -292,24 +287,16 @@ ${xhr.responseText}`)
292287
return
293288
}
294289
this.request(`${config.api_root}/${public_key}/enquiry`, (xhr) => {
295-
if (xhr.status !== 200) {
296-
throw Error(`bad response status ${xhr.status} not 200`)
297-
} else {
298-
this.enquiry_form_info = Object.assign({}, this.enquiry_form_info, JSON.parse(xhr.responseText))
299-
}
290+
this.enquiry_form_info = Object.assign({}, this.enquiry_form_info, JSON.parse(xhr.responseText))
300291
})
301292
},
302293
submit_enquiry (callback) {
303294
const data = JSON.stringify(clean(this.enquiry_data))
304-
const request_callback = (xhr) => {
305-
if (xhr.status !== 201) {
306-
throw Error(`bad response status ${xhr.status} not 201`)
307-
} else {
308-
this.enquiry_data = {}
309-
callback()
310-
}
295+
const request_callback = () => {
296+
this.enquiry_data = {}
297+
callback()
311298
}
312-
this.request(`${config.api_root}/${public_key}/enquiry`, request_callback, 'POST', data)
299+
this.request(`${config.api_root}/${public_key}/enquiry`, request_callback, 201, 'POST', data)
313300
},
314301
get_subject_list () {
315302
if (this.subjects.length > 0) {

test/unit/specs/grid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from 'vue'
22
import VueRouter from 'vue-router'
33
import grid from 'src/components/grid'
4-
import {tick, sleep} from './_shared'
4+
import {tick} from './_shared'
55

66
describe('grid.vue', () => {
77
it('should render contractors', () => {

0 commit comments

Comments
 (0)