Skip to content

Commit f7d9748

Browse files
authored
fix: allow StaticLegend to work with Notebooks pipe context (#4132)
1 parent 73fbc59 commit f7d9748

File tree

4 files changed

+147
-7
lines changed

4 files changed

+147
-7
lines changed

cypress/e2e/shared/legends.test.ts

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('Legends', () => {
2222
cy.createMapVariable(id)
2323
cy.fixture('routes').then(({orgs, explorer}) => {
2424
cy.visit(`${orgs}/${id}${explorer}`)
25-
cy.getByTestID('tree-nav')
25+
cy.getByTestID('tree-nav').should('be.visible')
2626
})
2727
})
2828
})
@@ -416,7 +416,7 @@ describe('Legends', () => {
416416
cy.fixture('routes').then(({orgs}) => {
417417
cy.get('@org').then(({id}: Organization) => {
418418
cy.visit(`${orgs}/${id}/dashboards-list`)
419-
cy.getByTestID('tree-nav')
419+
cy.getByTestID('tree-nav').should('be.visible')
420420
})
421421
})
422422
})
@@ -555,4 +555,109 @@ describe('Legends', () => {
555555
)
556556
})
557557
})
558+
559+
describe('in Notebooks', () => {
560+
beforeEach(() => {
561+
cy.flush()
562+
cy.signin()
563+
cy.get('@org').then(({id}: Organization) => {
564+
cy.createMapVariable(id)
565+
cy.fixture('routes').then(({orgs, notebooks}) => {
566+
cy.visit(`${orgs}/${id}${notebooks}`)
567+
cy.getByTestID('tree-nav').should('be.visible')
568+
})
569+
})
570+
})
571+
572+
it('allows the user to toggle the hover legend and static legend', () => {
573+
cy.intercept('PATCH', '/api/v2private/notebooks/*').as('updateNotebook')
574+
cy.writeData(points(100))
575+
const bucketName = 'defbuck'
576+
577+
cy.getByTestID('preset-new')
578+
.first()
579+
.click()
580+
581+
cy.getByTestID('time-machine-submit-button').should('be.visible')
582+
583+
// Delete 2 panels and leave "Visualize the Result"
584+
cy.getByTestID('sidebar-button')
585+
.first()
586+
.click()
587+
cy.getByTestID('Delete--list-item').click()
588+
cy.wait('@updateNotebook')
589+
590+
cy.getByTestID('sidebar-button')
591+
.first()
592+
.click()
593+
cy.getByTestID('Delete--list-item').click()
594+
cy.wait('@updateNotebook')
595+
596+
cy.get('.flow-panel__visible').should('have.length', 1)
597+
598+
// Add the Query Builder
599+
cy.get('.flow-divider--button')
600+
.first()
601+
.click()
602+
603+
// Opening the menu adds another Query Builder button
604+
cy.getByTestID('add-flow-btn--queryBuilder').should('have.length', 2)
605+
606+
// Click the newest Query Builder button
607+
cy.getByTestID('add-flow-btn--queryBuilder')
608+
.last()
609+
.click()
610+
cy.wait('@updateNotebook')
611+
612+
cy.getByTestID('bucket-selector').within(() => {
613+
cy.getByTestID(`selector-list ${bucketName}`).click()
614+
})
615+
cy.wait('@updateNotebook')
616+
617+
cy.getByTestID('builder-card')
618+
.eq(0)
619+
.within(() => {
620+
cy.getByTestID(`selector-list m`).click()
621+
})
622+
cy.wait('@updateNotebook')
623+
624+
cy.getByTestID('builder-card')
625+
.eq(1)
626+
.within(() => {
627+
cy.getByTestID(`selector-list v`).click()
628+
})
629+
cy.wait('@updateNotebook')
630+
631+
cy.getByTestID('builder-card')
632+
.eq(2)
633+
.within(() => {
634+
cy.getByTestID(`selector-list tv1`).click()
635+
})
636+
cy.wait('@updateNotebook')
637+
638+
cy.getByTestID('time-machine-submit-button').click()
639+
cy.get('button.flows-config-visualization-button').click()
640+
641+
cy.get('.flow-sidebar').should('be.visible')
642+
cy.getByTestID('hover-legend-toggle').scrollIntoView()
643+
644+
// Toggling any legend triggers an api call to update the Notebook spec
645+
// Hover Legend starts as Show and is toggleable
646+
cy.get('label[for="radio_hover_legend_hide"').click()
647+
cy.wait('@updateNotebook')
648+
649+
cy.get('label[for="radio_hover_legend_show"').click()
650+
cy.wait('@updateNotebook')
651+
652+
// Static Legend starts as Hide and is toggleable
653+
cy.get('label[for="radio_static_legend_show"').click()
654+
cy.wait('@updateNotebook')
655+
cy.get('.insert-cell-menu.always-on').scrollIntoView()
656+
cy.getByTestID('giraffe-static-legend').should('be.visible')
657+
658+
cy.get('label[for="radio_static_legend_hide"').click()
659+
cy.wait('@updateNotebook')
660+
cy.getByTestID('giraffe-static-legend').should('not.exist')
661+
})
662+
})
558663
})

