Skip to content

Commit 35d94e0

Browse files
committed
test(browser): jsdoc, better config build function
1 parent 83f808b commit 35d94e0

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

packages/nuejs/test/README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ This directory contains tests for the Nue.js using [domino](https://github.com/f
77
You can run the tests using Bun's built-in test runner:
88

99
```bash
10-
# From the nuejs package directory
11-
cd nue/packages/nuejs
10+
# Go to nuejs package directory
11+
cd packages/nuejs
1212

13-
# Run all tests
13+
# Run all nuejs tests
1414
bun test
1515

1616
# Or, run a specific test suite
@@ -21,25 +21,28 @@ bun test test/test-clicks
2121

2222
Each test has its own directory (with a name starting with `test-`) with the following files:
2323

24-
1. `component.dhtml` - Contains one or more `.dhtml` Nue.js components
25-
2. `<test-name>.test.js` - The actual test file that imports the components and tests them
26-
3. `index.html` - An HTML file that can be used to render the components in a browser
24+
1. `component.dhtml` Contains one or more `.dhtml` Nue.js components
25+
2. `<test-name>.test.js` The actual test file that imports the components and tests them
26+
3. `index.html` An HTML file that can be used to render the components in a browser
2727

2828
## Adding New Tests
2929

3030
To add a new test:
3131

32-
1. Create a new directory for your test (e.g., `test-my-feature/`)
32+
1. Create a new directory for your test (e.g. `test-my-feature/`)
3333
2. Create a `component.dhtml` file with one or more component definitions
34-
3. Create an `my-feature.test.js` file that imports and tests the components
34+
3. Create a `my-feature.test.js` file that imports and tests the components
3535
4. Create an `index.html` file to render the components in a browser
3636

3737
## Test Utilities
3838

3939
The `test-utils.js` file provides utilities to make testing easier:
4040

41+
- `mkConfigBase(import.meta.url)`: Creates a function to build a config for the current test directory
42+
- Returns: `(componentName, data) => ({ testPath: currentDir, componentName, data })` where data is optional
43+
4144
- `mountTestComponent({ testName, componentName, data })`: Mounts a specific component from a test directory
42-
- `testName`: Name of the test directory
45+
- `testPath`: Path to the test directory
4346
- `componentName`: Name of the component to mount
4447
- `data`: Optional data object to initialize the component with
4548
- Returns: `{ app, cleanup }` where `app` is the mounted component instance and `cleanup` is a function to clean up the test environment

packages/nuejs/test/test-utils.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,39 @@ import { createWindow } from 'domino'
66

77
import { parse } from '../src/compile.js'
88

9-
/* Use like `getDirname(import.meta.url)` */
9+
10+
/**
11+
* Get directory from url
12+
* @param {Url} url - Pass `import.meta.url`
13+
* @returns {string} Directory path containing the current file
14+
*/
1015
export function getDirname(url) {
1116
return dirname(fileURLToPath(url))
1217
}
1318

19+
/**
20+
* Get directory path from url and return function for usage with {@linkcode mountTestComponent}
21+
* @param {Url} url - `import.meta.url` to get the test directory
22+
*/
1423
export function mkConfigBase(url) {
15-
return componentName => ({ testName: getDirname(url), componentName })
24+
/**
25+
* @param {string} componentName
26+
* @param {Object?} data
27+
*/
28+
return (componentName, data) => ({ testPath: getDirname(url), componentName, data })
1629
}
1730

31+
1832
/**
1933
* Mounts a component from a test directory
2034
* @param {Object} options - Mount options
21-
* @param {string} options.testName - The name of the test directory
35+
* @param {string} options.testPath - The path to the test directory
2236
* @param {string} options.componentName - Name of the specific component to mount
2337
* @param {Object} [options.data={}] - Data to initialize the component with
2438
*/
25-
export async function mountTestComponent({ testName, componentName, data = {} }) {
39+
export async function mountTestComponent({ testPath, componentName, data = {} }) {
2640
// Validate inputs
27-
if (!testName || !componentName) throw new Error('Required parameters missing: "testName" and "componentName" must be provided')
41+
if (!testPath || !componentName) throw new Error('Required parameters missing: "testPath" and "componentName" must be provided')
2842

2943
// Setup domino DOM environment
3044
const window = createWindow('<!DOCTYPE html><html><body></body></html>')
@@ -38,7 +52,7 @@ export async function mountTestComponent({ testName, componentName, data = {} })
3852

3953
try {
4054
// Load and parse component
41-
const dhtmlPath = join(testName, 'component.dhtml')
55+
const dhtmlPath = join(testPath, 'component.dhtml')
4256
const source = readFileSync(dhtmlPath, 'utf-8')
4357
const components = parseComponents(source)
4458

@@ -66,6 +80,7 @@ export async function mountTestComponent({ testName, componentName, data = {} })
6680
}
6781
}
6882

83+
6984
/**
7085
* Parses component source and returns an array of component objects
7186
* @param {string} source - The component source code

0 commit comments

Comments
 (0)