Skip to content

Commit 76521ca

Browse files
committedFeb 3, 2021
fix: added eslint, prettier, husky
1 parent b2ced21 commit 76521ca

23 files changed

+1870
-284
lines changed
 

‎.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
indent_style = space
7+
indent_size = 2
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true

‎.eslintrc

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"plugins": [
3+
"prettier"
4+
],
5+
"parserOptions": {
6+
"project": [
7+
"tsconfig.json"
8+
]
9+
},
10+
"extends": [
11+
"prettier",
12+
"eslint-config-airbnb",
13+
"eslint-config-airbnb-typescript",
14+
"eslint-config-prettier"
15+
],
16+
"rules": {
17+
"prettier/prettier": [
18+
"error"
19+
],
20+
"no-cond-assign": "off",
21+
"no-plusplus": "off",
22+
"no-continue": "off",
23+
"no-multi-assign": "off",
24+
"no-param-reassign": "off",
25+
"import/prefer-default-export": "off",
26+
"react/react-in-jsx-scope": "off",
27+
"react/no-unknown-property": "off",
28+
"@typescript-eslint/indent": "off"
29+
}
30+
}

‎.prettierrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "all",
4+
"singleQuote": true,
5+
"tabWidth": 2,
6+
"printWidth": 120
7+
}

‎.prettierrc.yml

-5
This file was deleted.

‎LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 SpringType
3+
Copyright (c) 2018 - 2021 SpringType
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

‎README.md

+3-15
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ This is a tiny library to augment the SpringType v3 JSX/TSX microframework with
1313
- ✅ Supports JSX/TSX for runtime DOM mutations
1414
- ✅ Ultra-fast: Uses SpringType `ref` instead of DOM CSS selector queries
1515
- ✅ Supports the most important jQuery methods
16-
- ✅ Tiny: `421 bytes` (best, brotli) - `607 bytes` (worst, umd, gz)
16+
- ✅ Tiny: `431 bytes` (best, brotli) - `605 bytes` (worst, umd, gz)
1717
- ✅ Zero dependencies
1818
- ✅ First class TypeScript support
19-
- ✅ Unit Test coverage >90%
19+
- ✅ Unit Test coverage almost 100%
2020
- ✅ TestCafé smoke tests
2121

2222
<h2 align="center">API docs</h2>
@@ -95,7 +95,7 @@ Thank you so much for supporting us financially! 🙏🏻😎🥳👍
9595

9696
<h2 align="center">Maintainers</h2>
9797

98-
SpringType is brought to you by:
98+
`st-query` is brought to you by:
9999

100100
<table>
101101
<tbody>
@@ -106,18 +106,6 @@ SpringType is brought to you by:
106106
</br>
107107
<a href="https://github.com/kyr0">Aron Homberg</a>
108108
</td>
109-
<td align="center">
110-
<img width="150" height="150"
111-
src="https://avatars2.githubusercontent.com/u/17221813?s=150&v=4">
112-
</br>
113-
<a href="https://github.com/jsdevtom">Tom</a>
114-
</td>
115-
<td align="center">
116-
<img width="150" height="150"
117-
src="https://avatars2.githubusercontent.com/u/12079044?s=150&v=4">
118-
</br>
119-
<a href="https://github.com/mansi1">Michael Mannseicher</a>
120-
</td>
121109
</tr>
122110
<tbody>
123111
</table>

‎commitlint.config.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module.exports = {
2+
parserPreset: 'conventional-changelog-conventionalcommits',
3+
rules: {
4+
'body-leading-blank': [1, 'always'],
5+
'body-max-line-length': [2, 'always', 100],
6+
'footer-leading-blank': [1, 'always'],
7+
'footer-max-line-length': [2, 'always', 100],
8+
'header-max-length': [2, 'always', 100],
9+
'scope-case': [2, 'always', 'lower-case'],
10+
'subject-case': [
11+
2,
12+
'never',
13+
['sentence-case', 'start-case', 'pascal-case', 'upper-case'],
14+
],
15+
'subject-empty': [2, 'never'],
16+
'subject-full-stop': [2, 'never', '.'],
17+
'type-case': [2, 'always', 'lower-case'],
18+
'type-empty': [2, 'never'],
19+
'type-enum': [
20+
2,
21+
'always',
22+
[
23+
'feat',
24+
'add',
25+
'cut',
26+
'fix',
27+
'bump',
28+
'build',
29+
'make',
30+
'ci',
31+
'doc',
32+
'test',
33+
'chore',
34+
'refactor',
35+
'style',
36+
'optimize',
37+
'revert',
38+
'perf'
39+
],
40+
],
41+
},
42+
};

