Skip to content

Commit

Permalink
test: add confirm & layout test
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjinzhou committed May 24, 2018
1 parent 141bff7 commit 098e7ea
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
64 changes: 64 additions & 0 deletions components/layout/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { mount } from '@vue/test-utils'
import Vue from 'vue'
import Layout from '..'

const { Sider, Content } = Layout

describe('Layout', () => {
it('detect the sider as children', (done) => {
const wrapper = mount(
{
render () {
return (
<Layout>
<Sider>Sider</Sider>
<Content>Content</Content>
</Layout>
)
},
},
)
Vue.nextTick(() => {
expect(wrapper.find('.ant-layout').classes()).toContain('ant-layout-has-sider')
done()
})
})

it('detect the sider inside the children', (done) => {
const wrapper = mount(
{
render () {
return (
<Layout>
<div><Sider>Sider</Sider></div>
<Content>Content</Content>
</Layout>
)
},
}
)
Vue.nextTick(() => {
expect(wrapper.find('.ant-layout').classes()).toContain('ant-layout-has-sider')
done()
})
})

it('detect ant-layout-sider-has-trigger class in sider when ant-layout-sider-trigger div tag exists', (done) => {
const wrapper = mount(
{
render () {
return (
<Layout>
<div><Sider collapsible>Sider</Sider></div>
<Content>Content</Content>
</Layout>
)
},
}
)
Vue.nextTick(() => {
expect(wrapper.find('.ant-layout-sider').classes()).toContain('ant-layout-sider-has-trigger')
done()
})
})
})
74 changes: 74 additions & 0 deletions components/modal/__tests__/confirm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Modal from '..'

const { confirm } = Modal

describe('Modal.confirm triggers callbacks correctly', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})

afterEach(() => {
errorSpy.mockReset()
document.body.innerHTML = ''
})

afterAll(() => {
errorSpy.mockRestore()
})

function $$ (className) {
return document.body.querySelectorAll(className)
}

function open (args) {
confirm({
title: 'Want to delete these items?',
content: 'some descriptions',
...args,
})
}

it('trigger onCancel once when click on cancel button', () => {
const onCancel = jest.fn()
const onOk = jest.fn()
open({
onCancel,
onOk,
})
// first Modal
$$('.ant-btn')[0].click()
expect(onCancel.mock.calls.length).toBe(1)
expect(onOk.mock.calls.length).toBe(0)
})

it('trigger onOk once when click on ok button', () => {
const onCancel = jest.fn()
const onOk = jest.fn()
open({
onCancel,
onOk,
})
// second Modal
$$('.ant-btn-primary')[0].click()
expect(onCancel.mock.calls.length).toBe(0)
expect(onOk.mock.calls.length).toBe(1)
})

it('should allow Modal.comfirm without onCancel been set', () => {
open()
// Third Modal
$$('.ant-btn')[0].click()
expect(errorSpy).not.toHaveBeenCalled()
})

it('should allow Modal.comfirm without onOk been set', () => {
open()
// Fourth Modal
$$('.ant-btn-primary')[0].click()
expect(errorSpy).not.toHaveBeenCalled()
})

it('ok only', () => {
open({ okCancel: false })
expect($$('.ant-btn')).toHaveLength(1)
expect($$('.ant-btn')[0].innerHTML).toContain('OK')
})
})

0 comments on commit 098e7ea

Please sign in to comment.