From 6fec8ecd917921a3b962d7bd11e00867c4174ad2 Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Tue, 10 Apr 2018 13:56:53 -0700 Subject: [PATCH] Add test for bug --- ui/mocks/kapacitor/apis/index.ts | 8 +- .../containers/TickscriptPage.test.tsx | 85 +++++++++++++++++-- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/ui/mocks/kapacitor/apis/index.ts b/ui/mocks/kapacitor/apis/index.ts index 74aa998c72..5d94a48ba8 100644 --- a/ui/mocks/kapacitor/apis/index.ts +++ b/ui/mocks/kapacitor/apis/index.ts @@ -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: () => {}, + }, + }) +) diff --git a/ui/test/kapacitor/containers/TickscriptPage.test.tsx b/ui/test/kapacitor/containers/TickscriptPage.test.tsx index 3f8a093d0b..866d43f2a4 100644 --- a/ui/test/kapacitor/containers/TickscriptPage.test.tsx +++ b/ui/test/kapacitor/containers/TickscriptPage.test.tsx @@ -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() 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() + }) + }) + }) + }) })