cypress/fixtures/routes.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"endpoints": "/endpoints",
77
"explorer": "/data-explorer",
88
"flows": "/flows",
9+
"notebooks": "/notebooks",
910
"orgs": "/orgs",
1011
"rules": "/rules",
1112
"sources": "/load-data/sources",

src/visualization/components/View.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ jest.doMock('src/visualization/contextLoader', () => {
6767
}
6868
})
6969

70+
jest.mock('src/flows/index', () => {
71+
return {
72+
PROJECT_NAME: 'NoteBook',
73+
DEFAULT_PROJECT_NAME: 'Untitled Notebook',
74+
}
75+
})
76+
77+
jest.mock('src/flows/templates/index', () => {
78+
return {
79+
TEMPLATES: {},
80+
}
81+
})
82+
7083
import {View, SUPPORTED_VISUALIZATIONS} from 'src/visualization'
7184

7285
const setup = properties => {

src/visualization/utils/useStaticLegend.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Libraries
2-
import {useMemo, useCallback} from 'react'
2+
import {useMemo, useContext, useCallback} from 'react'
33
import {useDispatch, useSelector} from 'react-redux'
44

5+
// Context
6+
import {PipeContext} from 'src/flows/context/pipe'
7+
58
// Actions
69
import {setStaticLegend} from 'src/timeMachine/actions'
710

@@ -66,9 +69,11 @@ const convertShowToHide = (showState: boolean): boolean => !showState
6669
const eventPrefix = 'visualization.customize.staticlegend'
6770

6871
export const useStaticLegend = (properties): StaticLegendConfig => {
72+
const {id, data, update} = useContext(PipeContext)
73+
6974
const {isViewingVisOptions} = useSelector(getActiveTimeMachine)
7075
const dispatch = useDispatch()
71-
const update = useCallback(
76+
const timeMachineUpdate = useCallback(
7277
(staticLegend: StaticLegendAPI) => {
7378
dispatch(
7479
setStaticLegend({
@@ -79,6 +84,22 @@ export const useStaticLegend = (properties): StaticLegendConfig => {
7984
},
8085
[dispatch, properties.staticLegend]
8186
)
87+
88+
let updateStaticLegendProperties
89+
const notebookViewProperties = data?.properties || {}
90+
const notebookStaticLegend = notebookViewProperties.staticLegend || {}
91+
if (id) {
92+
updateStaticLegendProperties = (updatedProperties: StaticLegendAPI) =>
93+
update({
94+
properties: {
95+
...notebookViewProperties,
96+
staticLegend: {...notebookStaticLegend, ...updatedProperties},
97+
},
98+
})
99+
} else {
100+
updateStaticLegendProperties = timeMachineUpdate
101+
}
102+
82103
return useMemo(() => {
83104
const {
84105
legendColorizeRows = LEGEND_COLORIZE_ROWS_DEFAULT,
@@ -132,7 +153,7 @@ export const useStaticLegend = (properties): StaticLegendConfig => {
132153
validThreshold = LEGEND_ORIENTATION_THRESHOLD_VERTICAL
133154
}
134155

135-
update({
156+
updateStaticLegendProperties({
136157
colorizeRows: legendColorizeRows,
137158
opacity: validOpacity,
138159
orientationThreshold: validThreshold,
@@ -199,11 +220,11 @@ export const useStaticLegend = (properties): StaticLegendConfig => {
199220
updatedHeightRatio,
200221
})
201222

202-
update({
223+
updateStaticLegendProperties({
203224
heightRatio: updatedHeightRatio,
204225
})
205226
}
206227
},
207228
}
208-
}, [properties, isViewingVisOptions, update])
229+
}, [properties, isViewingVisOptions, updateStaticLegendProperties])
209230
}

0 commit comments

Comments
 (0)