Skip to content

Commit 99b87b1

Browse files
committed
feat: add test and eslint
1 parent d34d625 commit 99b87b1

File tree

11 files changed

+118
-48
lines changed

11 files changed

+118
-48
lines changed

examples/helia-remix/.eslintrc.cjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = {
2222
ignorePatterns: ["!**/.server", "!**/.client"],
2323

2424
// Base config
25-
extends: ["eslint:recommended"],
25+
extends: ["eslint:recommended", "ipfs"],
2626

2727
overrides: [
2828
// React
@@ -70,6 +70,7 @@ module.exports = {
7070
"plugin:@typescript-eslint/recommended",
7171
"plugin:import/recommended",
7272
"plugin:import/typescript",
73+
"ipfs"
7374
],
7475
},
7576

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const createHelia = jest.fn().mockResolvedValue({
2+
libp2p: {
3+
peerId: { toString: () => 'mock-peer-id' },
4+
status: 'started'
5+
}
6+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { initHelia } from "../components/helia";
2+
3+
describe('initialize Helia', () => {
4+
it('checks if HeliaNode has started', async () => {
5+
6+
const { heliaNode } = await initHelia();
7+
8+
expect(heliaNode.libp2p.status).toEqual('started');
9+
})
10+
11+
it('checks if there is a nodeId', async () => {
12+
13+
const { nodeId } = await initHelia();
14+
15+
expect(nodeId).toEqual('mock-peer-id');
16+
})
17+
18+
it('checks if nodeId is online', async () => {
19+
20+
const { nodeIsOnline } = await initHelia();
21+
22+
expect(nodeIsOnline).toBe(true);
23+
})
24+
})

examples/helia-remix/app/entry.server.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,4 @@ function handleBrowserRequest(
137137

138138
setTimeout(abort, ABORT_DELAY);
139139
});
140-
}
140+
}

examples/helia-remix/app/routes/_index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MetaFunction } from "@remix-run/node";
2-
import IpfsComponent from "../../components/ipfs";
2+
import HeliaComponent from "../../components/helia";
33

