/
main.tsx
185 lines (164 loc) · 4.87 KB
/
main.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* global Cypress, JSX */
import { action, runInAction } from 'mobx'
import { observer } from 'mobx-react'
import cs from 'classnames'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import { render } from 'react-dom'
// @ts-ignore
import EQ from 'css-element-queries/src/ElementQueries'
import { RunnablesErrorModel } from './runnables/runnable-error'
import appState, { AppState } from './lib/app-state'
import events, { Runner, Events } from './lib/events'
import ForcedGcWarning from './lib/forced-gc-warning'
import runnablesStore, { RunnablesStore } from './runnables/runnables-store'
import scroller, { Scroller } from './lib/scroller'
import statsStore, { StatsStore } from './header/stats-store'
import shortcuts from './lib/shortcuts'
import Header, { ReporterHeaderProps } from './header/header'
import Runnables from './runnables/runnables'
interface BaseReporterProps {
appState: AppState
className?: string
runnablesStore: RunnablesStore
runner: Runner
scroller: Scroller
statsStore: StatsStore
events: Events
error?: RunnablesErrorModel
resetStatsOnSpecChange?: boolean
renderReporterHeader?: (props: ReporterHeaderProps) => JSX.Element;
spec: Cypress.Cypress['spec']
experimentalStudioEnabled: boolean
/** Used for component testing front-end */
specRunId?: string | null
}
export interface SingleReporterProps extends BaseReporterProps{
runMode: 'single',
}
export interface MultiReporterProps extends BaseReporterProps{
runMode: 'multi',
allSpecs: Array<Cypress.Cypress['spec']>
}
@observer
class Reporter extends Component<SingleReporterProps | MultiReporterProps> {
static propTypes = {
error: PropTypes.shape({
title: PropTypes.string.isRequired,
link: PropTypes.string,
callout: PropTypes.string,
message: PropTypes.string.isRequired,
}),
runner: PropTypes.shape({
emit: PropTypes.func.isRequired,
on: PropTypes.func.isRequired,
}).isRequired,
spec: PropTypes.shape({
name: PropTypes.string.isRequired,
relative: PropTypes.string.isRequired,
absolute: PropTypes.string.isRequired,
}),
experimentalStudioEnabled: PropTypes.bool,
}
static defaultProps = {
runMode: 'single',
appState,
events,
runnablesStore,
scroller,
statsStore,
}
render () {
const {
appState,
className,
runMode,
runnablesStore,
scroller,
error,
events,
statsStore,
experimentalStudioEnabled,
renderReporterHeader = (props: ReporterHeaderProps) => <Header {...props}/>,
} = this.props
return (
<div className={cs(className, 'reporter', {
multiSpecs: runMode === 'multi',
'experimental-studio-enabled': experimentalStudioEnabled,
'studio-active': appState.studioActive,
})}>
{renderReporterHeader({ appState, statsStore })}
{this.props.runMode === 'single' ? (
<Runnables
appState={appState}
error={error}
runnablesStore={runnablesStore}
scroller={scroller}
spec={this.props.spec}
/>
) : this.props.allSpecs.map((spec) => (
<Runnables
key={spec.relative}
appState={appState}
error={error}
runnablesStore={runnablesStore}
scroller={scroller}
spec={spec}
/>
))}
<ForcedGcWarning
appState={appState}
events={events}
/>
</div>
)
}
// this hook will only trigger if we switch spec file at runtime
// it never happens in normal e2e but can happen in component-testing mode
componentDidUpdate (newProps: BaseReporterProps) {
this.props.runnablesStore.setRunningSpec(this.props.spec.relative)
if (
this.props.resetStatsOnSpecChange &&
this.props.specRunId !== newProps.specRunId
) {
runInAction('reporter:stats:reset', () => {
this.props.statsStore.reset()
})
}
}
componentDidMount () {
const { spec, appState, runnablesStore, runner, scroller, statsStore } = this.props
action('set:scrolling', () => {
appState.setAutoScrolling(appState.autoScrollingEnabled)
})()
this.props.events.init({
appState,
runnablesStore,
scroller,
statsStore,
})
this.props.events.listen(runner)
shortcuts.start()
EQ.init()
this.props.runnablesStore.setRunningSpec(spec.relative)
}
componentWillUnmount () {
shortcuts.stop()
}
}
declare global {
interface Window {
Cypress: any
state: AppState
render: ((props: Partial<BaseReporterProps>) => void)
}
}
// NOTE: this is for testing Cypress-in-Cypress
if (window.Cypress) {
window.state = appState
window.render = (props) => {
// @ts-ignore
render(<Reporter {...props as Required<ReporterProps>} />, document.getElementById('app'))
}
}
export { Reporter }