From d249fd6d9bd61b4c16bfe9e267a922c1ed88e168 Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Thu, 17 May 2018 13:27:45 -0700 Subject: [PATCH] WIP Introduce LOAD_SERVICES --- ui/src/shared/actions/sources.ts | 35 ++++++- .../reducers/{sources.js => sources.ts} | 27 +++++- ui/src/sources/components/InfluxTableHead.tsx | 1 + ui/src/sources/components/InfluxTableRow.tsx | 1 + ui/src/types/services.ts | 14 +++ ui/src/types/sources.ts | 1 + ui/test/resources.ts | 14 +++ ui/test/shared/reducers/sources.test.js | 58 ------------ ui/test/shared/reducers/sources.test.ts | 92 +++++++++++++++++++ 9 files changed, 179 insertions(+), 64 deletions(-) rename ui/src/shared/reducers/{sources.js => sources.ts} (73%) create mode 100644 ui/src/types/services.ts delete mode 100644 ui/test/shared/reducers/sources.test.js create mode 100644 ui/test/shared/reducers/sources.test.ts diff --git a/ui/src/shared/actions/sources.ts b/ui/src/shared/actions/sources.ts index 841c0a518..b840d7f6d 100644 --- a/ui/src/shared/actions/sources.ts +++ b/ui/src/shared/actions/sources.ts @@ -17,8 +17,41 @@ import { import {Source, Kapacitor} from 'src/types' -export type Action = ActionLoadSources | ActionUpdateSource +export type Action = + | ActionLoadSources + | ActionUpdateSource + | ActionAddSource + | ActionFetchKapacitors + | ActionSetActiveKapacitor + | ActionDeleteKapacitor + | ActionLoadServices +// Load Services +export type LoadServices = ( + source: Source, + services: Service[] +) => ActionLoadServices + +export interface ActionLoadServices { + type: 'LOAD_SERVICES' + payload: { + source: Source + services: Service[] + } +} + +export const loadServices = ( + source: Source, + services: Service[] +): ActionLoadServices => ({ + type: 'LOAD_SERVICES', + payload: { + source, + services, + }, +}) + +// Load Sources export type LoadSources = (sources: Source[]) => ActionLoadSources export interface ActionLoadSources { type: 'LOAD_SOURCES' diff --git a/ui/src/shared/reducers/sources.js b/ui/src/shared/reducers/sources.ts similarity index 73% rename from ui/src/shared/reducers/sources.js rename to ui/src/shared/reducers/sources.ts index cbed0e8c1..b616f5bda 100644 --- a/ui/src/shared/reducers/sources.js +++ b/ui/src/shared/reducers/sources.ts @@ -1,15 +1,28 @@ import _ from 'lodash' +import {Source, Kapacitor} from 'src/types' +import {Action} from 'src/shared/actions/sources' -const getInitialState = () => [] +export const initialState: Source[] = [] -const initialState = getInitialState() - -const sourcesReducer = (state = initialState, action) => { +const sourcesReducer = (state = initialState, action: Action): Source[] => { switch (action.type) { case 'LOAD_SOURCES': { return action.payload.sources } + case 'LOAD_SERVICES': { + const {services} = action.payload + const newState = state.map(source => { + if (source.id === action.payload.source.id) { + return {...source, services} + } + + return {...source} + }) + + return newState + } + case 'SOURCE_UPDATED': { const {source} = action.payload const updatedIndex = state.findIndex(s => s.id === source.id) @@ -59,7 +72,11 @@ const sourcesReducer = (state = initialState, action) => { const {kapacitor} = action.payload const updatedSources = _.cloneDeep(state) updatedSources.forEach(source => { - const index = _.findIndex(source.kapacitors, k => k.id === kapacitor.id) + const index = _.findIndex( + source.kapacitors, + k => k.id === kapacitor.id + ) + if (index >= 0) { source.kapacitors.splice(index, 1) } diff --git a/ui/src/sources/components/InfluxTableHead.tsx b/ui/src/sources/components/InfluxTableHead.tsx index 7f80b3b03..2d0624f07 100644 --- a/ui/src/sources/components/InfluxTableHead.tsx +++ b/ui/src/sources/components/InfluxTableHead.tsx @@ -13,6 +13,7 @@ const InfluxTableHead: SFC<{}> = (): ReactElement< InfluxDB Connection + Services Connection Kapacitor Connection { /> + Services Connection { - it('can correctly show default sources when adding a source', () => { - let state = [] - - state = reducer( - state, - addSource({ - id: '1', - default: true, - }) - ) - - state = reducer( - state, - addSource({ - id: '2', - default: true, - }) - ) - - expect(state.filter(s => s.default).length).toBe(1) - }) - - it('can correctly show default sources when updating a source', () => { - let state = [] - - state = reducer( - state, - addSource({ - id: '1', - default: true, - }) - ) - - state = reducer( - state, - addSource({ - id: '2', - default: true, - }) - ) - - state = reducer( - state, - updateSource({ - id: '1', - default: true, - }) - ) - - expect(state.find(({id}) => id === '1').default).toBe(true) - expect(state.find(({id}) => id === '2').default).toBe(false) - }) -}) diff --git a/ui/test/shared/reducers/sources.test.ts b/ui/test/shared/reducers/sources.test.ts new file mode 100644 index 000000000..e6325ee36 --- /dev/null +++ b/ui/test/shared/reducers/sources.test.ts @@ -0,0 +1,92 @@ +import reducer, {initialState} from 'src/shared/reducers/sources' + +import { + updateSource, + addSource, + loadServices, + loadSources, +} from 'src/shared/actions/sources' + +import {source, service} from 'test/resources' + +describe('Shared.Reducers.sources', () => { + it('can LOAD_SOURCES', () => { + const expected = [{...source, id: '1'}] + const actual = reducer(initialState, loadSources(expected)) + + expect(actual).toEqual(expected) + }) + + describe('ADD_SOURCES', () => { + it('can ADD_SOURCES', () => { + let state = [] + + state = reducer( + state, + addSource({ + ...source, + id: '1', + default: true, + }) + ) + + state = reducer( + state, + addSource({ + ...source, + id: '2', + default: true, + }) + ) + + expect(state.filter(s => s.default).length).toBe(1) + }) + + it('can correctly show default sources when updating a source', () => { + let state = [] + + state = reducer( + initialState, + addSource({ + ...source, + id: '1', + default: true, + }) + ) + + state = reducer( + state, + addSource({ + ...source, + id: '2', + default: true, + }) + ) + + state = reducer( + state, + updateSource({ + ...source, + id: '1', + default: true, + }) + ) + + expect(state.find(({id}) => id === '1').default).toBe(true) + expect(state.find(({id}) => id === '2').default).toBe(false) + }) + }) + + describe('LOAD_SERVICES', () => { + it('correctly loads the services', () => { + const s1 = {...service, id: '1'} + const s2 = {...service, id: '2'} + + const expected = [s1, s2] + const state = reducer(initialState, loadSources([source])) + const actual = reducer(state, loadServices(source, expected)) + + expect(actual[0].services).toEqual(expected) + }) + }) +})