-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathmain.tsx
164 lines (143 loc) · 4.62 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
/* global JSX */
import { action, runInAction } from 'mobx'
import { observer } from 'mobx-react'
import cs from 'classnames'
import React, { Component } from 'react'
import { createRoot } from 'react-dom/client'
// @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 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'
import TestingPreferences from './preferences/testing-preferences'
import type { MobxRunnerStore } from '@packages/app/src/store/mobx-runner-store'
export interface BaseReporterProps {
appState: AppState
className?: string
runnablesStore: RunnablesStore
runner: Runner
scroller: Scroller
statsStore: StatsStore
autoScrollingEnabled?: boolean
isSpecsListOpen?: boolean
events: Events
error?: RunnablesErrorModel
resetStatsOnSpecChange?: boolean
renderReporterHeader?: (props: ReporterHeaderProps) => JSX.Element
studioEnabled: boolean
runnerStore: MobxRunnerStore
}
export interface SingleReporterProps extends BaseReporterProps{
runMode: 'single'
}
@observer
class Reporter extends Component<SingleReporterProps> {
static defaultProps: Partial<SingleReporterProps> = {
runMode: 'single',
appState,
events,
runnablesStore,
scroller,
statsStore,
}
render () {
const {
appState,
className,
runnablesStore,
scroller,
error,
statsStore,
studioEnabled,
renderReporterHeader = (props: ReporterHeaderProps) => <Header {...props}/>,
runnerStore,
} = this.props
return (
<div className={cs(className, 'reporter', {
'studio-active': appState.studioActive,
})}>
{renderReporterHeader({ appState, statsStore, runnablesStore })}
{appState?.isPreferencesMenuOpen ? (
<TestingPreferences appState={appState} />
) : (
runnerStore.spec && <Runnables
appState={appState}
error={error}
runnablesStore={runnablesStore}
scroller={scroller}
spec={runnerStore.spec}
statsStore={statsStore}
studioEnabled={studioEnabled}
canSaveStudioLogs={runnerStore.canSaveStudioLogs}
/>
)}
</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) {
if (!this.props.runnerStore.spec) {
throw Error(`Expected runnerStore.spec not to be null.`)
}
this.props.runnablesStore.setRunningSpec(this.props.runnerStore.spec.relative)
if (
this.props.resetStatsOnSpecChange &&
this.props.runnerStore.specRunId !== newProps.runnerStore.specRunId
) {
runInAction('reporter:stats:reset', () => {
this.props.statsStore.reset()
})
}
}
componentDidMount () {
const { appState, runnablesStore, runner, scroller, statsStore, autoScrollingEnabled, isSpecsListOpen, runnerStore } = this.props
if (!runnerStore.spec) {
throw Error(`Expected runnerStore.spec not to be null.`)
}
action('set:scrolling', () => {
// set the initial enablement of auto scroll configured inside the user preferences when the app is loaded
appState.setAutoScrollingUserPref(autoScrollingEnabled)
})()
action('set:specs:list', () => {
appState.setSpecsList(isSpecsListOpen ?? false)
})()
this.props.events.init({
appState,
runnablesStore,
scroller,
statsStore,
})
this.props.events.listen(runner)
shortcuts.start()
EQ.init()
this.props.runnablesStore.setRunningSpec(runnerStore.spec.relative)
}
componentWillUnmount () {
shortcuts.stop()
}
}
declare global {
interface Window {
Cypress: any
state: AppState
render: ((props: Partial<BaseReporterProps>) => void)
__CYPRESS_MODE__: 'run' | 'open'
}
}
// NOTE: this is for testing Cypress-in-Cypress
if (window.Cypress) {
window.state = appState
window.render = (props) => {
const container: HTMLElement = document.getElementById('app') as HTMLElement
const root = createRoot(container)
root.render(<Reporter {...props as Required<BaseReporterProps>} />)
}
}
export { Reporter }