Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wxsms committed Aug 2, 2020
1 parent 2345609 commit 3c1869d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/components/typeahead/Typeahead.vue
Expand Up @@ -19,7 +19,7 @@
</template>

<script>
import {getRequest} from '../../utils/http.utils'
import {request} from '../../utils/http.utils'
import {isString} from '../../utils/object.utils'
import {
isElement,
Expand Down Expand Up @@ -214,7 +214,7 @@
} else if (this.asyncSrc) {
this.timeoutID = setTimeout(() => {
this.$emit('loading')
getRequest(this.asyncSrc + encodeURIComponent(value))
request(this.asyncSrc + encodeURIComponent(value))
.then(data => {
if (this.inputEl.matches(':focus')) {
this.prepareItems(this.asyncKey ? data[this.asyncKey] : data, true)
Expand Down
9 changes: 5 additions & 4 deletions src/utils/http.utils.js
@@ -1,6 +1,6 @@
import {isFunction, isExist} from './object.utils'
import { isFunction, isExist } from './object.utils'

export function getRequest (url) {
export function request (url, method = 'GET') {
let request = new window.XMLHttpRequest()
let data = {}
let p = {
Expand All @@ -19,10 +19,11 @@ export function getRequest (url) {
p.done(JSON.parse)
request.onreadystatechange = () => {
if (request.readyState === 4) {
let e = {status: request.status}
let e = { status: request.status }
if (request.status === 200) {
let response = request.responseText
for (let i in data.done) {
/* istanbul ignore else */
if (data.done.hasOwnProperty(i) && isFunction(data.done[i])) {
let value = data.done[i](response)
if (isExist(value)) {
Expand All @@ -35,7 +36,7 @@ export function getRequest (url) {
}
}
}
request.open('GET', url)
request.open(method, url)
request.setRequestHeader('Accept', 'application/json')
request.send()
return p
Expand Down
4 changes: 2 additions & 2 deletions test/specs/Typeahead.spec.js
@@ -1,5 +1,5 @@
import { triggerEvent, sleep, triggerKey, createVm, destroyVm, keyCodes } from '../utils'
import { getRequest } from '../../src/utils/http.utils'
import { request } from '../../src/utils/http.utils'
import states from '../assets/data/states.json'

function baseVm () {
Expand Down Expand Up @@ -591,7 +591,7 @@ describe('Typeahead', () => {
}, {
methods: {
queryFunction (query, done) {
getRequest('https://api.github.com/search/users?q=' + query)
request('https://api.github.com/search/users?q=' + query)
.then(data => {
done(data.items)
})
Expand Down
100 changes: 58 additions & 42 deletions test/specs/http.utils.spec.js
@@ -1,51 +1,67 @@
import * as utils from '../../src/utils/http.utils'

describe('http.utils', () => {
let xhr, requests, server
describe('#request ', () => {
let xhr, requests, server
before(function () {
xhr = sinon.useFakeXMLHttpRequest()
requests = []
xhr.onCreate = function (req) {
requests.push(req)
}
server = sinon.fakeServer.create()
})

before(function () {
xhr = sinon.useFakeXMLHttpRequest()
requests = []
xhr.onCreate = function (req) {
requests.push(req)
}
server = sinon.fakeServer.create()
})
after(function () {
xhr.restore()
server.restore()
})

after(function () {
xhr.restore()
server.restore()
})
it('should be able to get with success callback', () => {
const then = sinon.spy()
const always = sinon.spy()
utils.request('/some/path')
.then(then)
.always(always)
server.requests[0].respond(
200,
{ 'Content-Type': 'application/json' },
JSON.stringify([{ id: 1, text: 'Provide examples', done: true }])
)
sinon.assert.called(then)
sinon.assert.called(always)
})

it('should be able to get with success callback', () => {
const then = sinon.spy()
const always = sinon.spy()
utils.getRequest('/some/path')
.then(then)
.always(always)
server.requests[0].respond(
200,
{'Content-Type': 'application/json'},
JSON.stringify([{id: 1, text: 'Provide examples', done: true}])
)
sinon.assert.called(then)
sinon.assert.called(always)
})
it('should be able to post with success callback', () => {
const then = sinon.spy()
const always = sinon.spy()
utils.request('/some/path', 'POST')
.then(then)
.always(always)
server.requests[1].respond(
200,
{ 'Content-Type': 'application/json' },
JSON.stringify([{ id: 1, text: 'Provide examples', done: true }])
)
sinon.assert.called(then)
sinon.assert.called(always)
})

it('should be able to get with fail callback', () => {
const then = sinon.spy()
const err = sinon.spy()
const always = sinon.spy()
utils.getRequest('/some/path')
.then(then)
.catch(err)
.always(always)
server.requests[1].respond(
500,
{'Content-Type': 'application/json'},
JSON.stringify([{id: 1, text: 'Provide examples', done: true}])
)
sinon.assert.called(err)
sinon.assert.called(always)
it('should be able to get with fail callback', () => {
const then = sinon.spy()
const err = sinon.spy()
const always = sinon.spy()
utils.request('/some/path')
.then(then)
.catch(err)
.always(always)
server.requests[2].respond(
500,
{ 'Content-Type': 'application/json' },
JSON.stringify([{ id: 1, text: 'Provide examples', done: true }])
)
sinon.assert.called(err)
sinon.assert.called(always)
})
})
})

0 comments on commit 3c1869d

Please sign in to comment.