Skip to content

Commit

Permalink
⬆️ up test-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Aug 27, 2018
1 parent 37734d4 commit b4865d7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
22 changes: 10 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default {
} else if (this.isDelayElapsed) {
assert(
(this.$slots.default && this.$slots.default.length === 1) ||
(this.$slots.pending && this.$slots.pending.length === 1),
(this.$slots.pending && this.$slots.pending.length === 1),
'Provide exactly one default/pending slot with no `slot-scope` for the pending promise'
)
return this.$slots.default ? this.$slots.default[0] : this.$slots.pending[0]
Expand Down Expand Up @@ -81,16 +81,14 @@ export default {
// do not listen for already set up promises
promises.filter(p => !this.ongoingPromises.has(p)).forEach(p => {
this.ongoingPromises.set(p, true)
p
.then(data => {
if (this.promises === promises) {
this.resolved = true
this.data.push(data)
}
})
.catch(err => {
if (this.promises === promises) this.error.push(err)
})
p.then(data => {
if (this.promises === promises) {
this.resolved = true
this.data.push(data)
}
}).catch(err => {
if (this.promises === promises) this.error.push(err)
})
})
},
immediate: true,
Expand All @@ -102,7 +100,7 @@ export default {
if (this.pendingDelay > 0) {
this.isDelayElapsed = false
if (this.timerId) clearTimeout(this.timerId)
this.timerId = setTimeout(() => this.isDelayElapsed = true, this.pendingDelay)
this.timerId = setTimeout(() => (this.isDelayElapsed = true), this.pendingDelay)
} else {
this.isDelayElapsed = true
}
Expand Down
38 changes: 26 additions & 12 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ describe('Promised', () => {
expect(wrapper.text()).toBe('loading')
other[1]('done')
await tick()
// XXX looks like a bug in vue test utils, need to rerender to actually display things
wrapper.setProps({ pendingDelay: 100 })
expect(wrapper.text()).toBe('done')
})

Expand All @@ -121,6 +123,8 @@ describe('Promised', () => {
expect(wrapper.text()).toBe('loading')
other[2](new Error('nope'))
await tick()
// XXX looks like a bug in vue test utils, need to rerender to actually display things
wrapper.setProps({ pendingDelay: 100 })
expect(wrapper.text()).toBe('nope')
})
})
Expand Down Expand Up @@ -151,7 +155,9 @@ describe('Promised', () => {
reject(new Error('nope'))
await tick()
expect(errorSpy).toHaveBeenCalledTimes(2)
expect(errorSpy.mock.calls[0][0].toString()).toMatch(/Provide exactly one scoped slot named "catch"/)
expect(errorSpy.mock.calls[0][0].toString()).toMatch(
/Provide exactly one scoped slot named "catch"/
)
})

test('throws if no default scoped slot provided on resolve', async () => {
Expand All @@ -164,20 +170,25 @@ describe('Promised', () => {
expect(errorSpy).not.toHaveBeenCalled()
resolve()
await tick()
expect(errorSpy).toHaveBeenCalledTimes(2)
expect(errorSpy.mock.calls[0][0].toString()).toMatch(/Provide exactly one default\/then scoped slot/)
expect(errorSpy.mock.calls[0][0].toString()).toMatch(
/Provide exactly one default\/then scoped slot/
)
})

test('throws if no default slot provided while pending', async () => {
expect(errorSpy).not.toHaveBeenCalled()
wrapper = mount(NoPending, {
propsData: {
promise,
pendingDelay: 0,
},
})
expect(errorSpy).toHaveBeenCalledTimes(2)
expect(errorSpy.mock.calls[0][0].toString()).toMatch(/Provide exactly one default\/pending slot/)
expect(() => {
wrapper = mount(NoPending, {
propsData: {
promise,
pendingDelay: 0,
},
})
}).toThrowError(/Provide exactly one default\/pending slot/)
// expect(errorSpy).toHaveBeenCalledTimes(2)
// expect(errorSpy.mock.calls[0][0].toString()).toMatch(
// /Provide exactly one default\/pending slot/
// )
})
})

Expand Down Expand Up @@ -215,6 +226,7 @@ describe('Promised', () => {
let promise
beforeEach(() => {
clearTimeout.mockClear()
setTimeout.mockClear()
;[promise] = fakePromise()
wrapper = mount(Helper, {
propsData: {
Expand All @@ -232,12 +244,14 @@ describe('Promised', () => {
})

test('custom pendingDelay', async () => {
expect(setTimeout).toHaveBeenCalledTimes(1)
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 300)
;[promise] = fakePromise()
wrapper.setProps({
promise,
pendingDelay: 100,
promise,
})
expect(setTimeout).toHaveBeenCalledTimes(2)
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 100)
})

Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
resolved "https://registry.yarnpkg.com/@types/strip-json-comments/-/strip-json-comments-0.0.30.tgz#9aa30c04db212a9a0649d6ae6fd50accc40748a1"

"@vue/test-utils@^1.0.0-beta.11":
version "1.0.0-beta.11"
resolved "https://registry.yarnpkg.com/@vue/test-utils/-/test-utils-1.0.0-beta.11.tgz#adc18a69d3785da039e60fb77cbc8e968f34ecdd"
version "1.0.0-beta.24"
resolved "https://registry.yarnpkg.com/@vue/test-utils/-/test-utils-1.0.0-beta.24.tgz#da7c3165f49f57f23fdb98caccba0f511effb76f"
dependencies:
lodash "^4.17.4"

Expand Down

0 comments on commit b4865d7

Please sign in to comment.