Add test for bug

pull/10616/head
Andrew Watkins 2018-04-10 13:56:53 -07:00
parent b7e39e77f7
commit 6fec8ecd91
2 changed files with 86 additions and 7 deletions

View File

@ -1,2 +1,8 @@
export const pingKapacitorVersion = jest.fn(() => Promise.resolve('2.0'))
export const getLogStreamByRuleID = jest.fn(() => Promise.resolve())
export const getLogStreamByRuleID = jest.fn(() =>
Promise.resolve({
body: {
getReader: () => {},
},
})
)

View File

@ -1,22 +1,26 @@
import React from 'react'
import {shallow} from 'enzyme'
import {TickscriptPage} from 'src/kapacitor/containers/TickscriptPage'
import TickscriptHeader from 'src/kapacitor/components/TickscriptHeader'
import TickscriptSave from 'src/kapacitor/components/TickscriptSave'
import {source, kapacitorRules} from 'test/resources'
jest.mock('src/shared/apis', () => require('mocks/shared/apis'))
jest.mock('src/kapacitor/apis', () => require('mocks/kapacitor/apis'))
const setup = () => {
const kapacitorActions = {
updateTask: () => {},
createTask: () => {},
getRule: () => {},
}
const setup = (override?) => {
const props = {
source,
errorActions: {
errorThrown: () => {},
},
kapacitorActions: {
updateTask: () => {},
createTask: () => {},
getRule: () => {},
},
kapacitorActions,
router: {
push: () => {},
},
@ -25,7 +29,9 @@ const setup = () => {
},
rules: kapacitorRules,
notify: () => {},
...override,
}
const wrapper = shallow(<TickscriptPage {...props} />)
return {
@ -34,10 +40,77 @@ const setup = () => {
}
describe('Kapacitor.Containers.TickscriptPage', () => {
afterEach(() => {
jest.clearAllMocks()
})
describe('rendering', () => {
it('renders without errors', () => {
const {wrapper} = setup()
expect(wrapper.exists()).toBe(true)
})
})
describe('user interractions', () => {
describe('saving a new Tickscript', () => {
it('routes the user Tickscript edit page if save succeeds', done => {
const id = 'newly-create-tickscript'
const push = jest.fn()
const createTask = jest.fn(() =>
Promise.resolve({code: 200, message: 'nice tickscript', id})
)
const actions = {
...kapacitorActions,
createTask,
}
const {wrapper} = setup({
kapacitorActions: actions,
router: {push},
params: {ruleID: 'new'},
})
const header = wrapper.dive().find(TickscriptHeader)
const save = header.dive().find(TickscriptSave)
save.dive().simulate('click')
process.nextTick(() => {
expect(push).toHaveBeenCalledTimes(1)
expect(push.mock.calls[0][0]).toMatch(id)
done()
})
})
it('routes the user to Tickscript /new page if save fails', done => {
const push = jest.fn()
const createTask = jest.fn(() =>
Promise.resolve({code: 422, message: 'invalid tickscript'})
)
const actions = {
...kapacitorActions,
createTask,
}
const {wrapper} = setup({
kapacitorActions: actions,
router: {push},
params: {ruleID: 'new'},
})
const header = wrapper.dive().find(TickscriptHeader)
const save = header.dive().find(TickscriptSave)
save.dive().simulate('click')
process.nextTick(() => {
expect(push).toHaveBeenCalledTimes(1)
expect(push.mock.calls[0][0]).toMatch('new')
done()
})
})
})
})
})