‎e2e/smoke/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
"parcel/node-forge": "^0.10.0"
2424
},
2525
"dependencies": {
26-
"springtype": "^3.1.2"
26+
"springtype": "^3.2.1"
2727
}
28-
}
28+
}

‎e2e/smoke/src/index.tsx

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
import { $ } from "../../../dist/index";
2-
import { tsx, render, Ref } from "springtype";
1+
import { tsx, render, Ref } from 'springtype';
2+
import { $ } from '../../../dist/index';
33

44
interface SomeCustomInputProps {
55
name: string;
66
}
77

88
const SomeCustomInput = ({ name }: SomeCustomInputProps) => {
9-
109
const inputRef: Ref = {};
1110

1211
// programmatically added
1312
const onFocus = () => {
14-
console.log('onFocus!');
15-
}
13+
console.log('onFocus!');
14+
};
1615

1716
// implicitly added
1817
const onBlur = () => {
1918
console.log('Value on blur:', $(inputRef.current).val());
2019

21-
$(inputRef.current).val(2)
20+
$(inputRef.current).val('2');
2221

2322
$(inputRef.current).on('focus', onFocus);
24-
}
25-
return <input ref={inputRef} name={name} onBlur={onBlur} />
26-
}
27-
render(<SomeCustomInput name="firstname" />, document.body)
23+
};
24+
return <input ref={inputRef} name={name} onBlur={onBlur} />;
25+
};
26+
render(<SomeCustomInput name="firstname" />);

‎e2e/smoke/yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -5197,10 +5197,10 @@ split-string@^3.0.1, split-string@^3.0.2:
51975197
dependencies:
51985198
extend-shallow "^3.0.0"
51995199

5200-
springtype@^3.1.2:
5201-
version "3.1.2"
5202-
resolved "https://registry.yarnpkg.com/springtype/-/springtype-3.1.2.tgz#cc3b6fb2d72d7828646d801e445f4c5979beca97"
5203-
integrity sha512-IfnCIDifl2jDW1pus36NvUWA8wUVSDOHP5yZPH/vVjw+GIZ/iJQUnlk/fHjT6FXG9fawg21CUj3qNzYqMlnfRg==
5200+
springtype@^3.2.1:
5201+
version "3.2.1"
5202+
resolved "https://registry.yarnpkg.com/springtype/-/springtype-3.2.1.tgz#0e90d59b9039ceb2ac5876a31ad5593033ff76bc"
5203+
integrity sha512-hgMizEimVQ1J5K59qSLqSYekcNDEpafFrJ5dPHTCMTmUF8p7m33isJQ6VFlhrZEaHsNTk1vZRhZXKRMCDrCDMA==
52045204

52055205
sprintf-js@~1.0.2:
52065206
version "1.0.3"

