Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(classNames context): general improvements #771

Merged
merged 13 commits into from
Apr 7, 2023
Prev Previous commit
Next Next commit
Update 3 files
danilowoz committed Feb 27, 2023
commit 0df0607495d57cdb632ce9d3450ff866117b3f3a
12 changes: 9 additions & 3 deletions sandpack-react/src/components/Tests/Describes.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react";

import { css } from "../../styles";
import { classNames } from "../../utils/classNames";
import { useClassNames } from "../../utils/classNames";

import type { Test } from "./Tests";
import { Tests } from "./Tests";
@@ -25,6 +25,7 @@ const containerClassName = css({
export const Describes: React.FC<{ describes: Describe[] }> = ({
describes,
}) => {
const classNames = useClassNames();
return (
<>
{describes.map((describe) => {
@@ -36,8 +37,13 @@ export const Describes: React.FC<{ describes: Describe[] }> = ({
const describes = Object.values(describe.describes);

return (
<div key={describe.name} className={classNames(containerClassName)}>
<div className={classNames(nameClassName)}>{describe.name}</div>
<div
key={describe.name}
className={classNames("test-describe", [containerClassName])}
>
<div className={classNames("test-name", [nameClassName])}>
{describe.name}
</div>

<Tests tests={tests} />

5 changes: 3 additions & 2 deletions sandpack-react/src/components/Tests/FormattedError.tsx
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import type { TestError } from "@codesandbox/sandpack-client";
import * as React from "react";

import { css } from "../../styles";
import { classNames } from "../../utils/classNames";
import { useClassNames } from "../../utils/classNames";

import {
failTextClassName,
@@ -23,9 +23,10 @@ const containerClassName = css({
});

export const FormattedError: React.FC<Props> = ({ error, path }) => {
const classNames = useClassNames();
return (
<div
className={classNames(containerClassName)}
className={classNames("test-error", [containerClassName])}
dangerouslySetInnerHTML={{ __html: formatDiffMessage(error, path) }}
/>
);
72 changes: 54 additions & 18 deletions sandpack-react/src/components/Tests/Specs.tsx
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import * as React from "react";

import { css } from "../../styles";
import { buttonClassName } from "../../styles/shared";
import { classNames } from "../../utils/classNames";
import { useClassNames } from "../../utils/classNames";

import type { Describe } from "./Describes";
import { Describes } from "./Describes";
@@ -78,14 +78,22 @@ export const Specs: React.FC<Props> = ({
status,
verbose,
}) => {
const classNames = useClassNames();

return (
<>
{specs.map((spec) => {
if (spec.error) {
return (
<div key={spec.name} className={classNames(gapBottomClassName)}>
<div
key={spec.name}
className={classNames("test-spec", [gapBottomClassName])}
>
<SpecLabel
className={classNames(labelClassName, failBackgroundClassName)}
className={classNames("test-spec-error", [
labelClassName,
failBackgroundClassName,
])}
>
Error
</SpecLabel>
@@ -107,31 +115,41 @@ export const Specs: React.FC<Props> = ({
const stats = getSpecTestResults(spec);

return (
<div key={spec.name} className={classNames(gapBottomClassName)}>
<div className={classNames(fileContainer)}>
<div
key={spec.name}
className={classNames("test-spec-name", [gapBottomClassName])}
>
<div
className={classNames("test-spec-name-container", [
fileContainer,
])}
>
{status === "complete" ? (
stats.fail > 0 ? (
<SpecLabel
className={classNames(
className={classNames("test-spec-complete", [
labelClassName,
failBackgroundClassName
)}
failBackgroundClassName,
])}
>
Fail
</SpecLabel>
) : (
<SpecLabel
className={classNames(
className={classNames("test-spec-pass", [
labelClassName,
passBackgroundClassName
)}
passBackgroundClassName,
])}
>
Pass
</SpecLabel>
)
) : (
<SpecLabel
className={classNames(labelClassName, runBackgroundClassName)}
className={classNames("test-spec-run", [
labelClassName,
runBackgroundClassName,
])}
>
Run
</SpecLabel>
@@ -151,10 +169,15 @@ export const Specs: React.FC<Props> = ({
return (
<div
key={`failing-${test.name}`}
className={classNames(gapBottomClassName)}
className={classNames("test-spec-error", [
gapBottomClassName,
])}
>
<div
className={classNames(failTestClassName, failTextClassName)}
className={classNames("test-spec-error-text", [
failTestClassName,
failTextClassName,
])}
>
{test.blocks.join(" › ")}{test.name}
</div>
@@ -179,8 +202,12 @@ const SpecLabel: React.FC<{ className: string; children: React.ReactNode }> = ({
children,
className,
}) => {
const classNames = useClassNames();

return (
<span className={classNames(specLabelClassName, className)}>
<span
className={classNames("test-spec-label", [specLabelClassName, className])}
>
{children}
</span>
);
@@ -193,14 +220,23 @@ const FilePath: React.FC<{ onClick: () => void; path: string }> = ({
const parts = path.split("/");
const basePath = parts.slice(0, parts.length - 1).join("/") + "/";
const fileName = parts[parts.length - 1];
const classNames = useClassNames();

return (
<button
className={classNames(buttonClassName, filePathButtonClassName)}
className={classNames("test-filename", [
buttonClassName,
filePathButtonClassName,
])}
onClick={onClick}
type="button"
>
<span className={classNames(filePathClassName)}>{basePath}</span>
<span className={classNames(fileNameClassName)}>{fileName}</span>
<span className={classNames("test-filename-base", [filePathClassName])}>
{basePath}
</span>
<span className={classNames("test-filename-file", [fileNameClassName])}>
{fileName}
</span>
</button>
);
};