|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import React, {useEffect, useState} from 'react'; |
| 4 | +import {bindActionCreators} from 'redux'; |
| 5 | +import {isEmpty} from 'lodash'; |
| 6 | +import {connect} from 'react-redux'; |
| 7 | +import PropTypes from 'prop-types'; |
| 8 | +import classNames from 'classnames'; |
| 9 | +import * as actions from '../../../modules/actions'; |
| 10 | +import Popup from '../../popup'; |
| 11 | +import {getFailedTests, getCheckedTests} from '../../../modules/selectors/tree'; |
| 12 | +import useLocalStorage from '../../../hooks/useLocalStorage'; |
| 13 | + |
| 14 | +import './index.styl'; |
| 15 | + |
| 16 | +const RunMode = Object.freeze({ |
| 17 | + ALL: 'All', |
| 18 | + FAILED: 'Failed', |
| 19 | + CHECKED: 'Checked' |
| 20 | +}); |
| 21 | + |
| 22 | +const RunButton = ({actions, autoRun, isDisabled, isRunning, failedTests, checkedTests}) => { |
| 23 | + const [mode, setMode] = useState(RunMode.ALL); |
| 24 | + const [showCheckboxes] = useLocalStorage('showCheckboxes', false); |
| 25 | + |
| 26 | + const btnClassName = classNames('btn', {'button_blink': isRunning}); |
| 27 | + |
| 28 | + const shouldDisableFailed = isEmpty(failedTests); |
| 29 | + const shouldDisableChecked = !showCheckboxes || isEmpty(checkedTests); |
| 30 | + |
| 31 | + const selectAllTests = () => setMode(RunMode.ALL); |
| 32 | + const selectFailedTests = () => !shouldDisableFailed && setMode(RunMode.FAILED); |
| 33 | + const selectCheckedTests = () => !shouldDisableChecked && setMode(RunMode.CHECKED); |
| 34 | + |
| 35 | + const runAllTests = () => actions.runAllTests(); |
| 36 | + const runFailedTests = () => actions.runFailedTests(failedTests); |
| 37 | + const runCheckedTests = () => actions.retrySuite(checkedTests); |
| 38 | + |
| 39 | + const handleRunClick = () => { |
| 40 | + const action = { |
| 41 | + [RunMode.ALL]: runAllTests, |
| 42 | + [RunMode.FAILED]: runFailedTests, |
| 43 | + [RunMode.CHECKED]: runCheckedTests |
| 44 | + }[mode]; |
| 45 | + |
| 46 | + action(); |
| 47 | + }; |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (autoRun) { |
| 51 | + runAllTests(); |
| 52 | + } |
| 53 | + }, []); |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + selectCheckedTests(); |
| 57 | + }, [shouldDisableChecked]); |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + const shouldResetFailedMode = mode === RunMode.FAILED && shouldDisableFailed; |
| 61 | + const shouldResetCheckedMode = mode === RunMode.CHECKED && shouldDisableChecked; |
| 62 | + |
| 63 | + if (shouldResetFailedMode || shouldResetCheckedMode) { |
| 64 | + setMode(RunMode.ALL); |
| 65 | + } |
| 66 | + }, [shouldDisableFailed, shouldDisableChecked]); |
| 67 | + |
| 68 | + return ( |
| 69 | + <div className='run-button'> |
| 70 | + <button disabled={isDisabled} onClick={handleRunClick} className={btnClassName}> |
| 71 | + {isRunning ? 'Running' : `Run ${mode.toLowerCase()} tests`} |
| 72 | + </button> |
| 73 | + {!isDisabled && <Popup |
| 74 | + action='hover' |
| 75 | + hideOnClick={true} |
| 76 | + target={<div className='run-button__dropdown' />} |
| 77 | + > |
| 78 | + <ul className='run-mode'> |
| 79 | + <li |
| 80 | + className='run-mode__item' |
| 81 | + onClick={selectAllTests} |
| 82 | + > |
| 83 | + {RunMode.ALL} |
| 84 | + </li> |
| 85 | + <li |
| 86 | + className={classNames('run-mode__item', {'run-mode__item_disabled': shouldDisableFailed})} |
| 87 | + onClick={selectFailedTests}>{RunMode.FAILED} |
| 88 | + </li> |
| 89 | + <li |
| 90 | + className={classNames('run-mode__item', {'run-mode__item_disabled': shouldDisableChecked})} |
| 91 | + onClick={selectCheckedTests} |
| 92 | + > |
| 93 | + {RunMode.CHECKED} |
| 94 | + </li> |
| 95 | + </ul> |
| 96 | + </Popup>} |
| 97 | + </div> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +RunButton.propTypes = { |
| 102 | + // from store |
| 103 | + autoRun: PropTypes.bool.isRequired, |
| 104 | + isDisabled: PropTypes.bool, |
| 105 | + isRunning: PropTypes.bool, |
| 106 | + failedTests: PropTypes.arrayOf(PropTypes.shape({ |
| 107 | + testName: PropTypes.string, |
| 108 | + browserName: PropTypes.string |
| 109 | + })).isRequired, |
| 110 | + checkedTests: PropTypes.arrayOf(PropTypes.shape({ |
| 111 | + testName: PropTypes.string, |
| 112 | + browserName: PropTypes.string |
| 113 | + })).isRequired |
| 114 | +}; |
| 115 | + |
| 116 | +export default connect( |
| 117 | + (state) => { |
| 118 | + const autoRun = state.autoRun; |
| 119 | + const allRootSuiteIds = state.tree.suites.allRootIds; |
| 120 | + const processing = state.processing; |
| 121 | + const isDisabled = !allRootSuiteIds.length || processing; |
| 122 | + const isRunning = state.running; |
| 123 | + const failedTests = getFailedTests(state); |
| 124 | + const checkedTests = getCheckedTests(state); |
| 125 | + |
| 126 | + return {autoRun, isDisabled, isRunning, failedTests, checkedTests}; |
| 127 | + }, |
| 128 | + (dispatch) => ({actions: bindActionCreators(actions, dispatch)}) |
| 129 | +)(RunButton); |
0 commit comments