‎package.json

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "st-query",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"publishConfig": {
55
"access": "public"
66
},
7-
"source": "src/$.ts",
7+
"source": "src/index.ts",
88
"main": "dist/index.js",
99
"exports": "dist/index.modern.js",
1010
"module": "dist/index.module.js",
@@ -39,21 +39,48 @@
3939
"test:watch": "jest --verbose --watch --coverage",
4040
"test:ci": "jest --verbose --runInBand",
4141
"prerelease": "yarn build",
42-
"release": "cd dist",
43-
"postrelease": "yarn publish"
42+
"release": "cd dist && yarn publish",
43+
"format:fix": "pretty-quick --verbose --pattern 'src/**/*.ts{,x}'",
44+
"format:fix:staged": "yarn format:fix --staged",
45+
"format:check": "pretty-quick --check --verbose --pattern 'src/**/*.ts{,x}'",
46+
"lint:fix": "eslint --ext ts 'src/**/*.ts{,x}' --fix",
47+
"lint:check": "eslint --ext ts 'src/**/*.ts{,x}'"
4448
},
4549
"author": "Aron Homberg <info@aron-homberg.de>",
4650
"license": "MIT",
51+
"husky": {
52+
"hooks": {
53+
"pre-commit": "yarn format:fix:staged && yarn lint:check",
54+
"pre-push": "yarn audit --level MODERATE && yarn test && yarn e2e",
55+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
56+
}
57+
},
4758
"devDependencies": {
59+
"@commitlint/cli": "^11.0.0",
4860
"@types/jest": "^25.2.1",
4961
"@types/node": "^13.13.2",
62+
"@typescript-eslint/eslint-plugin": "^4.14.2",
63+
"@typescript-eslint/parser": "^4.14.2",
64+
"conventional-changelog-conventionalcommits": "^4.5.0",
65+
"eslint": "^7.19.0",
66+
"eslint-config-airbnb": "18.2.1",
67+
"eslint-config-airbnb-typescript": "^12.0.0",
68+
"eslint-config-prettier": "^7.2.0",
69+
"eslint-plugin-import": "^2.22.1",
70+
"eslint-plugin-jest": "^24.1.3",
71+
"eslint-plugin-jsx-a11y": "^6.4.1",
72+
"eslint-plugin-prettier": "^3.3.1",
73+
"eslint-plugin-react": "^7.22.0",
74+
"eslint-plugin-react-hooks": "^4.2.0",
75+
"husky": "^4.3.8",
5076
"jest": "^26.6.3",
77+
"microbundle": "^0.13.0",
5178
"prettier": "^2.2.1",
52-
"springtype": "^3.1.2",
79+
"pretty-quick": "^3.1.0",
80+
"springtype": "^3.2.1",
5381
"testcafe": "^1.10.1",
5482
"ts-jest": "^26.5.0",
5583
"ts-node": "^9.1.1",
56-
"typescript": "^4.1.3",
57-
"microbundle": "^0.13.0"
84+
"typescript": "^4.1.3"
5885
}
5986
}

‎scripts/e2e-build-all.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
const { exec } = require("child_process");
1+
import { exec } from 'child_process';
22

3-
const e2eProjects = ["smoke"];
3+
const e2eProjects = ['smoke'];
44

5-
const getPackageDir = (dirName: string) => "./e2e/" + dirName;
6-
7-
for (let packageDir of e2eProjects) {
8-
process.chdir(getPackageDir(packageDir));
5+
e2eProjects.forEach((packageDir: string) => {
6+
process.chdir(`./e2e/${packageDir}`);
97

108
console.log(`[i] Installing e2e project ${packageDir}...`);
11-
exec("yarn");
9+
exec('yarn');
1210

1311
console.log(`[i] Building e2e project ${packageDir}...`);
14-
exec("yarn build");
12+
exec('yarn build');
1513

16-
process.chdir("../../");
17-
}
14+
process.chdir('../../');
15+
});

‎src/__test__/$.test.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { $ } from "../$";
1+
import { $ } from '..';
22

