test(loading-bar): add test cases (#1157)

This commit is contained in:
Yugang Cao 2021-09-11 20:38:45 +08:00 committed by GitHub
parent 488a891ea3
commit 8a92c5c40e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 111 additions and 8 deletions

View File

@ -1,8 +0,0 @@
import { mount } from '@vue/test-utils'
import { NLoadingBarProvider } from '../index'
describe('n-loading-bar', () => {
it('should work with import on demand', () => {
mount(NLoadingBarProvider)
})
})

View File

@ -0,0 +1,111 @@
import { mount } from '@vue/test-utils'
import { defineComponent, h, nextTick } from 'vue'
import { NLoadingBarProvider, useLoadingBar } from '../index'
const Provider = defineComponent({
render () {
return <NLoadingBarProvider>{this.$slots}</NLoadingBarProvider>
}
})
describe('n-loading-bar', () => {
it('should work with import on demand', () => {
mount(NLoadingBarProvider)
})
it('should have start type', () => {
const Test = defineComponent({
setup () {
const loadingBar = useLoadingBar()
loadingBar.start()
},
render () {
return null
}
})
const wrapper = mount(() => (
<Provider>{{ default: () => <Test /> }}</Provider>
))
void nextTick(() => {
expect(document.querySelector('.n-loading-bar')).not.toEqual(null)
wrapper.unmount()
})
})
it('should have finish type', async () => {
const Test = defineComponent({
setup () {
const loadingBar = useLoadingBar()
loadingBar.start()
loadingBar.finish()
},
render () {
return null
}
})
const wrapper = mount(() => (
<Provider>{{ default: () => <Test /> }}</Provider>
))
await nextTick()
expect(document.querySelector('.n-loading-bar--finishing')).not.toEqual(
null
)
wrapper.unmount()
})
it('should have error type', async () => {
const Test = defineComponent({
setup () {
const loadingBar = useLoadingBar()
loadingBar.error()
},
render () {
return null
}
})
const wrapper = mount(() => (
<Provider>{{ default: () => <Test /> }}</Provider>
))
await nextTick(() => {
setTimeout(() => {
expect(document.querySelector('.n-loading-bar--error')).not.toEqual(
null
)
wrapper.unmount()
}, 300)
})
})
it('should have loadingBarStyle prop', (done) => {
const Test = defineComponent({
setup () {
const loadingBar = useLoadingBar()
loadingBar.error()
},
render () {
return null
}
})
const wrapper = mount(NLoadingBarProvider, {
props: {
loadingBarStyle: {
error: {
height: '5px',
color: '#ccc'
}
}
},
slots: {
default: () => <Test />
}
})
void nextTick(() => {
setTimeout(() => {
expect(
document.querySelector('.n-loading-bar--error')?.getAttribute('style')
).toContain('height: 5px;')
wrapper.unmount()
done()
}, 300)
})
})
})