44
export const meta: MetaFunction = () => {
55
return [
@@ -14,7 +14,7 @@ export default function Index() {
1414
<div className="flex flex-col items-center gap-16">
1515
<header className="flex flex-col items-center gap-9">
1616
<h1 className="leading text-2xl font-bold text-gray-800 dark:text-gray-100">
17-
Welcome to <span className="sr-only">Remix</span>
17+
Welcome to <span className="">Helia Remix</span>
1818
</h1>
1919
<div className="h-[144px] w-[434px]">
2020
<img
@@ -55,7 +55,7 @@ export default function Index() {
5555
</a>
5656

5757

58-
<IpfsComponent />
58+
<HeliaComponent />
5959
</div>
6060

6161

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React, { useState, useEffect } from 'react'
2+
import { createHelia } from 'helia'
3+
4+
export const initHelia = async () => {
5+
6+
const heliaNode = await createHelia()
7+
8+
const nodeId = heliaNode.libp2p.peerId.toString()
9+
const nodeIsOnline = heliaNode.libp2p.status === 'started'
10+
11+
return { heliaNode, nodeId, nodeIsOnline }
12+
}
13+
14+
const HeliaComponent: React.FC = () => {
15+
const [id, setId] = useState<string | null>(null)
16+
//@ts-expect-error : Type 'null' is not assignable to type 'HeliaNode'.
17+
const [helia, setHelia] = useState<HeliaNode | null>(null)
18+
const [isOnline, setIsOnline] = useState<boolean>(false)
19+
20+
useEffect(() => {
21+
if (helia) return
22+
23+
const initHeliaNode = async () => {
24+
const { heliaNode, nodeId, nodeIsOnline } = await initHelia()
25+
setId(nodeId);
26+
setHelia(heliaNode);
27+
setIsOnline(nodeIsOnline);
28+
}
29+
30+
initHeliaNode()
31+
}, [helia])
32+
33+
if (!helia || !id) {
34+
return <h4>Starting Helia...</h4>
35+
}
36+
37+
return (
38+
<div>
39+
<h4 data-test="id">ID: {id}</h4>
40+
<h4 data-test="status">Status: {isOnline ? 'Online' : 'Offline'}</h4>
41+
</div>
42+
)
43+
}
44+
45+
export default HeliaComponent

examples/helia-remix/components/ipfs.tsx

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/helia-remix/jest.setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom';

examples/helia-remix/package.json

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,26 @@
88
"dev": "remix vite:dev",
99
"lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
1010
"start": "remix-serve ./build/server/index.js",
11-
"typecheck": "tsc"
11+
"typecheck": "tsc",
12+
"test": "jest",
13+
"test:watch": "jest --watch"
1214
},
1315
"dependencies": {
1416
"@remix-run/node": "^2.14.0",
1517
"@remix-run/react": "^2.14.0",
1618
"@remix-run/serve": "^2.14.0",
19+
"eslint-config-ipfs": "^7.0.6",
1720
"helia": "^5.1.1",
1821
"isbot": "^4.1.0",
1922
"react": "^18.2.0",
20-
"react-dom": "^18.2.0"
23+
"react-dom": "^18.2.0",
24+
"vitest": "^3.0.5"
2125
},
2226
"devDependencies": {
2327
"@remix-run/dev": "^2.14.0",
28+
"@testing-library/jest-dom": "^6.6.3",
29+
"@testing-library/react": "^16.2.0",
30+
"@types/jest": "^29.5.14",
2431
"@types/react": "^18.2.20",
2532
"@types/react-dom": "^18.2.7",
2633
"@typescript-eslint/eslint-plugin": "^6.7.4",
@@ -32,13 +39,37 @@
3239
"eslint-plugin-jsx-a11y": "^6.7.1",
3340
"eslint-plugin-react": "^7.33.2",
3441
"eslint-plugin-react-hooks": "^4.6.0",
42+
"identity-obj-proxy": "^3.0.0",
43+
"jest": "^29.7.0",
44+
"jest-environment-jsdom": "^29.7.0",
3545
"postcss": "^8.4.38",
3646
"tailwindcss": "^3.4.4",
47+
"ts-jest": "^29.2.5",
3748
"typescript": "^5.1.6",
3849
"vite": "^5.1.0",
3950
"vite-tsconfig-paths": "^4.2.1"
4051
},
4152
"engines": {
4253
"node": ">=20.0.0"
54+
},
55+
"jest": {
56+
"testEnvironment": "jsdom",
57+
"transform": {
58+
"^.+\\.(ts|tsx)$": "ts-jest"
59+
},
60+
"moduleNameMapper": {
61+
"\\.(css|less|scss|sass)$": "identity-obj-proxy",
62+
"^helia$": "<rootDir>/node_modules/helia"
63+
},
64+
"setupFilesAfterEnv": [
65+
"<rootDir>/jest.setup.ts"
66+
],
67+
"moduleDirectories": [
68+
"node_modules"
69+
],
70+
"testMatch": [
71+
"**/__tests__/**/*.[jt]s?(x)",
72+
"**/?(*.)+(spec|test).[jt]s?(x)"
73+
]
4374
}
4475
}
173 KB
Binary file not shown.

examples/helia-remix/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
"**/.server/**/*.ts",
66
"**/.server/**/*.tsx",
77
"**/.client/**/*.ts",
8-
"**/.client/**/*.tsx"
8+
"**/.client/**/*.tsx",
9+
"**/__tests__/**/*"
910
],
1011
"compilerOptions": {
1112
"lib": ["DOM", "DOM.Iterable", "ES2022"],
12-
"types": ["@remix-run/node", "vite/client"],
13+
"types": ["@remix-run/node", "vite/client", "jest", "@testing-library/jest-dom", "node"],
1314
"isolatedModules": true,
1415
"esModuleInterop": true,
1516
"jsx": "react-jsx",

0 commit comments

Comments
 (0)