@@ -2,7 +2,7 @@ import * as messages from '@cucumber/messages'
22import { getWorstTestStepResult } from '@cucumber/messages'
33import { faChevronRight } from '@fortawesome/free-solid-svg-icons'
44import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
5- import React from 'react'
5+ import React , { FunctionComponent , useContext , useMemo , useState } from 'react'
66import {
77 Accordion ,
88 AccordionItem ,
@@ -14,9 +14,7 @@ import {
1414import CucumberQueryContext from '../../CucumberQueryContext'
1515import GherkinQueryContext from '../../GherkinQueryContext'
1616import 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'
2018import styles from './GherkinDocumentList.module.scss'
2119
2220interface IProps {
@@ -25,44 +23,43 @@ interface IProps {
2523 preExpand ?: boolean
2624}
2725
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 )
3529 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+ } )
6056
6157 return (
6258 < Accordion
6359 allowMultipleExpanded = { true }
6460 allowZeroExpanded = { true }
65- preExpanded = { preExpanded }
61+ preExpanded = { expanded }
62+ onChange = { setExpanded }
6663 className = { styles . accordion }
6764 >
6865 { gherkinDocs . map ( ( doc ) => {
@@ -73,7 +70,7 @@ export const GherkinDocumentList: React.FunctionComponent<IProps> = ({
7370 if ( ! source ) throw new Error ( `No source for ${ doc . uri } ` )
7471
7572 return (
76- < AccordionItem key = { doc . uri } className = { styles . accordionItem } >
73+ < AccordionItem key = { doc . uri } uuid = { doc . uri } className = { styles . accordionItem } >
7774 < AccordionItemHeading >
7875 < AccordionItemButton className = { styles . accordionButton } >
7976 < FontAwesomeIcon
@@ -87,15 +84,17 @@ export const GherkinDocumentList: React.FunctionComponent<IProps> = ({
8784 < span > { doc . uri } </ span >
8885 </ AccordionItemButton >
8986 </ 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+ ) }
9998 </ AccordionItem >
10099 )
101100 } ) }
0 commit comments