3-
describe("$", () => {
4-
it('is defined', () => {
5-
expect($).toBeDefined();
6-
});
3+
describe('$', () => {
4+
it('is defined', () => {
5+
expect($).toBeDefined();
6+
});
77

8-
it('implements all methods', () => {
9-
expect($(document.body).attr).toBeInstanceOf(Function);
10-
expect($(document.body).val).toBeInstanceOf(Function);
11-
expect($(document.body).empty).toBeInstanceOf(Function);
12-
expect($(document.body).remove).toBeInstanceOf(Function);
13-
expect($(document.body).html).toBeInstanceOf(Function);
14-
expect($(document.body).addClass).toBeInstanceOf(Function);
15-
expect($(document.body).hasClass).toBeInstanceOf(Function);
16-
expect($(document.body).removeClass).toBeInstanceOf(Function);
17-
expect($(document.body).toggleClass).toBeInstanceOf(Function);
18-
expect($(document.body).replaceWith).toBeInstanceOf(Function);
19-
expect($(document.body).on).toBeInstanceOf(Function);
20-
expect($(document.body).off).toBeInstanceOf(Function);
21-
});
8+
it('implements all methods', () => {
9+
expect($(document.body).attr).toBeInstanceOf(Function);
10+
expect($(document.body).val).toBeInstanceOf(Function);
11+
expect($(document.body).empty).toBeInstanceOf(Function);
12+
expect($(document.body).remove).toBeInstanceOf(Function);
13+
expect($(document.body).html).toBeInstanceOf(Function);
14+
expect($(document.body).addClass).toBeInstanceOf(Function);
15+
expect($(document.body).hasClass).toBeInstanceOf(Function);
16+
expect($(document.body).removeClass).toBeInstanceOf(Function);
17+
expect($(document.body).toggleClass).toBeInstanceOf(Function);
18+
expect($(document.body).replaceWith).toBeInstanceOf(Function);
19+
expect($(document.body).on).toBeInstanceOf(Function);
20+
expect($(document.body).off).toBeInstanceOf(Function);
21+
});
2222
});

‎src/__test__/css.test.ts

+49-50
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,50 @@
1-
import { $ } from "../$";
2-
3-
describe("CSS class manipulation", () => {
4-
it('can toggle a CSS class', () => {
5-
$(document.body).toggleClass('lol-t')
6-
expect(document.body.querySelector('.lol-t')).toBeDefined();
7-
});
8-
9-
it('can untoggle a CSS class', () => {
10-
$(document.body).toggleClass('lol-ut')
11-
$(document.body).toggleClass('lol-ut')
12-
expect(document.body.querySelector('.lol-ut')).toEqual(null);
13-
});
14-
15-
it('can check for a class', () => {
16-
$(document.body).toggleClass('lol-c')
17-
expect(document.body.querySelector('.lol-c')).toBeDefined();
18-
expect($(document.body).hasClass('lol-c')).toEqual(true);
19-
});
20-
21-
it('can check for a non-existing class', () => {
22-
$(document.body).toggleClass('lol-cn')
23-
$(document.body).toggleClass('lol-cn')
24-
expect($(document.body).hasClass('lol-cn')).toEqual(false);
25-
});
26-
27-
it('can add one class', () => {
28-
$(document.body).addClass('oneClass')
29-
expect($(document.body).hasClass('oneClass')).toEqual(true);
30-
});
31-
32-
it('can add many classes', () => {
33-
$(document.body).addClass(['oneClass1', 'nextClass1'])
34-
expect($(document.body).hasClass('oneClass1')).toEqual(true);
35-
expect($(document.body).hasClass('nextClass1')).toEqual(true);
36-
});
37-
38-
it('can remove one class', () => {
39-
$(document.body).addClass('oneClass2')
40-
$(document.body).removeClass('oneClass2')
41-
expect($(document.body).hasClass('oneClass2')).toEqual(false);
42-
});
43-
44-
it('can remove many classes', () => {
45-
$(document.body).addClass(['oneClass3', 'nextClass3'])
46-
$(document.body).removeClass(['oneClass3', 'nextClass3'])
47-
expect($(document.body).hasClass('oneClass3')).toEqual(false);
48-
expect($(document.body).hasClass('nextClass3')).toEqual(false);
49-
});
50-
1+
import { $ } from '..';
2+
3+
describe('CSS class manipulation', () => {
4+
it('can toggle a CSS class', () => {
5+
$(document.body).toggleClass('lol-t');
6+
expect(document.body.querySelector('.lol-t')).toBeDefined();
7+
});
8+
9+
it('can untoggle a CSS class', () => {
10+
$(document.body).toggleClass('lol-ut');
11+
$(document.body).toggleClass('lol-ut');
12+
expect(document.body.querySelector('.lol-ut')).toEqual(null);
13+
});
14+
15+
it('can check for a class', () => {
16+
$(document.body).toggleClass('lol-c');
17+
expect(document.body.querySelector('.lol-c')).toBeDefined();
18+
expect($(document.body).hasClass('lol-c')).toEqual(true);
19+
});
20+
21+
it('can check for a non-existing class', () => {
22+
$(document.body).toggleClass('lol-cn');
23+
$(document.body).toggleClass('lol-cn');
24+
expect($(document.body).hasClass('lol-cn')).toEqual(false);
25+
});
26+
27+
it('can add one class', () => {
28+
$(document.body).addClass('oneClass');
29+
expect($(document.body).hasClass('oneClass')).toEqual(true);
30+
});
31+
32+
it('can add many classes', () => {
33+
$(document.body).addClass(['oneClass1', 'nextClass1']);
34+
expect($(document.body).hasClass('oneClass1')).toEqual(true);
35+
expect($(document.body).hasClass('nextClass1')).toEqual(true);
36+
});
37+
38+
it('can remove one class', () => {
39+
$(document.body).addClass('oneClass2');
40+
$(document.body).removeClass('oneClass2');
41+
expect($(document.body).hasClass('oneClass2')).toEqual(false);
42+
});
43+
44+
it('can remove many classes', () => {
45+
$(document.body).addClass(['oneClass3', 'nextClass3']);
46+
$(document.body).removeClass(['oneClass3', 'nextClass3']);
47+
expect($(document.body).hasClass('oneClass3')).toEqual(false);
48+
expect($(document.body).hasClass('nextClass3')).toEqual(false);
49+
});
5150
});

‎src/__test__/dom.test.tsx

+63-38
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,64 @@
1-
import { $ } from "../$";
2-
import { IElement, render, tsx } from "springtype";
3-
4-
describe("General DOM manipulation", () => {
5-
6-
it('can update children of a SpringType-created DOM element', () => {
7-
const el = render(<div>Check</div>) as IElement;
8-
$(el).html(<div>Check2</div>);
9-
expect(el.childNodes[0].textContent).toEqual('Check2');
10-
});
11-
12-
it('can empty an element', () => {
13-
document.body.innerHTML = '<div>Test</div>';
14-
$(document.body).empty();
15-
expect(document.body.childNodes[0]).toBeFalsy();
16-
});
17-
18-
it('can set an attribute', () => {
19-
$(document.body).attr('foo', 'bar');
20-
expect(document.body.getAttribute('foo')).toEqual('bar');
21-
});
22-
23-
it('can get an attribute', () => {
24-
$(document.body).attr('foo2', 'bar');
25-
expect($(document.body).attr('foo2')).toEqual('bar');
26-
});
27-
28-
it('can replace an element with another', () => {
29-
const divRef: any = {};
30-
render(<div ref={divRef}>Check</div>, document.body) as IElement;
31-
divRef.current = $(divRef.current).replaceWith(<div tabIndex="2">Check2</div>);
32-
expect($(divRef.current).attr('tabIndex')).toEqual("2");
33-
});
34-
35-
it('remove an element', () => {
36-
$(document.body).remove();
37-
expect(document.body).toBeFalsy();
38-
});
1+
import { IElement, Ref, render, tsx } from 'springtype';
2+
import { $ } from '..';
3+
4+
describe('General DOM manipulation', () => {
5+
it('can update children of a SpringType-created DOM element', () => {
6+
const el = render(<div>Check</div>) as IElement;
7+
$(el).html(<div>Check2</div>);
8+
expect(el.childNodes[0].textContent).toEqual('Check2');
9+
});
10+
11+
it('can empty an element', () => {
12+
document.body.innerHTML = '<div>Test</div>';
13+
$(document.body).empty();
14+
expect(document.body.childNodes[0]).toBeFalsy();
15+
});
16+
17+
it('can set an attribute', () => {
18+
$(document.body).attr('foo', 'bar');
19+
expect(document.body.getAttribute('foo')).toEqual('bar');
20+
});
21+
22+
it('can get an attribute', () => {
23+
$(document.body).attr('foo2', 'bar');
24+
expect($(document.body).attr('foo2')).toEqual('bar');
25+
});
26+
27+
it('can get an input value', () => {
28+
const inputRef: Ref = {};
29+
render(<input ref={inputRef} value="123" />, document.body) as IElement;
30+
expect($(inputRef.current!).val()).toEqual('123');
31+
});
32+
33+
it('can set an input value', () => {
34+
const inputRef: Ref = {};
35+
render(<input ref={inputRef} value="123" />, document.body) as IElement;
36+
$(inputRef.current!).val('345');
37+
expect($(inputRef.current!).val()).toEqual('345');
38+
});
39+
40+
it('can get a checkbox checked value', () => {
41+
const inputRef: Ref = {};
42+
render(<input ref={inputRef} type="checkbox" checked />, document.body) as IElement;
43+
expect($(inputRef.current!).val()).toEqual(true);
44+
});
45+
46+
it('can set a checkbox checked value', () => {
47+
const inputRef: Ref = {};
48+
render(<input ref={inputRef} type="checkbox" />, document.body) as IElement;
49+
$(inputRef.current!).val(true);
50+
expect($(inputRef.current!).val()).toEqual(true);
51+
});
52+
53+
it('can replace an element with another', () => {
54+
const divRef: any = {};
55+
render(<div ref={divRef}>Check</div>, document.body) as IElement;
56+
divRef.current = $(divRef.current).replaceWith(<input tabIndex="-2" />);
57+
expect($(divRef.current).attr('tabIndex')).toEqual('-2');
58+
});
59+
60+
it('remove an element', () => {
61+
$(document.body).remove();
62+
expect(document.body).toBeFalsy();
63+
});
3964
});

‎src/__test__/event.test.tsx

+36-29
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1-
import { $ } from "../$";
2-
import { IElement, render, tsx } from "springtype";
3-
4-
describe("Event handling", () => {
5-
6-
it('can register for an event programmatically', () => {
7-
const elRef: any = {};
8-
render(<button ref={elRef}>Click me</button>) as IElement;
9-
10-
const onClick = jest.fn(() => {});
11-
$(elRef.current).on('click', onClick);
12-
13-
elRef.current.click();
14-
15-
expect(onClick.mock.calls.length).toEqual(1);
16-
});
17-
18-
it('can *un*register for an event programmatically', () => {
19-
const elRef: any = {};
20-
render(<button ref={elRef}>Click me</button>) as IElement;
21-
22-
const onClick = jest.fn(() => {});
23-
$(elRef.current).on('click', onClick);
24-
$(elRef.current).off('click', onClick);
25-
26-
elRef.current.click();
27-
28-
expect(onClick.mock.calls.length).toEqual(0);
29-
});
1+
import { IElement, render, tsx } from 'springtype';
2+
import { $ } from '..';
3+
4+
describe('Event handling', () => {
5+
it('can register for an event programmatically', () => {
6+
const elRef: any = {};
7+
render(
8+
<button type="button" ref={elRef}>
9+
Click me
10+
</button>,
11+
) as IElement;
12+
13+
const onClick = jest.fn(() => {});
14+
$(elRef.current).on('click', onClick);
15+
16+
elRef.current.click();
17+
18+
expect(onClick.mock.calls.length).toEqual(1);
19+
});
20+
21+
it('can *un*register for an event programmatically', () => {
22+
const elRef: any = {};
23+
render(
24+
<button type="button" ref={elRef}>
25+
Click me
26+
</button>,
27+
) as IElement;
28+
29+
const onClick = jest.fn(() => {});
30+
$(elRef.current).on('click', onClick);
31+
$(elRef.current).off('click', onClick);
32+
33+
elRef.current.click();
34+
35+
expect(onClick.mock.calls.length).toEqual(0);
36+
});
3037
});

‎src/css.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
import { API } from "./interface/api";
1+
import { API } from './interface/api';
22

33
export const addSingleClass = (ref: Element, className: string) => {
4-
if (!ref.classList.contains(className)) {
4+
if (!ref.classList.contains(className)) {
55
ref.classList.add(className);
66
}
7-
}
7+
};
88

99
export const addClass = (ref: Element, impl: API) => (className: Array<string> | string) => {
1010
if (className instanceof Array) {
11-
for (let i=0; i<className.length; i++) {
11+
for (let i = 0; i < className.length; i++) {
1212
addSingleClass(ref, className[i]);
1313
}
1414
} else {
1515
addSingleClass(ref, className);
1616
}
1717
return impl;
18-
}
18+
};
1919

2020
export const removeSingleClass = (ref: Element, className: string) => {
21-
if (ref.classList.contains(className)) {
21+
if (ref.classList.contains(className)) {
2222
ref.classList.remove(className);
2323
}
24-
}
24+
};
2525

2626
export const removeClass = (ref: Element, impl: API) => (className: Array<string> | string) => {
2727
if (className instanceof Array) {
28-
for (let i=0; i<className.length; i++) {
28+
for (let i = 0; i < className.length; i++) {
2929
removeSingleClass(ref, className[i]);
3030
}
3131
} else {
3232
removeSingleClass(ref, className);
3333
}
3434
return impl;
35-
}
35+
};
3636

3737
export const toggleClass = (ref: Element, impl: API) => (className: string) => {
3838
ref.classList.toggle(className);
3939
return impl;
40-
}
40+
};
4141

42-
export const hasClass = (ref: Element) => (className: string) => {
43-
return ref.classList.contains(className);
44-
}
42+
export const hasClass = (ref: Element) => (className: string) => ref.classList.contains(className);

‎src/dom.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { API } from "./interface/api";
1+
import { API } from './interface/api';
22

33
const render = (ref: Element, vdom: any) => (ref as any).$st.render(vdom);
44

@@ -9,7 +9,7 @@ export const attr = (ref: Element, impl: API) => (name: string, value?: any) =>
99
};
1010

1111
export const val = (ref: Element, impl: API) => (value?: any) => {
12-
const isCheckbox = (ref as any).type === "checkbox";
12+
const isCheckbox = (ref as any).type === 'checkbox';
1313
if (typeof value === 'undefined') {
1414
return isCheckbox ? (ref as any).checked : (ref as any).value;
1515
}
@@ -24,24 +24,23 @@ export const val = (ref: Element, impl: API) => (value?: any) => {
2424
export const empty = (ref: Element, impl: API) => () => {
2525
ref.innerHTML = '';
2626
return impl;
27-
}
27+
};
2828

2929
export const html = (ref: Element, impl: API) => (vdom: any) => {
3030
empty(ref, impl)();
3131
ref.appendChild(render(ref, vdom));
3232
return impl;
33-
}
33+
};
3434

3535
export const remove = (ref: Element, impl: API) => () => {
3636
if (ref.parentNode) ref.parentNode.removeChild(ref);
3737
return impl;
38-
}
38+
};
3939

40-
export const replaceWith = (ref: Element, impl: API) => (vdom: any) => {
40+
export const replaceWith = (ref: Element) => (vdom: any) => {
41+
const el = render(ref, vdom);
4142
if (ref.parentNode) {
42-
const el = render(ref, vdom);
4343
ref.parentNode.replaceChild(el, ref);
44-
return el;
4544
}
46-
return impl;
47-
}
45+
return el;
46+
};

‎src/events.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { API } from "./interface/api";
1+
import { API } from './interface/api';
22

3-
export const off = (ref: Element|Window, impl: API) => (eventName: string, handler: EventListener) => {
3+
export const off = (ref: Element | Window, impl: API) => (eventName: string, handler: EventListener) => {
44
ref.removeEventListener(eventName, handler);
55
return impl;
6-
}
6+
};
77

8-
export const on = (ref: Element|Window, impl: API) => (eventName: string, handler: EventListener) => {
8+
export const on = (ref: Element | Window, impl: API) => (eventName: string, handler: EventListener) => {
99
ref.addEventListener(eventName, handler);
1010
return impl;
11-
}
11+
};

‎src/$.ts ‎src/index.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { addClass, removeClass, toggleClass, hasClass } from "./css";
2-
import { attr, empty, html, remove, replaceWith, val } from "./dom";
3-
import { on, off } from "./events";
4-
import { API } from "./interface/api";
1+
import { addClass, removeClass, toggleClass, hasClass } from './css';
2+
import { attr, empty, html, remove, replaceWith, val } from './dom';
3+
import { on, off } from './events';
4+
import { API } from './interface/api';
55

66
let impl: API;
77

@@ -12,7 +12,7 @@ export const $ = (ref: Element) => {
1212
empty: empty(ref, impl),
1313
html: html(ref, impl),
1414
remove: remove(ref, impl),
15-
replaceWith: replaceWith(ref, impl),
15+
replaceWith: replaceWith(ref),
1616
addClass: addClass(ref, impl),
1717
removeClass: removeClass(ref, impl),
1818
toggleClass: toggleClass(ref, impl),
@@ -21,4 +21,4 @@ export const $ = (ref: Element) => {
2121
off: off(ref, impl),
2222
};
2323
return impl;
24-
};
24+
};

‎src/interface/api.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ export interface API {
44
empty(): API;
55
html(vdom: any): API;
66
remove(): API;
7-
replaceWith(vdom: any): Element | API;
8-
addClass(className: Array<string> | string): API;
9-
removeClass(className: Array<string> | string): API;
7+
replaceWith(vdom: any): Element;
8+
addClass(className: Array<string> | string): API;
9+
removeClass(className: Array<string> | string): API;
1010
hasClass(className: string): boolean;
1111
toggleClass(className: string): API;
1212
on(eventName: string, handler: EventListener): API;
1313
off(eventName: string, handler: EventListener): API;
14-
}
14+
}

‎tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
"jsxFactory": "tsx"
1717
},
1818
"exclude": ["node_modules", "**/node_modules/*"],
19-
"include": ["src", "e2e", "jest.config.js"]
19+
"include": ["src", "e2e", "scripts", "jest.config.js"]
2020
}

‎yarn.lock

+1,517-55
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.