Skip to content

Commit d69cf3f

Browse files
author
James Hill
committed
Updated packages and added catch for null root
1 parent b05a899 commit d69cf3f

6 files changed

Lines changed: 1064 additions & 784 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
A super fast and lightweight (**971bytes** gzipped) utility function to build HTML node trees and stateful functional components. Can be used either directly via the `h` function, or from transpiled `JSX`.
1+
A super fast and lightweight (**1kb** gzipped) utility function to build HTML node trees and stateful functional components. Can be used either directly via the `h` function, or from transpiled `JSX`.
22

33
Due to it's size, `hypnode` is a useful companion utility for writing _WebComponents_, simplifying `HTML` structures, element references and state management. See section entitled "WebComponents" for an example.
44

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
},
2929
"homepage": "https://github.com/jhdevuk/hypnode#readme",
3030
"devDependencies": {
31-
"@types/jest": "^25.1.1",
32-
"jest": "^25.1.0",
31+
"@types/jest": "^26.0.10",
32+
"jest": "^26.4.0",
3333
"npm-run-all": "^4.1.5",
34-
"prettier": "^1.19.1",
35-
"ts-jest": "^25.1.0",
34+
"prettier": "^2.0.5",
35+
"ts-jest": "^26.2.0",
3636
"tslint": "^5.20.1",
3737
"tslint-config-prettier": "^1.18.0",
3838
"tslint-eslint-rules": "^5.4.0",
39-
"typescript": "^3.7.5",
39+
"typescript": "^3.9.7",
4040
"typescript-tslint-plugin": "^0.5.5"
4141
}
4242
}

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ function addStyleProperies(
156156

157157
const items = Object.keys(value);
158158

159-
const result = items.reduce((style: string, key, index) => {
160-
const name = key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
159+
const result = items.reduce((style, item) => {
160+
const name = item.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
161161

162-
style += `${name}:${value[key]};`;
162+
style += `${name}:${value[item]};`;
163163

164164
return style;
165165
}, '');
@@ -199,7 +199,11 @@ function addAttributes(element: HTMLElement, key: string, value: string) {
199199
*
200200
* -------------------------------- */
201201

202-
function render(root: HTMLElement, output: HTMLElement) {
202+
function render(root: HTMLElement | null, output: HTMLElement) {
203+
if (!root) {
204+
throw new Error('hypnode -> render(): Missing root element');
205+
}
206+
203207
if (!root.firstElementChild) {
204208
root.appendChild(output);
205209

tests/src/index.spec.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,27 @@ describe('Core:Hypnode', () => {
136136
expect(result.outerHTML).toEqual(sample);
137137
});
138138

139-
it('correctly appends output with "render()" to root element', () => {
140-
const root = mockRoot(false) as any;
141-
const result = h('div', { title: testText }, testText);
139+
describe('render()', () => {
140+
it('correctly appends output with "render()" to root element', () => {
141+
const root = mockRoot(false) as any;
142+
const result = h('div', { title: testText }, testText);
142143

143-
render(root, result);
144+
render(root, result);
144145

145-
expect(mockAppendChild).toBeCalledWith(result);
146-
});
146+
expect(mockAppendChild).toBeCalledWith(result);
147+
});
148+
149+
it('correctly replaces root child with "render()"', () => {
150+
const root = mockRoot(true) as any;
151+
const result = h('div', { title: testText }, testText);
147152

148-
it('correctly replaces root child with "render()"', () => {
149-
const root = mockRoot(true) as any;
150-
const result = h('div', { title: testText }, testText);
153+
render(root, result);
151154

152-
render(root, result);
155+
expect(mockReplaceChild).toBeCalledWith(result, root.firstElementChild);
156+
});
153157

154-
expect(mockReplaceChild).toBeCalledWith(result, root.firstElementChild);
158+
it('throws an error if root is undefined', () => {
159+
expect(() => render(null, h('div'))).toThrowError();
160+
});
155161
});
156162
});

tests/src/useState.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import { h, useState } from '../../src/index';
2323
* -------------------------------- */
2424

2525
describe('Core:useState', () => {
26-
beforeEach(() => jest.clearAllMocks());
26+
jest.useFakeTimers();
27+
28+
afterEach(() => jest.clearAllMocks());
2729

2830
it('correctly renders component from "useState" function', () => {
2931
const sample = `<a>${testValue + 1}</a>`;

0 commit comments

Comments
 (0)