Add editTemplate test and action / update tests

pull/1316/head
Andrew Watkins 2017-04-19 14:46:26 -07:00
parent c7724531ab
commit ffc8d8de1e
3 changed files with 97 additions and 23 deletions

View File

@ -10,11 +10,12 @@ import {
editDashboardCell,
renameDashboardCell,
syncDashboardCell,
editTemplate,
} from 'src/dashboards/actions'
let state
const d1 = {id: 1, cells: [], name: "d1"}
const d2 = {id: 2, cells: [], name: "d2"}
const d1 = {id: 1, cells: [], name: 'd1', templates: []}
const d2 = {id: 2, cells: [], name: 'd2', templates: []}
const dashboards = [d1, d2]
const c1 = {
x: 0,
@ -23,9 +24,20 @@ const c1 = {
h: 4,
id: 1,
isEditing: false,
name: "Gigawatts",
name: 'Gigawatts',
}
const cells = [c1]
const tempVar = {
id: 1,
type: 'measurement',
label: 'test query',
code: '$HOSTS',
query: {
db: 'db1',
text: 'SHOW TAGS WHERE HUNTER = "coo"',
},
values: ['h1', 'h2', 'h3'],
}
describe('DataExplorer.Reducers.UI', () => {
it('can load the dashboards', () => {
@ -106,7 +118,28 @@ describe('DataExplorer.Reducers.UI', () => {
dashboards: [dash],
}
const actual = reducer(state, renameDashboardCell(dash, 0, 0, "Plutonium Consumption Rate (ug/sec)"))
expect(actual.dashboards[0].cells[0].name).to.equal("Plutonium Consumption Rate (ug/sec)")
const actual = reducer(
state,
renameDashboardCell(dash, 0, 0, 'Plutonium Consumption Rate (ug/sec)')
)
expect(actual.dashboards[0].cells[0].name).to.equal(
'Plutonium Consumption Rate (ug/sec)'
)
})
it('can edit a template', () => {
const dash = {...d1, templates: [tempVar]}
state = {
dashboards: [dash],
}
const updates = {
code: '$NEWCODE',
label: 'new label',
type: 'tagKey',
}
const expected = {...tempVar, ...updates}
const actual = reducer(state, editTemplate(dash.id, tempVar.id, updates))
expect(actual.dashboards[0].templates[0]).to.deep.equal(expected)
})
})

View File

@ -111,6 +111,15 @@ export const editCellQueryStatus = (queryID, status) => ({
},
})
export const editTemplate = (dashboardID, templateID, updates) => ({
type: 'EDIT_TEMPLATE',
payload: {
dashboardID,
templateID,
updates,
},
})
// Stub Template Variables Data
const templates = [

View File

@ -31,7 +31,9 @@ export default function ui(state = initialState, action) {
const {dashboard} = action.payload
const newState = {
dashboard,
dashboards: state.dashboards.map((d) => d.id === dashboard.id ? dashboard : d),
dashboards: state.dashboards.map(
d => (d.id === dashboard.id ? dashboard : d)
),
}
return {...state, ...newState}
@ -40,7 +42,7 @@ export default function ui(state = initialState, action) {
case 'DELETE_DASHBOARD': {
const {dashboard} = action.payload
const newState = {
dashboards: state.dashboards.filter((d) => d.id !== dashboard.id),
dashboards: state.dashboards.filter(d => d.id !== dashboard.id),
}
return {...state, ...newState}
@ -49,10 +51,7 @@ export default function ui(state = initialState, action) {
case 'DELETE_DASHBOARD_FAILED': {
const {dashboard} = action.payload
const newState = {
dashboards: [
_.cloneDeep(dashboard),
...state.dashboards,
],
dashboards: [_.cloneDeep(dashboard), ...state.dashboards],
}
return {...state, ...newState}
}
@ -66,7 +65,9 @@ export default function ui(state = initialState, action) {
}
const newState = {
dashboards: state.dashboards.map((d) => d.id === dashboard.id ? newDashboard : d),
dashboards: state.dashboards.map(
d => (d.id === dashboard.id ? newDashboard : d)
),
}
return {...state, ...newState}
@ -78,7 +79,9 @@ export default function ui(state = initialState, action) {
const newCells = [cell, ...dashboard.cells]
const newDashboard = {...dashboard, cells: newCells}
const newDashboards = dashboards.map((d) => d.id === dashboard.id ? newDashboard : d)
const newDashboards = dashboards.map(
d => (d.id === dashboard.id ? newDashboard : d)
)
const newState = {dashboards: newDashboards}
return {...state, ...newState}
@ -87,7 +90,7 @@ export default function ui(state = initialState, action) {
case 'EDIT_DASHBOARD_CELL': {
const {x, y, isEditing, dashboard} = action.payload
const cell = dashboard.cells.find((c) => c.x === x && c.y === y)
const cell = dashboard.cells.find(c => c.x === x && c.y === y)
const newCell = {
...cell,
@ -96,11 +99,13 @@ export default function ui(state = initialState, action) {
const newDashboard = {
...dashboard,
cells: dashboard.cells.map((c) => c.x === x && c.y === y ? newCell : c),
cells: dashboard.cells.map(c => (c.x === x && c.y === y ? newCell : c)),
}
const newState = {
dashboards: state.dashboards.map((d) => d.id === dashboard.id ? newDashboard : d),
dashboards: state.dashboards.map(
d => (d.id === dashboard.id ? newDashboard : d)
),
}
return {...state, ...newState}
@ -110,13 +115,17 @@ export default function ui(state = initialState, action) {
const {cell} = action.payload
const {dashboard} = state
const newCells = dashboard.cells.filter((c) => !(c.x === cell.x && c.y === cell.y))
const newCells = dashboard.cells.filter(
c => !(c.x === cell.x && c.y === cell.y)
)
const newDashboard = {
...dashboard,
cells: newCells,
}
const newState = {
dashboards: state.dashboards.map((d) => d.id === dashboard.id ? newDashboard : d),
dashboards: state.dashboards.map(
d => (d.id === dashboard.id ? newDashboard : d)
),
}
return {...state, ...newState}
@ -127,11 +136,15 @@ export default function ui(state = initialState, action) {
const newDashboard = {
...dashboard,
cells: dashboard.cells.map((c) => c.x === cell.x && c.y === cell.y ? cell : c),
cells: dashboard.cells.map(
c => (c.x === cell.x && c.y === cell.y ? cell : c)
),
}
const newState = {
dashboards: state.dashboards.map((d) => d.id === dashboard.id ? newDashboard : d),
dashboards: state.dashboards.map(
d => (d.id === dashboard.id ? newDashboard : d)
),
}
return {...state, ...newState}
@ -140,7 +153,7 @@ export default function ui(state = initialState, action) {
case 'RENAME_DASHBOARD_CELL': {
const {x, y, name, dashboard} = action.payload
const cell = dashboard.cells.find((c) => c.x === x && c.y === y)
const cell = dashboard.cells.find(c => c.x === x && c.y === y)
const newCell = {
...cell,
@ -149,11 +162,13 @@ export default function ui(state = initialState, action) {
const newDashboard = {
...dashboard,
cells: dashboard.cells.map((c) => c.x === x && c.y === y ? newCell : c),
cells: dashboard.cells.map(c => (c.x === x && c.y === y ? newCell : c)),
}
const newState = {
dashboards: state.dashboards.map((d) => d.id === dashboard.id ? newDashboard : d),
dashboards: state.dashboards.map(
d => (d.id === dashboard.id ? newDashboard : d)
),
}
return {...state, ...newState}
@ -164,6 +179,23 @@ export default function ui(state = initialState, action) {
return {...state, cellQueryStatus: {queryID, status}}
}
case 'EDIT_TEMPLATE': {
const {dashboardID, templateID, updates} = action.payload
const dashboards = state.dashboards.map(
d =>
(d.id === dashboardID
? {
...d,
templates: d.templates.map(
t => (t.id === templateID ? {...t, ...updates} : t)
),
}
: d)
)
return {...state, dashboards}
}
}
return state