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

feat(SandpackTests): Add SandpackTests component #562

Merged
merged 52 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
4fad824
Bump `package-build-stats` to a newer version without `node-sass` dep…
mattphillips Aug 25, 2022
6366981
Add `SandpackTest` component
mattphillips Aug 25, 2022
e06c30c
Inline mapped components
mattphillips Aug 25, 2022
33efd8d
Extract common functions to utils
mattphillips Aug 25, 2022
9b6131e
Move test message types to `sandpack-client`
mattphillips Aug 25, 2022
1c518bb
Move test dispatch message types to `sandpack-client`
mattphillips Aug 25, 2022
a3ec725
Add missing `type` to test `RunButton`
mattphillips Aug 25, 2022
b149dcc
Remove tailwind styles from Toggle
mattphillips Aug 25, 2022
072f1e7
Add `Tests` export to public API
mattphillips Aug 25, 2022
dab1ee1
Remove todo
mattphillips Aug 25, 2022
4cc9820
Add title to iframe
mattphillips Aug 25, 2022
bb02627
set height
danilowoz Aug 26, 2022
302b485
update theme and styles
danilowoz Aug 26, 2022
133d6ca
Update 10 files
danilowoz Aug 26, 2022
486c870
Update 5 files
danilowoz Aug 26, 2022
c691da6
Fix broken test suite results summary
mattphillips Aug 26, 2022
908430f
Update 3 files
danilowoz Aug 26, 2022
cb52203
Run all tests when tests are initialised and watchMode is enabled
mattphillips Aug 26, 2022
9894a4d
Fix broken import
mattphillips Aug 26, 2022
3aa08d0
Add `test-ts` template
mattphillips Aug 26, 2022
8542b52
Watch mode should only run current spec when active file is a test
mattphillips Aug 26, 2022
ebd6bb8
Add error describe/test title highlighting
mattphillips Aug 26, 2022
545fffe
Add verbose test example
mattphillips Aug 26, 2022
51e867f
Update 10 files
danilowoz Aug 29, 2022
62cfdd5
Update 3 files
danilowoz Aug 29, 2022
549cb79
Update 17 files
danilowoz Aug 29, 2022
acce75e
Update Templates.spec.js
danilowoz Aug 29, 2022
c5443f6
Update Templates.spec.js
danilowoz Aug 29, 2022
67c7cbb
Update 3 files
danilowoz Aug 29, 2022
cdd7d54
Replace `immer` and `lodash.set` with `clean-set`
mattphillips Aug 29, 2022
70d747c
Add `RUN` spec label
mattphillips Aug 29, 2022
ea60302
add className to test component
danilowoz Aug 30, 2022
090a79d
merge from main
danilowoz Aug 30, 2022
b0cd5ab
Fix after merging
danilowoz Aug 30, 2022
7c52183
Update Playground.stories.tsx
danilowoz Aug 30, 2022
3f4e1aa
Layout tweaks (wip)
danilowoz Aug 30, 2022
bb353e0
Add `onComplete` optional callback to `SandpackTests`
mattphillips Aug 30, 2022
8d3a167
new test layout (wip)
danilowoz Aug 30, 2022
a5bdc4c
Merge branch 'mp/sandpack-test' of https://github.com/mattphillips/sa…
danilowoz Aug 30, 2022
67f9fd0
Prefix html ids with clientId
mattphillips Aug 30, 2022
7eafab5
Update `Tests` docs
mattphillips Aug 30, 2022
4a56734
Inline `ExtendedSandpackTests` component
mattphillips Aug 30, 2022
6def1cf
Tests: layout tweaks
danilowoz Aug 30, 2022
f2fb4c1
Update SandpackTests.tsx and components.md
danilowoz Aug 30, 2022
252d492
Set of tweaks to show off components
danilowoz Aug 30, 2022
5e6c05a
`suite only` button is only shown when there are multiple spec files
mattphillips Aug 30, 2022
d3fa8b2
Update 4 files
danilowoz Aug 31, 2022
fea0aed
Update Sandpack.test.tsx and styles.test.ts.snap
danilowoz Aug 31, 2022
7961d21
Update 7 files
danilowoz Aug 31, 2022
6dd9d65
Revert yarn.lock
danilowoz Aug 31, 2022
4135c95
Update yarn.lock
danilowoz Aug 31, 2022
feaf291
Update main.css and Sandpack.tsx
danilowoz Aug 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .codesandbox/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@
"command": "yarn workspace @codesandbox/sandpack-react build"
},
"dev:website-docs": {
"name": "Website docs",
"name": "Documentation",
"command": "yarn dev:docs",
"runAtStart": false
},
"dev:sandpack-react": {
"name": "Storybook",
"command": "yarn dev:react",
"runAtStart": true
"runAtStart": true,
"preview": {
"port": 6006,
"pr-link": "direct"
}
},
"dev:website-landing": {
"name": "Website landing",
Expand All @@ -66,4 +70,4 @@
"runAtStart": false
}
}
}
}
2 changes: 2 additions & 0 deletions cypress/integration/Templates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const getIframeBody = () => {

describe("Templates", () => {
Object.keys(SANDBOX_TEMPLATES).forEach((template) => {
if (template === "test-ts") return null; // tests there is no preview

it(`Should run the ${template} template`, () => {
accessPage(template);

Expand Down
120 changes: 120 additions & 0 deletions sandpack-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,125 @@ export interface NpmRegistry {
registryAuthToken?: string;
}

type TestStatus = "running" | "pass" | "fail";

export type TestError = Error & {
matcherResult?: boolean;
mappedErrors?: Array<{
fileName: string;
_originalFunctionName: string;
_originalColumnNumber: number;
_originalLineNumber: number;
_originalScriptCode: Array<{
lineNumber: number;
content: string;
highlight: boolean;
}> | null;
}>;
};

export interface Test {
name: string;
blocks: string[];
status: TestStatus;
path: string;
errors: TestError[];
duration?: number | undefined;
}

export type SandboxTestMessage =
| RunAllTests
| RunTests
| ClearJestErrors
| ({ type: "test" } & (
| InitializedTestsMessage
| TestCountMessage
| TotalTestStartMessage
| TotalTestEndMessage
| AddFileMessage
| RemoveFileMessage
| FileErrorMessage
| DescribeStartMessage
| DescribeEndMessage
| AddTestMessage
| TestStartMessage
| TestEndMessage
));

interface InitializedTestsMessage {
event: "initialize_tests";
}

interface ClearJestErrors {
type: "action";
action: "clear-errors";
source: "jest";
path: string;
}

interface TestCountMessage {
event: "test_count";
count: number;
}

interface TotalTestStartMessage {
event: "total_test_start";
}

interface TotalTestEndMessage {
event: "total_test_end";
}

interface AddFileMessage {
event: "add_file";
path: string;
}

interface RemoveFileMessage {
event: "remove_file";
path: string;
}

interface FileErrorMessage {
event: "file_error";
path: string;
error: TestError;
}

interface DescribeStartMessage {
event: "describe_start";
blockName: string;
}

interface DescribeEndMessage {
event: "describe_end";
}

interface AddTestMessage {
event: "add_test";
testName: string;
path: string;
}

interface TestStartMessage {
event: "test_start";
test: Test;
}

interface TestEndMessage {
event: "test_end";
test: Test;
}

interface RunAllTests {
type: "run-all-tests";
}

interface RunTests {
type: "run-tests";
path: string;
}

export type SandpackMessage = BaseSandpackMessage &
(
| {
Expand Down Expand Up @@ -258,6 +377,7 @@ export type SandpackMessage = BaseSandpackMessage &
data: string[];
}>;
}
| SandboxTestMessage
);

export type Template =
Expand Down
26 changes: 26 additions & 0 deletions sandpack-react/.storybook/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,29 @@ p {
margin: 0;
margin-bottom: 0.3em;
}

.playground-grid {
display: grid;
grid-template-columns: 200px 400px 400px;
grid-template-rows: repeat(2, 250px);
gap: 1px;
}

.playground-grid .sp-file-explorer {
grid-area: 1 / 1 / 3 / 2;
}

.playground-grid .sp-editor {
grid-area: 1 / 2 / 2 / 3;
}

.playground-grid .sp-previe {
grid-area: 1 / 3 / 2 / 4;
}

.playground-grid .sp-consol {
grid-area: 2 / 2 / 3 / 3;
}
.playground-grid .sp-tests {
grid-area: 2 / 3 / 3 / 4;
}
1 change: 1 addition & 0 deletions sandpack-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@codesandbox/sandpack-client": "^1.5.4",
"@react-hook/intersection-observer": "^3.1.1",
"@stitches/core": "^1.2.6",
"clean-set": "^1.1.2",
"codesandbox-import-util-types": "^2.2.3",
"lodash.isequal": "^4.5.0",
"lz-string": "^1.4.4",
Expand Down
82 changes: 65 additions & 17 deletions sandpack-react/src/Playground.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SandpackLayout,
SandpackFileExplorer,
SandpackConsole,
SandpackTests,
} from "./";

export default {
Expand All @@ -25,21 +26,22 @@ export const Main = (): JSX.Element => {
Editor: true,
FileExplorer: true,
Console: true,
Tests: true,
},
Options: {
showTabs: true,
showLineNumbers: true,
showInlineErrors: true,
closableTabs: true,
wrapContent: true,
wrapContent: false,
readOnly: false,
showReadOnly: true,
showNavigator: true,
showRefreshButton: true,
consoleShowHeader: true,
},
Template: "react" as const,
Theme: "auto",
Template: "exhaustedFilesTests" as const,
Theme: "light",
});

const update = (key: any, value: any): void => {
Expand Down Expand Up @@ -80,6 +82,9 @@ export const Main = (): JSX.Element => {
}
value={config.Template}
>
<option value="exhaustedFilesTests">
exhaustedFilesTests
</option>
{Object.keys(SANDBOX_TEMPLATES).map((tem) => (
<option value={tem}>{tem}</option>
))}
Expand Down Expand Up @@ -135,25 +140,68 @@ export const Main = (): JSX.Element => {
</div>

<SandpackProvider
template={config.Template}
customSetup={{
dependencies:
config.Template === "exhaustedFilesTests"
? exhaustedFilesTests.dependencies
: {},
}}
files={
config.Template === "exhaustedFilesTests"
? exhaustedFilesTests.files
: {}
}
template={
config.Template === "exhaustedFilesTests" ? null : config.Template
}
theme={themes[config.Theme] || config.Theme}
>
<SandpackLayout>
{config.Components.FileExplorer && <SandpackFileExplorer />}
{config.Components.Editor && (
<SandpackCodeEditor {...codeEditorOptions} />
)}
{config.Components.Preview && (
<SandpackPreview
showNavigator={config.Options?.showNavigator}
showRefreshButton={config.Options?.showRefreshButton}
/>
)}
{config.Components.Console && (
<SandpackConsole showHeader={config.Options.consoleShowHeader} />
)}
<div className="playground-grid">
{config.Components.FileExplorer && <SandpackFileExplorer />}
{config.Components.Editor && (
<SandpackCodeEditor {...codeEditorOptions} />
)}
{config.Components.Preview && (
<SandpackPreview
showNavigator={config.Options?.showNavigator}
showRefreshButton={config.Options?.showRefreshButton}
/>
)}

{config.Components.Console && (
<SandpackConsole showHeader={config.Options.consoleShowHeader} />
)}
{config.Components.Tests && <SandpackTests />}
</div>
</SandpackLayout>
</SandpackProvider>
</div>
);
};

const defaultTemplate = SANDBOX_TEMPLATES["react-ts"];

const exhaustedFilesTests = {
...defaultTemplate,
dependencies: {
...defaultTemplate.dependencies,
"@testing-library/react": "^13.3.0",
"@testing-library/jest-dom": "^5.16.5",
},
files: {
"/src/index.tsx": SANDBOX_TEMPLATES["react-ts"].files["/index.tsx"],
"/src/App.tsx": `console.log("Hello world");\n\n${SANDBOX_TEMPLATES["react-ts"].files["/App.tsx"].code}`,
"/src/App.test.tsx": `import '@testing-library/jest-dom';
import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';

test('renders welcome message', () => {
render(<App />);
expect(screen.getByText('Hello World')).toBeInTheDocument();
});`,
"/src/styles.css": SANDBOX_TEMPLATES["react-ts"].files["/styles.css"],
"/package.json": JSON.stringify({ main: "/src/index.tsx" }),
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { THEME_PREFIX } from "../../styles";
import {
buttonClassName,
iconStandaloneClassName,
actionButtonClassName,
roundedButtonClassName,
} from "../../styles/shared";
import { classNames } from "../../utils/classNames";

Expand All @@ -24,7 +24,7 @@ export const OpenInCodeSandboxButton = (): JSX.Element | null => {
c("button", "icon-standalone"),
buttonClassName,
iconStandaloneClassName,
actionButtonClassName
roundedButtonClassName
)}
>
<ExportIcon />
Expand Down
4 changes: 2 additions & 2 deletions sandpack-react/src/common/RunButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from "react";
import { useSandpack } from "../hooks/useSandpack";
import { RunIcon } from "../icons";
import { css, THEME_PREFIX } from "../styles";
import { actionButtonClassName, buttonClassName } from "../styles/shared";
import { roundedButtonClassName, buttonClassName } from "../styles/shared";
import { classNames } from "../utils/classNames";

const runButtonClassName = css({
Expand All @@ -30,7 +30,7 @@ export const RunButton = ({
className={classNames(
c("button"),
buttonClassName,
actionButtonClassName,
roundedButtonClassName,
runButtonClassName,
className
)}
Expand Down
4 changes: 4 additions & 0 deletions sandpack-react/src/common/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export const stackClassName = css({
backgroundColor: "$colors$surface1",
transition: "height $transitions$default",
gap: 1, // border between components

[`&:has(.${THEME_PREFIX}-stack)`]: {
backgroundColor: "$colors$surface2",
},
});

/**
Expand Down
2 changes: 1 addition & 1 deletion sandpack-react/src/components/CodeEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const SandpackCodeEditor = React.forwardRef<
};

return (
<SandpackStack style={style}>
<SandpackStack className={c("editor")} style={style}>
{shouldShowTabs && <FileTabs closableTabs={closableTabs} />}

<div className={classNames(c("code-editor"), editorClassName)}>
Expand Down
Loading