@@ -2,7 +2,7 @@ import * as messages from '@cucumber/messages'
2
2
import { getWorstTestStepResult } from '@cucumber/messages'
3
3
import { faChevronRight } from '@fortawesome/free-solid-svg-icons'
4
4
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
5
- import React from 'react'
5
+ import React , { FunctionComponent , useContext , useMemo , useState } from 'react'
6
6
import {
7
7
Accordion ,
8
8
AccordionItem ,
@@ -14,9 +14,7 @@ import {
14
14
import CucumberQueryContext from '../../CucumberQueryContext'
15
15
import GherkinQueryContext from '../../GherkinQueryContext'
16
16
import UriContext from '../../UriContext'
17
- import { GherkinDocument } from '../gherkin/GherkinDocument'
18
- import { MDG } from '../gherkin/MDG'
19
- import { StatusIcon } from '../gherkin/StatusIcon'
17
+ import { GherkinDocument , MDG , StatusIcon } from '../gherkin'
20
18
import styles from './GherkinDocumentList.module.scss'
21
19
22
20
interface IProps {
@@ -25,44 +23,43 @@ interface IProps {
25
23
preExpand ?: boolean
26
24
}
27
25
28
- export const GherkinDocumentList : React . FunctionComponent < IProps > = ( {
29
- gherkinDocuments,
30
- preExpand,
31
- } ) => {
32
- const gherkinQuery = React . useContext ( GherkinQueryContext )
33
- const cucumberQuery = React . useContext ( CucumberQueryContext )
34
-
26
+ export const GherkinDocumentList : FunctionComponent < IProps > = ( { gherkinDocuments, preExpand } ) => {
27
+ const gherkinQuery = useContext ( GherkinQueryContext )
28
+ const cucumberQuery = useContext ( CucumberQueryContext )
35
29
const gherkinDocs = gherkinDocuments || gherkinQuery . getGherkinDocuments ( )
36
-
37
- const entries : Array < [ string , messages . TestStepResultStatus ] > = gherkinDocs . map (
38
- ( gherkinDocument ) => {
39
- if ( ! gherkinDocument . uri ) throw new Error ( 'No url for gherkinDocument' )
40
- const gherkinDocumentStatus = gherkinDocument . feature
41
- ? getWorstTestStepResult (
42
- cucumberQuery . getPickleTestStepResults ( gherkinQuery . getPickleIds ( gherkinDocument . uri ) )
43
- ) . status
44
- : messages . TestStepResultStatus . UNDEFINED
45
- return [ gherkinDocument . uri , gherkinDocumentStatus ]
46
- }
47
- )
48
- const gherkinDocumentStatusByUri = new Map ( entries )
49
-
50
- // Pre-expand any document that is *not* passed - assuming this is what people want to look at first
51
- const preExpanded = preExpand
52
- ? ( gherkinDocs
53
- . filter (
54
- ( doc ) =>
55
- doc . uri &&
56
- gherkinDocumentStatusByUri . get ( doc . uri ) !== messages . TestStepResultStatus . PASSED
57
- )
58
- . map ( ( doc ) => doc . uri ) as string [ ] )
59
- : [ ]
30
+ const gherkinDocumentStatusByUri = useMemo ( ( ) => {
31
+ const entries : Array < [ string , messages . TestStepResultStatus ] > = gherkinDocs . map (
32
+ ( gherkinDocument ) => {
33
+ if ( ! gherkinDocument . uri ) throw new Error ( 'No url for gherkinDocument' )
34
+ const gherkinDocumentStatus = gherkinDocument . feature
35
+ ? getWorstTestStepResult (
36
+ cucumberQuery . getPickleTestStepResults ( gherkinQuery . getPickleIds ( gherkinDocument . uri ) )
37
+ ) . status
38
+ : messages . TestStepResultStatus . UNDEFINED
39
+ return [ gherkinDocument . uri , gherkinDocumentStatus ]
40
+ }
41
+ )
42
+ return new Map ( entries )
43
+ } , [ gherkinDocs , gherkinQuery , cucumberQuery ] )
44
+ const [ expanded , setExpanded ] = useState < Array < string | number > > ( ( ) => {
45
+ // Pre-expand any document that is *not* passed - assuming this is what people want to look at first
46
+ return preExpand
47
+ ? ( gherkinDocs
48
+ . filter (
49
+ ( doc ) =>
50
+ doc . uri &&
51
+ gherkinDocumentStatusByUri . get ( doc . uri ) !== messages . TestStepResultStatus . PASSED
52
+ )
53
+ . map ( ( doc ) => doc . uri ) as string [ ] )
54
+ : [ ]
55
+ } )
60
56
61
57
return (
62
58
< Accordion
63
59
allowMultipleExpanded = { true }
64
60
allowZeroExpanded = { true }
65
- preExpanded = { preExpanded }
61
+ preExpanded = { expanded }
62
+ onChange = { setExpanded }
66
63
className = { styles . accordion }
67
64
>
68
65
{ gherkinDocs . map ( ( doc ) => {
@@ -73,7 +70,7 @@ export const GherkinDocumentList: React.FunctionComponent<IProps> = ({
73
70
if ( ! source ) throw new Error ( `No source for ${ doc . uri } ` )
74
71
75
72
return (
76
- < AccordionItem key = { doc . uri } className = { styles . accordionItem } >
73
+ < AccordionItem key = { doc . uri } uuid = { doc . uri } className = { styles . accordionItem } >
77
74
< AccordionItemHeading >
78
75
< AccordionItemButton className = { styles . accordionButton } >
79
76
< FontAwesomeIcon
@@ -87,15 +84,17 @@ export const GherkinDocumentList: React.FunctionComponent<IProps> = ({
87
84
< span > { doc . uri } </ span >
88
85
</ AccordionItemButton >
89
86
</ AccordionItemHeading >
90
- < AccordionItemPanel className = { styles . accordionPanel } >
91
- < UriContext . Provider value = { doc . uri } >
92
- { source . mediaType === messages . SourceMediaType . TEXT_X_CUCUMBER_GHERKIN_PLAIN ? (
93
- < GherkinDocument gherkinDocument = { doc } source = { source } />
94
- ) : (
95
- < MDG uri = { doc . uri } > { source . data } </ MDG >
96
- ) }
97
- </ UriContext . Provider >
98
- </ AccordionItemPanel >
87
+ { expanded . includes ( doc . uri ) && (
88
+ < AccordionItemPanel className = { styles . accordionPanel } >
89
+ < UriContext . Provider value = { doc . uri } >
90
+ { source . mediaType === messages . SourceMediaType . TEXT_X_CUCUMBER_GHERKIN_PLAIN ? (
91
+ < GherkinDocument gherkinDocument = { doc } source = { source } />
92
+ ) : (
93
+ < MDG uri = { doc . uri } > { source . data } </ MDG >
94
+ ) }
95
+ </ UriContext . Provider >
96
+ </ AccordionItemPanel >
97
+ ) }
99
98
</ AccordionItem >
100
99
)
101
100
} ) }
0 commit comments