Skip to content

Commit 373ab43

Browse files
authored
fix(sandpack-tests): introduce hideTestsAndSupressLogs prop (#775)
Closes #768
1 parent 89de9a1 commit 373ab43

File tree

7 files changed

+137
-44
lines changed

7 files changed

+137
-44
lines changed

sandpack-react/src/components/Tests/Header.tsx

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react";
22

33
import { css } from "../../styles";
4-
import { roundedButtonClassName, buttonClassName } from "../../styles/shared";
4+
import { buttonClassName, roundedButtonClassName } from "../../styles/shared";
55
import { classNames } from "../../utils/classNames";
66
import { ConsoleIcon } from "../icons";
77

@@ -34,6 +34,9 @@ interface Props {
3434
watchMode: boolean;
3535
setWatchMode: () => void;
3636
showSuitesOnly: boolean;
37+
showVerboseButton: boolean;
38+
showWatchButton: boolean;
39+
hideTestsAndSupressLogs: boolean;
3740
}
3841

3942
const buttonsClassName = classNames(
@@ -51,6 +54,9 @@ export const Header: React.FC<Props> = ({
5154
watchMode,
5255
setWatchMode,
5356
showSuitesOnly,
57+
showWatchButton,
58+
showVerboseButton,
59+
hideTestsAndSupressLogs,
5460
}) => {
5561
return (
5662
<div className={classNames(wrapperClassName, flexClassName)}>
@@ -87,24 +93,28 @@ export const Header: React.FC<Props> = ({
8793
Suite only
8894
</button>
8995
)}
90-
<button
91-
className={buttonsClassName}
92-
data-active={verbose}
93-
disabled={status === "initialising"}
94-
onClick={setVerbose}
95-
type="button"
96-
>
97-
Verbose
98-
</button>
99-
<button
100-
className={buttonsClassName}
101-
data-active={watchMode}
102-
disabled={status === "initialising"}
103-
onClick={setWatchMode}
104-
type="button"
105-
>
106-
Watch
107-
</button>
96+
{showVerboseButton && (
97+
<button
98+
className={buttonsClassName}
99+
data-active={verbose}
100+
disabled={status === "initialising" || hideTestsAndSupressLogs}
101+
onClick={setVerbose}
102+
type="button"
103+
>
104+
Verbose
105+
</button>
106+
)}
107+
{showWatchButton && (
108+
<button
109+
className={buttonsClassName}
110+
data-active={watchMode}
111+
disabled={status === "initialising"}
112+
onClick={setWatchMode}
113+
type="button"
114+
>
115+
Watch
116+
</button>
117+
)}
108118
</div>
109119
</div>
110120
);

sandpack-react/src/components/Tests/SandpackTests.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ export const SandpackTests: React.FC<
5858
watchMode?: boolean;
5959
onComplete?: (specs: Record<string, Spec>) => void;
6060
actionsChildren?: JSX.Element;
61+
showVerboseButton?: boolean;
62+
showWatchButton?: boolean;
63+
/**
64+
* Hide the tests and supress logs
65+
* If `true` the tests will be hidden and the logs will be supressed. This is useful when you want to run tests in the background and don't want to show the tests to the user.
66+
* @default false
67+
*/
68+
hideTestsAndSupressLogs?: boolean;
6169
} & React.HtmlHTMLAttributes<HTMLDivElement>
6270
> = ({
6371
verbose = false,
@@ -66,6 +74,9 @@ export const SandpackTests: React.FC<
6674
className,
6775
onComplete,
6876
actionsChildren,
77+
showVerboseButton = true,
78+
showWatchButton = true,
79+
hideTestsAndSupressLogs = false,
6980
...props
7081
}) => {
7182
const theme = useSandpackTheme();
@@ -379,6 +390,7 @@ export const SandpackTests: React.FC<
379390
<iframe ref={iframe} style={{ display: "none" }} title="Sandpack Tests" />
380391

381392
<Header
393+
hideTestsAndSupressLogs={hideTestsAndSupressLogs}
382394
setSuiteOnly={(): void =>
383395
setState((s) => ({ ...s, suiteOnly: !s.suiteOnly }))
384396
}
@@ -389,6 +401,8 @@ export const SandpackTests: React.FC<
389401
setState((s) => ({ ...s, watchMode: !s.watchMode }));
390402
}}
391403
showSuitesOnly={state.specsCount > 1}
404+
showVerboseButton={showVerboseButton}
405+
showWatchButton={showWatchButton}
392406
status={state.status}
393407
suiteOnly={state.suiteOnly}
394408
verbose={state.verbose}
@@ -418,6 +432,7 @@ export const SandpackTests: React.FC<
418432
) : (
419433
<>
420434
<Specs
435+
hideTestsAndSupressLogs={hideTestsAndSupressLogs}
421436
openSpec={openSpec}
422437
specs={specs}
423438
status={state.status}

sandpack-react/src/components/Tests/Specs.tsx

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface Props {
2525
verbose: boolean;
2626
status: Status;
2727
openSpec: (name: string) => void;
28+
hideTestsAndSupressLogs?: boolean;
2829
}
2930

3031
const fileContainer = css({
@@ -77,6 +78,7 @@ export const Specs: React.FC<Props> = ({
7778
openSpec,
7879
status,
7980
verbose,
81+
hideTestsAndSupressLogs,
8082
}) => {
8183
return (
8284
<>
@@ -138,36 +140,46 @@ export const Specs: React.FC<Props> = ({
138140
)}
139141

140142
<FilePath
141-
onClick={(): void => openSpec(spec.name)}
143+
onClick={(): void => {
144+
if (!hideTestsAndSupressLogs) {
145+
openSpec(spec.name);
146+
}
147+
}}
142148
path={spec.name}
143149
/>
144150
</div>
145151

146-
{verbose && <Tests tests={tests} />}
152+
{verbose && !hideTestsAndSupressLogs && <Tests tests={tests} />}
147153

148-
{verbose && <Describes describes={describes} />}
154+
{verbose && !hideTestsAndSupressLogs && (
155+
<Describes describes={describes} />
156+
)}
149157

150-
{getFailingTests(spec).map((test) => {
151-
return (
152-
<div
153-
key={`failing-${test.name}`}
154-
className={classNames(gapBottomClassName)}
155-
>
158+
{!hideTestsAndSupressLogs &&
159+
getFailingTests(spec).map((test) => {
160+
return (
156161
<div
157-
className={classNames(failTestClassName, failTextClassName)}
162+
key={`failing-${test.name}`}
163+
className={classNames(gapBottomClassName)}
158164
>
159-
{test.blocks.join(" › ")}{test.name}
165+
<div
166+
className={classNames(
167+
failTestClassName,
168+
failTextClassName
169+
)}
170+
>
171+
{test.blocks.join(" › ")}{test.name}
172+
</div>
173+
{test.errors.map((e) => (
174+
<FormattedError
175+
key={`failing-${test.name}-error`}
176+
error={e}
177+
path={test.path}
178+
/>
179+
))}
160180
</div>
161-
{test.errors.map((e) => (
162-
<FormattedError
163-
key={`failing-${test.name}-error`}
164-
error={e}
165-
path={test.path}
166-
/>
167-
))}
168-
</div>
169-
);
170-
})}
181+
);
182+
})}
171183
</div>
172184
);
173185
})}

sandpack-react/src/components/Tests/Tests.stories.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,35 @@ export const VerboseMode: React.FC = () => {
147147
);
148148
};
149149

