-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1e8ffe
commit 2cda1bb
Showing
12 changed files
with
240 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { mount } from "enzyme"; | ||
import _ from "lodash"; | ||
import React from "react"; | ||
import { GlobalHotKeys } from "react-hotkeys"; | ||
import { Provider } from "react-redux"; | ||
|
||
import { expect, it } from "@jest/globals"; | ||
|
||
import { DtaleHotkeys, ReactDtaleHotkeys } from "../../dtale/DtaleHotkeys"; | ||
import menuUtils from "../../menuUtils"; | ||
import reduxUtils from "../redux-test-utils"; | ||
import { buildInnerHTML } from "../test-utils"; | ||
|
||
describe("DtaleHotkeys tests", () => { | ||
const { open, innerWidth, innerHeight } = window; | ||
let result, propagateState, openChart, buildClickHandlerSpy; | ||
|
||
beforeAll(() => { | ||
propagateState = jest.fn(); | ||
openChart = jest.fn(); | ||
delete window.open; | ||
window.open = jest.fn(); | ||
window.innerHeight = 800; | ||
window.innerWidth = 1400; | ||
}); | ||
|
||
beforeEach(() => { | ||
buildClickHandlerSpy = jest.spyOn(menuUtils, "buildClickHandler"); | ||
buildInnerHTML({ settings: "" }); | ||
result = mount(<ReactDtaleHotkeys {...{ propagateState, dataId: "1", openChart }} />, { | ||
attachTo: document.getElementById("content"), | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
buildClickHandlerSpy.mockClear(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
window.open = open; | ||
window.innerHeight = innerHeight; | ||
window.innerWidth = innerWidth; | ||
}); | ||
|
||
it("renders GlobalHotKeys", () => { | ||
expect(result.find(GlobalHotKeys).length).toBe(1); | ||
}); | ||
|
||
it("does not render when cell being edited", () => { | ||
result.setProps({ editedCell: "true" }); | ||
expect(result.find(GlobalHotKeys).length).toBe(0); | ||
}); | ||
|
||
it("sets state and fires click handler on menu open", () => { | ||
const hotkeys = result.find(GlobalHotKeys); | ||
const menuHandler = hotkeys.prop("handlers").MENU; | ||
menuHandler(); | ||
expect(propagateState.mock.calls).toHaveLength(1); | ||
expect(propagateState.mock.calls[0][0]).toEqual({ menuOpen: true }); | ||
expect(buildClickHandlerSpy.mock.calls).toHaveLength(1); | ||
buildClickHandlerSpy.mock.calls[0][1](); | ||
expect(propagateState.mock.calls).toHaveLength(2); | ||
expect(propagateState.mock.calls[1][0]).toEqual({ menuOpen: false }); | ||
}); | ||
|
||
it("opens new tab on describe open", () => { | ||
const hotkeys = result.find(GlobalHotKeys); | ||
const describeHandler = hotkeys.prop("handlers").DESCRIBE; | ||
describeHandler(); | ||
expect(window.open.mock.calls).toHaveLength(1); | ||
expect(window.open.mock.calls[0][0]).toBe("/dtale/popup/describe/1"); | ||
}); | ||
|
||
it("calls window.open on code export", () => { | ||
const hotkeys = result.find(GlobalHotKeys); | ||
const codeHandler = hotkeys.prop("handlers").CODE; | ||
codeHandler(); | ||
expect(window.open.mock.calls).toHaveLength(1); | ||
expect(window.open.mock.calls[0][0]).toBe("/dtale/popup/code-export/1"); | ||
}); | ||
|
||
it("calls window.open on code export", () => { | ||
const hotkeys = result.find(GlobalHotKeys); | ||
const chartsHandler = hotkeys.prop("handlers").CHARTS; | ||
chartsHandler(); | ||
expect(window.open.mock.calls).toHaveLength(1); | ||
expect(window.open.mock.calls[0][0]).toBe("/charts/1"); | ||
}); | ||
|
||
it("calls openChart from redux", () => { | ||
const store = reduxUtils.createDtaleStore(); | ||
buildInnerHTML({ settings: "" }, store); | ||
const reduxResult = mount( | ||
<Provider store={store}> | ||
<DtaleHotkeys propagateState={propagateState} /> | ||
</Provider>, | ||
{ attachTo: document.getElementById("content") } | ||
); | ||
const hotkeys = reduxResult.find(GlobalHotKeys); | ||
const filterHandler = hotkeys.prop("handlers").FILTER; | ||
filterHandler(); | ||
expect(_.pick(store.getState().chartData, ["type", "visible"])).toEqual({ | ||
type: "filter", | ||
visible: true, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import _ from "lodash"; | ||
import PropTypes from "prop-types"; | ||
import React from "react"; | ||
import { GlobalHotKeys } from "react-hotkeys"; | ||
import { connect } from "react-redux"; | ||
|
||
import { openChart } from "../actions/charts"; | ||
import menuFuncs from "./menu/dataViewerMenuUtils"; | ||
|
||
class ReactDtaleHotkeys extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
} | ||
|
||
render() { | ||
if (this.props.editedCell) { | ||
return null; | ||
} | ||
const keyMap = { | ||
MENU: "shift+m", | ||
DESCRIBE: "shift+d", | ||
FILTER: "shift+f", | ||
BUILD: "shift+b", | ||
CHARTS: "shift+c", | ||
CODE: "shift+x", | ||
}; | ||
const handlers = _.pick(menuFuncs.buildHotkeyHandlers(this.props), _.keys(keyMap)); | ||
return <GlobalHotKeys keyMap={keyMap} handlers={handlers} />; | ||
} | ||
} | ||
ReactDtaleHotkeys.displayName = "DtaleHotkeys"; | ||
ReactDtaleHotkeys.propTypes = { | ||
dataId: PropTypes.string.isRequired, // eslint-disable-line react/no-unused-prop-types | ||
editedCell: PropTypes.string, | ||
propagateState: PropTypes.func, // eslint-disable-line react/no-unused-prop-types | ||
openChart: PropTypes.func, // eslint-disable-line react/no-unused-prop-types | ||
}; | ||
const ReduxDtaleHotkeys = connect( | ||
state => _.pick(state, ["dataId", "editedCell"]), | ||
dispatch => ({ | ||
openChart: chartProps => dispatch(openChart(chartProps)), | ||
}) | ||
)(ReactDtaleHotkeys); | ||
|
||
export { ReactDtaleHotkeys, ReduxDtaleHotkeys as DtaleHotkeys }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
import $ from "jquery"; | ||
|
||
function buildClickHandler(namespace, close, container = null, clickFilters = () => false) { | ||
$(document).bind(`click.${namespace}`, function (e) { | ||
if (clickFilters(e)) { | ||
return; | ||
} | ||
const unbind = !container || (!container.is(e.target) && container.has(e.target).length === 0); | ||
if (unbind) { | ||
$(document).unbind(`click.${namespace}`); | ||
close(); | ||
} | ||
}); | ||
} | ||
|
||
function openMenu(namespace, open, close, selector = "div.column-toggle", clickFilters = () => false) { | ||
return e => { | ||
const container = $(e.target).closest(selector); | ||
// add handler to close menu | ||
$(document).bind(`click.${namespace}`, function (e) { | ||
if (clickFilters(e)) { | ||
return; | ||
} | ||
if (!container.is(e.target) && container.has(e.target).length === 0) { | ||
$(document).unbind(`click.${namespace}`); | ||
close(); | ||
} | ||
}); | ||
buildClickHandler(namespace, close, container, clickFilters); | ||
open(e); | ||
}; | ||
} | ||
|
||
export default { openMenu }; | ||
export default { openMenu, buildClickHandler }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters