Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

feat: add vitest + buildTree function #21

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@githubnext/blocks",
"version": "2.3.4",
"version": "2.4.0-1",
"license": "MIT",
"engines": {
"node": ">=14.0.0"
Expand All @@ -17,7 +17,8 @@
],
"scripts": {
"build": "tsup --dts",
"prepublish": "yarn build"
"prepublish": "yarn build",
"test": "vitest"
},
"bin": {
"blocks": "./bin/blocks.js"
Expand Down Expand Up @@ -60,7 +61,8 @@
"react-dom": "^18.1.0",
"tsup": "^5.6.0",
"typescript": "^4.4.4",
"use-debounce": "^8.0.2"
"use-debounce": "^8.0.2",
"vitest": "^0.26.2"
},
"browserslist": {
"production": [
Expand Down
308 changes: 308 additions & 0 deletions test/githubnext-blocks-stub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
{
"sha": "3f9e537fd37b2aba54e9c96f5f8d47a068d5a4d4",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/3f9e537fd37b2aba54e9c96f5f8d47a068d5a4d4",
"tree": [
{
"path": ".github",
"mode": "040000",
"type": "tree",
"sha": "c5c285e55eefbd579ea8e209d879725c77444629",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/c5c285e55eefbd579ea8e209d879725c77444629"
},
{
"path": ".github/blocks",
"mode": "040000",
"type": "tree",
"sha": "bcf902176eedefbddc96df2cf18260199582da85",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/bcf902176eedefbddc96df2cf18260199582da85"
},
{
"path": ".github/blocks/all.json",
"mode": "100644",
"type": "blob",
"sha": "16ecf1b266669cdf1d76216ab319c3047f56dc05",
"size": 940,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/16ecf1b266669cdf1d76216ab319c3047f56dc05"
},
{
"path": ".github/blocks/file",
"mode": "040000",
"type": "tree",
"sha": "903383c44c60cbc1d18097498f08bd9652285858",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/903383c44c60cbc1d18097498f08bd9652285858"
},
{
"path": ".github/blocks/file/githubnext__blocks-examples__chart-block",
"mode": "040000",
"type": "tree",
"sha": "5a19bc924cb26fcbb75cc24b611807199bc631a6",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/5a19bc924cb26fcbb75cc24b611807199bc631a6"
},
{
"path": ".github/blocks/file/githubnext__blocks-examples__chart-block/examples%2Fweather.csv.json",
"mode": "100644",
"type": "blob",
"sha": "14d708c5f9092017f302914c7198df3f39480e9c",
"size": 98,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/14d708c5f9092017f302914c7198df3f39480e9c"
},
{
"path": ".github/blocks/file/githubnext__blocks-examples__chart",
"mode": "040000",
"type": "tree",
"sha": "831e0d5c0a694c937340a805663380405ae724da",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/831e0d5c0a694c937340a805663380405ae724da"
},
{
"path": ".github/blocks/file/githubnext__blocks-examples__chart/examples%2Fweather.csv.json",
"mode": "100644",
"type": "blob",
"sha": "7630239ea0c2bff8e9db83bbec59a122e8d61005",
"size": 55,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/7630239ea0c2bff8e9db83bbec59a122e8d61005"
},
{
"path": ".github/blocks/file/githubnext__blocks-examples__react-feedback-block",
"mode": "040000",
"type": "tree",
"sha": "59399e1841b8c4c628fe5c43acbca47b7966e8a4",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/59399e1841b8c4c628fe5c43acbca47b7966e8a4"
},
{
"path": ".github/blocks/file/githubnext__blocks-examples__react-feedback-block/examples%2FMyComponent.jsx.json",
"mode": "100644",
"type": "blob",
"sha": "a1487119d789eee3778a49fb61b7b434cc6a5a31",
"size": 861,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/a1487119d789eee3778a49fb61b7b434cc6a5a31"
},
{
"path": "README.md",
"mode": "100644",
"type": "blob",
"sha": "f6d1a4b81f49a808b21d7771a59d81f7186dd4d8",
"size": 5841,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/f6d1a4b81f49a808b21d7771a59d81f7186dd4d8"
},
{
"path": "docs",
"mode": "040000",
"type": "tree",
"sha": "20f7bfdef06094f64ce411349f87a37bb15be242",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/20f7bfdef06094f64ce411349f87a37bb15be242"
},
{
"path": "docs/Developing blocks",
"mode": "040000",
"type": "tree",
"sha": "aa44099ccfad821581659376a42d0233b2176e02",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/aa44099ccfad821581659376a42d0233b2176e02"
},
{
"path": "docs/Developing blocks/1 Intro.md",
"mode": "100644",
"type": "blob",
"sha": "97f3ecfb8eb8c074d2f35c00ad35b77bc94973b6",
"size": 2219,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/97f3ecfb8eb8c074d2f35c00ad35b77bc94973b6"
},
{
"path": "docs/Developing blocks/2 Building your first block.md",
"mode": "100644",
"type": "blob",
"sha": "b102b6f091009241b7d5e45d1db2b4a8ba1871b3",
"size": 6822,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/b102b6f091009241b7d5e45d1db2b4a8ba1871b3"
},
{
"path": "docs/Developing blocks/3 Deploying your block.md",
"mode": "100644",
"type": "blob",
"sha": "4adb8736e45beebe035c3942d09302180cabdad3",
"size": 2838,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/4adb8736e45beebe035c3942d09302180cabdad3"
},
{
"path": "docs/Developing blocks/4 API reference and types.md",
"mode": "100644",
"type": "blob",
"sha": "7b7ccf7080eb557561921a29509f971567f20332",
"size": 2896,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/7b7ccf7080eb557561921a29509f971567f20332"
},
{
"path": "examples",
"mode": "040000",
"type": "tree",
"sha": "0d8899f91273e3d128ac6d62576fc2d386743afd",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/0d8899f91273e3d128ac6d62576fc2d386743afd"
},
{
"path": "examples/MyComponent.jsx",
"mode": "100644",
"type": "blob",
"sha": "69af016f6fdab442f10e777b109ecf5741d69308",
"size": 1888,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/69af016f6fdab442f10e777b109ecf5741d69308"
},
{
"path": "examples/avocado.glb",
"mode": "100644",
"type": "blob",
"sha": "38a4c4ae23cfab3437e2e813877dba1c8b535c6d",
"size": 1030684,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/38a4c4ae23cfab3437e2e813877dba1c8b535c6d"
},
{
"path": "examples/drawing.excalidraw",
"mode": "100644",
"type": "blob",
"sha": "4b08a6d4bb4307e69d6437ad85a84c2146e47490",
"size": 9155,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/4b08a6d4bb4307e69d6437ad85a84c2146e47490"
},
{
"path": "examples/framer motion docs",
"mode": "040000",
"type": "tree",
"sha": "f25c35381254defb132f99eb14301d77f1b25429",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/f25c35381254defb132f99eb14301d77f1b25429"
},
{
"path": "examples/framer motion docs/example1.js",
"mode": "100644",
"type": "blob",
"sha": "b5d45303288f0c8e4ce0b5aa89de3b9a5bb9cb9a",
"size": 715,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/b5d45303288f0c8e4ce0b5aa89de3b9a5bb9cb9a"
},
{
"path": "examples/framer motion docs/example2.js",
"mode": "100644",
"type": "blob",
"sha": "43a14bbca1be3a0ac9802ef35fcf31afcea708cc",
"size": 484,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/43a14bbca1be3a0ac9802ef35fcf31afcea708cc"
},
{
"path": "examples/framer motion docs/example3.js",
"mode": "100644",
"type": "blob",
"sha": "4fb2324b2d72497c53b5d5d27d2f5b9ce8c3a908",
"size": 529,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/4fb2324b2d72497c53b5d5d27d2f5b9ce8c3a908"
},
{
"path": "examples/framer motion docs/example4.js",
"mode": "100644",
"type": "blob",
"sha": "7e1445083ecc6d44c42e5f839fc47af15e61c07a",
"size": 599,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/7e1445083ecc6d44c42e5f839fc47af15e61c07a"
},
{
"path": "examples/framer motion docs/example5.js",
"mode": "100644",
"type": "blob",
"sha": "40d9e859f0f28829d4d515397ac2b32f508f3d2a",
"size": 951,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/40d9e859f0f28829d4d515397ac2b32f508f3d2a"
},
{
"path": "examples/framer motion docs/index.md",
"mode": "100644",
"type": "blob",
"sha": "9592eabde457e22327f226fefffe5239d0a039da",
"size": 2486,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/9592eabde457e22327f226fefffe5239d0a039da"
},
{
"path": "examples/lodash docs",
"mode": "040000",
"type": "tree",
"sha": "56c320adc1d21510aa94445a65417944938195b4",
"url": "https://api.github.com/repos/githubnext/blocks/git/trees/56c320adc1d21510aa94445a65417944938195b4"
},
{
"path": "examples/lodash docs/example1.js",
"mode": "100644",
"type": "blob",
"sha": "859b003b7a1f265951fab0144ae84e0869757461",
"size": 90,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/859b003b7a1f265951fab0144ae84e0869757461"
},
{
"path": "examples/lodash docs/example2.js",
"mode": "100644",
"type": "blob",
"sha": "828184704f1e5b012716dce57c45ad87a91d09f0",
"size": 96,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/828184704f1e5b012716dce57c45ad87a91d09f0"
},
{
"path": "examples/lodash docs/example3.js",
"mode": "100644",
"type": "blob",
"sha": "a4ffa13e8b46b79e4548a05aa2751e26ef479cec",
"size": 104,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/a4ffa13e8b46b79e4548a05aa2751e26ef479cec"
},
{
"path": "examples/lodash docs/index.md",
"mode": "100644",
"type": "blob",
"sha": "76309b7b4732b677d1922e435f9113c0df52c15e",
"size": 958,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/76309b7b4732b677d1922e435f9113c0df52c15e"
},
{
"path": "examples/machinelearning.json",
"mode": "100644",
"type": "blob",
"sha": "90c5917f28bc2b20f89eda73aba0c9f42704a48e",
"size": 203,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/90c5917f28bc2b20f89eda73aba0c9f42704a48e"
},
{
"path": "examples/p5-sketch.js",
"mode": "100644",
"type": "blob",
"sha": "b999d6a8477de769dfa0295cd4da24b84ae0394f",
"size": 354,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/b999d6a8477de769dfa0295cd4da24b84ae0394f"
},
{
"path": "examples/package.json",
"mode": "100644",
"type": "blob",
"sha": "0cfddc5a23e924e52ab4039378a32e54afe0132f",
"size": 7815,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/0cfddc5a23e924e52ab4039378a32e54afe0132f"
},
{
"path": "examples/pets.mermaid",
"mode": "100644",
"type": "blob",
"sha": "ecf6f95dd87547b2434481d44ee6a4393589f30d",
"size": 83,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/ecf6f95dd87547b2434481d44ee6a4393589f30d"
},
{
"path": "examples/styleguide.css",
"mode": "100644",
"type": "blob",
"sha": "4744514ffe53eb1628c6a5584c8c855af6e30c87",
"size": 2433,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/4744514ffe53eb1628c6a5584c8c855af6e30c87"
},
{
"path": "examples/weather.csv",
"mode": "100644",
"type": "blob",
"sha": "c2584293f508f7bedb246e3411a46ebeaba9b674",
"size": 114079,
"url": "https://api.github.com/repos/githubnext/blocks/git/blobs/c2584293f508f7bedb246e3411a46ebeaba9b674"
}
],
"truncated": false
}
48 changes: 48 additions & 0 deletions test/tree.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, it } from "vitest";
import stubTree from "./githubnext-blocks-stub.json";
import { buildTree } from "../utils/lib/tree";

describe("buildTree", () => {
it("converts the flattened tree into a nested data structure", async () => {
const tree = buildTree(stubTree.tree);

let rootFiles = tree.getDirectoryFiles("");
expect(rootFiles.length).toBe(1);
expect(rootFiles[0].path).toBe("README.md");

let baseDotGithubFiles = tree.getDirectoryFiles(".github");
expect(baseDotGithubFiles.length).toBe(0);

let nestedDotGithubFiles = tree.getDirectoryFiles(".github/blocks");
expect(nestedDotGithubFiles.length).toBe(1);
expect(nestedDotGithubFiles[0].path).toBe("all.json");
expect(nestedDotGithubFiles[0].meta.type).toBe("blob");

let recursiveDotGithubFiles = tree.getDirectoryFiles(".github", {
recursive: true,
});

expect(recursiveDotGithubFiles.length).toBe(4);
expect(recursiveDotGithubFiles[0].meta.path).toBe(
".github/blocks/all.json"
);
expect(recursiveDotGithubFiles[1].meta.path).toBe(
".github/blocks/file/githubnext__blocks-examples__chart-block/examples%2Fweather.csv.json"
);
expect(recursiveDotGithubFiles[2].meta.path).toBe(
".github/blocks/file/githubnext__blocks-examples__chart/examples%2Fweather.csv.json"
);
expect(recursiveDotGithubFiles[3].meta.path).toBe(
".github/blocks/file/githubnext__blocks-examples__react-feedback-block/examples%2FMyComponent.jsx.json"
);

let examplesDir = tree.getDirectoryFiles("examples");
expect(examplesDir.length).toBe(9);

let framerExamples = tree.getDirectoryFiles("examples/framer motion docs");
expect(framerExamples.length).toBe(6);
expect(framerExamples[0].meta.path).toBe(
"examples/framer motion docs/example1.js"
);
});
});
1 change: 1 addition & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./types";
export * from "./lib";
export * from "./lib/tree";
export * from "./components/block-picker";
Loading