Skip to content

Commit

Permalink
test(sandpackutils): cover getSandpackStateFromProps [skip ci] (#336)
Browse files Browse the repository at this point in the history
* tests(sandpackutils): cover getSandpackStateFromProps

* Update sandpackUtils.test.ts
  • Loading branch information
danilowoz authored Jan 27, 2022
1 parent 9155a1e commit 2ab3dee
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 28 deletions.
164 changes: 137 additions & 27 deletions sandpack-react/src/utils/sandpackUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import {
} from "./sandpackUtils";

describe(getSandpackStateFromProps, () => {
/**
* activePath
*/
test("it returns the main file in case activePath doesn't exist", () => {
const setup = getSandpackStateFromProps({
template: "react",
activePath: "NO_EXIST.js",
});

expect(setup.activePath).not.toBe("NO_EXIST.js");
expect(setup.activePath).toBe(REACT_TEMPLATE.main);
});

test("always return an activeFile", () => {
const template = getSandpackStateFromProps({ template: "react" });
expect(template.activePath).toBe("/App.js");
Expand All @@ -19,8 +32,42 @@ describe(getSandpackStateFromProps, () => {
expect(customSetup.activePath).toBe("foo.js");
});

test("show activePath even when it's hidden", () => {
const setup = getSandpackStateFromProps({
template: "react",
activePath: "/App.js",
customSetup: {
files: {
"/App.js": { hidden: true, code: "" },
"/custom.js": { hidden: true, code: "" },
},
},
});

expect(setup.activePath).toEqual("/App.js");
});

test("activePath overrides the customSetup.main", () => {
const setup = getSandpackStateFromProps({
template: "react",
activePath: "/App.js",
customSetup: {
main: "/custom.js",
files: {
"/App.js": "",
"/custom.js": "",
},
},
});

expect(setup.activePath).toEqual("/App.js");
});

/**
* hidden file
*/
test("exclude hidden files from template", () => {
const output = getSandpackStateFromProps({ template: "react" });
const setup = getSandpackStateFromProps({ template: "react" });
const collectFilenames = Object.entries(REACT_TEMPLATE.files).reduce(
(acc, [key, value]) => {
if (!value.hidden) {
Expand All @@ -32,11 +79,11 @@ describe(getSandpackStateFromProps, () => {
[] as string[]
);

expect(output.openPaths.sort()).toEqual(collectFilenames.sort());
expect(setup.openPaths.sort()).toEqual(collectFilenames.sort());
});

test("exclude hidden files from custom files", () => {
const output = getSandpackStateFromProps({
const setup = getSandpackStateFromProps({
customSetup: {
entry: "/App.js",
files: {
Expand All @@ -46,11 +93,11 @@ describe(getSandpackStateFromProps, () => {
},
});

expect(output.openPaths.sort()).toEqual(["/App.js"]);
expect(setup.openPaths.sort()).toEqual(["/App.js"]);
});

test("exclude hidden files from custom files & template", () => {
const output = getSandpackStateFromProps({
const setup = getSandpackStateFromProps({
template: "react",
customSetup: {
files: {
Expand All @@ -60,14 +107,30 @@ describe(getSandpackStateFromProps, () => {
},
});

expect(output.openPaths.sort()).toEqual(["/App.js"]);
expect(setup.openPaths.sort()).toEqual(["/App.js"]);
});

test("not allow to hidden the entry file", () => {
test("show files which are `hidden` & `active` at the same time", () => {
const setup = getSandpackStateFromProps({
template: "react",
customSetup: {
files: {
"/App.js": { hidden: true, active: true, code: "" },
"/custom.js": { hidden: true, code: "" },
},
},
});

expect(setup.openPaths.sort()).toEqual(["/App.js"]);
});

/**
* entry file
*/
test("it needs to provide a entry file, when template is omitted", () => {
try {
getSandpackStateFromProps({
customSetup: {
entry: "/App.js",
files: {
"/App.js": { hidden: true, code: "" },
"/custom.js": { hidden: true, code: "" },
Expand All @@ -82,42 +145,89 @@ describe(getSandpackStateFromProps, () => {
}
});

test("show files which are `hidden` & `active` at the same time", () => {
const output = getSandpackStateFromProps({
test("it updates the entry file in the package.json", () => {
const setup = getSandpackStateFromProps({
template: "react",
customSetup: {
files: {
"/App.js": { hidden: true, active: true, code: "" },
"/custom.js": { hidden: true, code: "" },
},
entry: "foo.ts",
files: { "foo.ts": "" },
},
});

expect(output.openPaths.sort()).toEqual(["/App.js"]);
const packageContent = JSON.parse(setup.files["/package.json"].code);
expect(packageContent.main).toBe("foo.ts");
});

test("show `activePath` even when it's hidden", () => {
const output = getSandpackStateFromProps({
/**
* openPaths
*/
test("should not show invalid files into `openPaths`", () => {
const setup = getSandpackStateFromProps({
template: "react",
activePath: "/App.js",
openPaths: ["/App.js", "not-exist.js"],
});

expect(setup.openPaths).toEqual(["/App.js"]);
});

/**
* main file (will be deprecated)
*/
test("it uses main as activePath", () => {
const setup = getSandpackStateFromProps({
template: "react",
customSetup: {
main: "myfile.js",
files: { "myfile.js": "" },
},
});

expect(setup.activePath).toEqual("myfile.js");
});

/**
* dependencies
*/
test("it creates a package.json with the dependencies", () => {
const setup = getSandpackStateFromProps({
customSetup: {
entry: "index.js",
files: { "index.js": "" },
dependencies: { foo: "*" },
},
});

const packageContent = JSON.parse(setup.files["/package.json"].code);
expect(packageContent.dependencies).toEqual({ foo: "*" });
});

test("it defatuls to a package.json", () => {
const setup = getSandpackStateFromProps({
customSetup: {
entry: "index.js",
files: {
"/App.js": { hidden: true, code: "" },
"/custom.js": { hidden: true, code: "" },
"index.js": "",
},
},
});

expect(output.activePath).toEqual("/App.js");
const packageContent = JSON.parse(setup.files["/package.json"].code);
expect(packageContent.dependencies).toEqual({});
});

test("should not show invalid files into `openPaths`", () => {
const output = getSandpackStateFromProps({
template: "react",
openPaths: ["/App.js", "not-exist.js"],
});
/**
* environment
*/
it("environment default to parcel", () => {
const setup = getSandpackStateFromProps({});

expect(setup.environment).toBe("parcel");
});

it("environment default to the custom template environment", () => {
const setup = getSandpackStateFromProps({ template: "svelte" });

expect(output.openPaths).toEqual(["/App.js"]);
expect(setup.environment).toBe("svelte");
});
});

Expand Down
2 changes: 1 addition & 1 deletion sandpack-react/src/utils/sandpackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const getSandpackStateFromProps = (
}

// If no activePath is specified, use the first open file
if (!activePath) {
if (!activePath || !projectSetup.files[activePath]) {
activePath = projectSetup.main || openPaths[0];
}

Expand Down

0 comments on commit 2ab3dee

Please sign in to comment.