Skip to content

Commit 9011d30

Browse files
committed
Extract hook logic to useTestRunHooks
This may be used by consumers of <TestRunHooks /> to eg. conditionally render a heading.
1 parent 39f1d86 commit 9011d30

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

src/components/app/TestRunHooks.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
import { TestRunHookFinished } from '@cucumber/messages'
21
import React, { FC } from 'react'
32

4-
import { useQueries } from '../../hooks/useQueries.js'
3+
import { useTestRunHooks } from '../../hooks/useTestRunHooks.js'
54
import { TestRunHookOutcome } from '../results/TestRunHookOutcome.js'
65
import styles from './TestRunHooks.module.scss'
76

87
export const TestRunHooks: FC = () => {
9-
const { cucumberQuery } = useQueries()
10-
const testRunHooksFinished: ReadonlyArray<TestRunHookFinished> =
11-
cucumberQuery.findAllTestRunHookFinished()
8+
const hooks = useTestRunHooks()
9+
1210
return (
1311
<ol aria-label="RunHooks" className={styles.hooks}>
14-
{testRunHooksFinished.map((testRunHookFinished) => {
15-
const testRunHook = cucumberQuery.findHookBy(testRunHookFinished)
16-
12+
{hooks.map(({ testRunHookFinished, hook }) => {
1713
return (
1814
<TestRunHookOutcome
15+
key={hook.id}
1916
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
20-
key={testRunHook!.id}
21-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
22-
hook={testRunHook!}
17+
hook={hook!}
2318
testRunHookFinished={testRunHookFinished}
2419
/>
2520
)

src/hooks/helpers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function ensure<T>(value: T | undefined, message: string): T {
2+
if (!value) {
3+
throw new Error(message)
4+
}
5+
return value
6+
}

src/hooks/useTestRunHooks.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Hook, TestRunHookFinished } from '@cucumber/messages'
2+
3+
import { ensure } from './helpers.js'
4+
import { useQueries } from './useQueries.js'
5+
6+
type RunHooksList = { testRunHookFinished: TestRunHookFinished; hook: Hook }[]
7+
8+
export function useTestRunHooks(): RunHooksList {
9+
const { cucumberQuery } = useQueries()
10+
const testRunHooksFinished: ReadonlyArray<TestRunHookFinished> =
11+
cucumberQuery.findAllTestRunHookFinished()
12+
13+
return testRunHooksFinished.map((testRunHookFinished) => {
14+
const hook = ensure(
15+
cucumberQuery.findHookBy(testRunHookFinished),
16+
'Expected testRunHookFinished to resolve with a hook'
17+
)
18+
19+
return {
20+
testRunHookFinished,
21+
hook,
22+
}
23+
})
24+
}

0 commit comments

Comments
 (0)