150+
/**
151+
* This story is used to test the `hideTestsAndSupressLogs` prop.
152+
* Tests content should not be visible in the tests console.
153+
* It is useful when you want to hide the tests from the user.
154+
*
155+
*/
156+
export const HiddenTests: React.FC = () => {
157+
return (
158+
<SandpackProvider
159+
customSetup={{ entry: "add.ts" }}
160+
files={{
161+
"/add.test.ts": addTests,
162+
"/add.ts": add,
163+
"/src/app/sub.ts": sub,
164+
"/src/app/sub.test.ts": subTests,
165+
}}
166+
options={{
167+
visibleFiles: ["/add.ts"],
168+
}}
169+
theme={dracula}
170+
>
171+
<SandpackLayout style={{ "--sp-layout-height": "70vh" } as CSSProperties}>
172+
<SandpackCodeEditor showRunButton={false} showLineNumbers />
173+
<SandpackTests hideTestsAndSupressLogs />
174+
</SandpackLayout>
175+
</SandpackProvider>
176+
);
177+
};
178+
150179
export const OneTestFile: React.FC = () => {
151180
return (
152181
<SandpackProvider

sandpack-react/src/contexts/sandpackContext.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export const SandpackProvider: React.FC<SandpackProviderProps> = (props) => {
1717
const [clientState, { dispatchMessage, addListener, ...clientOperations }] =
1818
useClient(props, fileState);
1919
const appState = useAppState(props, fileState.files);
20-
2120
React.useEffect(() => {
2221
clientOperations.initializeSandpackIframe();
2322
}, []);

sandpack-react/src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ export interface SandpackOptions {
107107
wrapContent?: boolean;
108108
resizablePanels?: boolean;
109109
codeEditor?: SandpackCodeOptions;
110-
111110
/**
112111
* This disables editing of content by the user in all files.
113112
*/
@@ -549,7 +548,6 @@ export interface SandpackState {
549548
resetFile: (path: string) => void;
550549
resetAllFiles: () => void;
551550
registerReactDevTools: (value: ReactDevToolsMode) => void;
552-
553551
/**
554552
* Element refs
555553
* Different components inside the SandpackProvider might register certain elements of interest for sandpack

website/docs/src/pages/advanced-usage/components.mdx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,36 @@ export default () => (
504504
`}
505505
</CodeBlock>
506506

507+
### Hiding Tests
508+
509+
You can hide the test files using the `visibleFiles` prop. Additionally, if you want to suppress test content and only show the test results, you can use the `hideTestsAndSuppressLogs` option.
510+
This option will hide the test files, suppress the console logs, and disable the verbose button.
511+
512+
<CodeBlock stack>
513+
{`
514+
import {
515+
SandpackProvider,
516+
SandpackLayout,
517+
SandpackCodeEditor,
518+
SandpackTests,
519+
} from "@codesandbox/sandpack-react";
520+
521+
export default () => (
522+
<SandpackProvider
523+
template="test-ts"
524+
options={{
525+
visibleFiles: ["/add.ts"],
526+
}}
527+
>
528+
<SandpackLayout>
529+
<SandpackCodeEditor />
530+
<SandpackTests hideTestsAndSupressLogs />
531+
</SandpackLayout>
532+
</SandpackProvider>
533+
);
534+
`}
535+
</CodeBlock>
536+
507537
## Console
508538

509539
`SandpackConsole` is a Sandpack devtool that allows printing the console logs from a Sandpack client. It is designed to be a light version of a browser console, which means that it's limited to a set of common use cases you may encounter when coding.
@@ -672,4 +702,4 @@ of them comunicate with sandpack through the shared context.
672702
<Callout icon="🎉">
673703
**Congrats!**<br/>
674704
You can now easily create a custom Sandpack component by reusing some of the building components of the library. The next step is to build your own sandpack components with the help of our custom hooks.
675-
</Callout>
705+
</Callout>

0 commit comments

Comments
 (0)