lithent 0.36.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -1
- package/dist/hook/mountCallback.d.ts +3 -1
- package/dist/hook/ref.d.ts +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/lithent.mjs +67 -44
- package/dist/lithent.mjs.map +1 -1
- package/dist/lithent.umd.js +1 -1
- package/dist/lithent.umd.js.map +1 -1
- package/dist/types/index.d.ts +3 -2
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/predicator.d.ts +2 -1
- package/dist/utils/universalRef.d.ts +3 -9
- package/dist/wDom.d.ts +0 -1
- package/examples/dist/assets/index.2b484d3b.js +2 -0
- package/examples/dist/assets/index.2b484d3b.js.map +1 -0
- package/examples/dist/index.html +1 -1
- package/package.json +11 -5
- package/src/diff.ts +5 -12
- package/src/hook/mountCallback.ts +11 -4
- package/src/hook/ref.ts +1 -0
- package/src/hook/updateCallback.ts +1 -9
- package/src/index.ts +11 -2
- package/src/render.ts +48 -31
- package/src/{examples → tests}/callback.tsx +0 -1
- package/src/tests/example1.tsx +75 -0
- package/src/tests/example10.tsx +90 -0
- package/src/tests/example11.tsx +82 -0
- package/src/tests/example12.tsx +76 -0
- package/src/tests/example13.tsx +59 -0
- package/src/tests/example14.tsx +115 -0
- package/src/tests/example15.tsx +68 -0
- package/src/tests/example16.tsx +49 -0
- package/src/tests/example17.tsx +50 -0
- package/src/tests/example2.tsx +104 -0
- package/src/tests/example3.tsx +96 -0
- package/src/tests/example4.tsx +138 -0
- package/src/tests/example5.tsx +63 -0
- package/src/tests/example6.tsx +54 -0
- package/src/tests/example7.tsx +21 -0
- package/src/tests/example8.tsx +68 -0
- package/src/tests/example9.tsx +46 -0
- package/src/tests/renew.tsx +56 -0
- package/src/tests/svg.tsx +44 -0
- package/src/types/index.ts +12 -13
- package/src/utils/index.ts +3 -0
- package/src/utils/predicator.ts +6 -2
- package/src/utils/universalRef.ts +15 -9
- package/src/wDom.ts +5 -7
- package/examples/dist/assets/index.f15c0062.js +0 -2
- package/examples/dist/assets/index.f15c0062.js.map +0 -1
- package/src/examples/renew.tsx +0 -29
- /package/src/{examples → tests}/insert.tsx +0 -0
- /package/src/{examples → tests}/loop.tsx +0 -0
- /package/src/{examples → tests}/nested.tsx +0 -0
- /package/src/{examples → tests}/selectbox.tsx +0 -0
- /package/src/{examples → tests}/textarea.tsx +0 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { h, Fragment, render, mount, ref, nextTick } from '@/index';
|
|
2
|
+
const testChangeFiveRef = ref<null | (() => void)>(null);
|
|
3
|
+
const testChangeSixRef = ref<null | (() => void)>(null);
|
|
4
|
+
|
|
5
|
+
const Root = mount(function (r) {
|
|
6
|
+
let showFive = true;
|
|
7
|
+
let showSix = true;
|
|
8
|
+
|
|
9
|
+
const toggleFive = () => {
|
|
10
|
+
showFive = !showFive;
|
|
11
|
+
r();
|
|
12
|
+
};
|
|
13
|
+
const toggleSix = () => {
|
|
14
|
+
showSix = !showSix;
|
|
15
|
+
r();
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
testChangeFiveRef.value = () => {
|
|
19
|
+
showFive = !showFive;
|
|
20
|
+
r();
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
testChangeSixRef.value = () => {
|
|
24
|
+
showSix = !showSix;
|
|
25
|
+
r();
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return () => (
|
|
29
|
+
<Fragment>
|
|
30
|
+
<li>
|
|
31
|
+
4 <button onClick={toggleFive}>toggleFive</button>
|
|
32
|
+
<button onClick={toggleSix}>toggleSix</button>
|
|
33
|
+
</li>
|
|
34
|
+
{showFive ? <li>5</li> : null}
|
|
35
|
+
{showSix ? <li>6</li> : null}
|
|
36
|
+
<li>7</li>
|
|
37
|
+
</Fragment>
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
document.body.innerHTML = `<ul id="root"> <li>1</li> <li>2</li> <li>3</li> <li id="next">8</li> <li>9</li> </ul>`;
|
|
42
|
+
|
|
43
|
+
const testWrap = document.getElementById('root') as HTMLElement;
|
|
44
|
+
|
|
45
|
+
render(<Root />, testWrap, document.getElementById('next'));
|
|
46
|
+
|
|
47
|
+
if (import.meta.vitest) {
|
|
48
|
+
const { it, expect } = import.meta.vitest;
|
|
49
|
+
it("Test that 'Lithent' can correctly handle virtual DOM elements when real and virtual DOM elements are mixed under one parent.", () => {
|
|
50
|
+
expect(testWrap.outerHTML).toBe(
|
|
51
|
+
'<ul id="root"> <li>1</li> <li>2</li> <li>3</li> <li>4 <button>toggleFive</button><button>toggleSix</button></li><li>5</li><li>6</li><li>7</li><li id="next">8</li> <li>9</li> </ul>'
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('The toggle button should remove only the 5.', () => {
|
|
56
|
+
if (testChangeFiveRef.value && testChangeSixRef.value) {
|
|
57
|
+
testChangeFiveRef.value();
|
|
58
|
+
}
|
|
59
|
+
nextTick().then(() => {
|
|
60
|
+
expect(testWrap.outerHTML).toBe(
|
|
61
|
+
'<ul id="root"> <li>1</li> <li>2</li> <li>3</li> <li>4 <button>toggleFive</button><button>toggleSix</button></li><li>6</li><li>7</li><li id="next">8</li> <li>9</li> </ul>'
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('The toggle button should remove only the 6.', () => {
|
|
67
|
+
if (testChangeFiveRef.value && testChangeSixRef.value) {
|
|
68
|
+
testChangeSixRef.value();
|
|
69
|
+
}
|
|
70
|
+
nextTick().then(() => {
|
|
71
|
+
expect(testWrap.outerHTML).toBe(
|
|
72
|
+
'<ul id="root"> <li>1</li> <li>2</li> <li>3</li> <li>4 <button>toggleFive</button><button>toggleSix</button></li><li>7</li><li id="next">8</li> <li>9</li> </ul>'
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { h, Fragment, render, mount, ref, nextTick } from '@/index';
|
|
2
|
+
const testChangeRef = ref<null | (() => void)>(null);
|
|
3
|
+
|
|
4
|
+
const Loop = mount(function (renew) {
|
|
5
|
+
let list: { key: number; value: string }[] = [
|
|
6
|
+
{ key: 4, value: '4' },
|
|
7
|
+
{ key: 5, value: '5' },
|
|
8
|
+
{ key: 6, value: '6' },
|
|
9
|
+
{ key: 7, value: '7' },
|
|
10
|
+
];
|
|
11
|
+
const handle = () => {
|
|
12
|
+
list = [
|
|
13
|
+
{ key: 7, value: 'seven' },
|
|
14
|
+
{ key: 6, value: 'six' },
|
|
15
|
+
{ key: 5, value: 'five' },
|
|
16
|
+
{ key: 4, value: 'four' },
|
|
17
|
+
];
|
|
18
|
+
renew();
|
|
19
|
+
};
|
|
20
|
+
testChangeRef.value = () => {
|
|
21
|
+
handle();
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return () => (
|
|
25
|
+
<Fragment>
|
|
26
|
+
<button onClick={handle}>handle</button>
|
|
27
|
+
{list.map(item => (
|
|
28
|
+
<div key={item.key}>{item.value}</div>
|
|
29
|
+
))}
|
|
30
|
+
</Fragment>
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
document.body.innerHTML =
|
|
35
|
+
'<ul id="list-root"> <li>1</li><li>2</li><li>3</li><li id="nextTarget">8</li><li>9</li> </ul>';
|
|
36
|
+
|
|
37
|
+
const testWrap = document.getElementById('list-root') as HTMLElement;
|
|
38
|
+
|
|
39
|
+
render(<Loop />, testWrap, document.getElementById('nextTarget'));
|
|
40
|
+
|
|
41
|
+
if (import.meta.vitest) {
|
|
42
|
+
const { it, expect } = import.meta.vitest;
|
|
43
|
+
it("Tests if 'Lithent' correctly handles changes to the virtual DOM element when there is a loop element mixed between the real and virtual DOM elements.", () => {
|
|
44
|
+
expect(testWrap.outerHTML).toBe(
|
|
45
|
+
'<ul id="list-root"> <li>1</li><li>2</li><li>3</li><button>handle</button><div>4</div><div>5</div><div>6</div><div>7</div><li id="nextTarget">8</li><li>9</li> </ul>'
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('It should only update the virtualDOM elements well, without changing the existing Real DOM elements.', () => {
|
|
50
|
+
if (testChangeRef.value) {
|
|
51
|
+
testChangeRef.value();
|
|
52
|
+
}
|
|
53
|
+
nextTick().then(() => {
|
|
54
|
+
expect(testWrap.outerHTML).toBe(
|
|
55
|
+
'<ul id="list-root"> <li>1</li><li>2</li><li>3</li><button>handle</button><div>seven</div><div>six</div><div>five</div><div>four</div><li id="nextTarget">8</li><li>9</li> </ul>'
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {
|
|
2
|
+
h,
|
|
3
|
+
Fragment,
|
|
4
|
+
render,
|
|
5
|
+
mount,
|
|
6
|
+
ref,
|
|
7
|
+
nextTick,
|
|
8
|
+
mountCallback,
|
|
9
|
+
} from '@/index';
|
|
10
|
+
|
|
11
|
+
let mountCount1 = 0;
|
|
12
|
+
let mountCount2 = 0;
|
|
13
|
+
let unmountCount1 = 0;
|
|
14
|
+
let unmountCount2 = 0;
|
|
15
|
+
|
|
16
|
+
const testChangeRef = ref<null | (() => void)>(null);
|
|
17
|
+
|
|
18
|
+
const Depth2 = mount(() => {
|
|
19
|
+
mountCallback(() => {
|
|
20
|
+
console.log('mount2');
|
|
21
|
+
mountCount2 += 1;
|
|
22
|
+
return () => {
|
|
23
|
+
console.log('unmount2');
|
|
24
|
+
unmountCount2 += 1;
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return () => (
|
|
29
|
+
<Fragment>
|
|
30
|
+
<span>depth2</span>
|
|
31
|
+
</Fragment>
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const Depth1 = mount(() => {
|
|
36
|
+
mountCallback(() => {
|
|
37
|
+
console.log('mount1');
|
|
38
|
+
mountCount1 += 1;
|
|
39
|
+
return () => {
|
|
40
|
+
console.log('unmount1');
|
|
41
|
+
unmountCount1 += 1;
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return () => (
|
|
46
|
+
<Fragment>
|
|
47
|
+
<span>depth1</span>
|
|
48
|
+
<Depth2 />
|
|
49
|
+
</Fragment>
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const CallbackRoot = mount(renew => {
|
|
54
|
+
let isShow = true;
|
|
55
|
+
let logEl = ref(null);
|
|
56
|
+
|
|
57
|
+
const toggle = () => {
|
|
58
|
+
isShow = !isShow;
|
|
59
|
+
renew();
|
|
60
|
+
};
|
|
61
|
+
testChangeRef.value = () => {
|
|
62
|
+
toggle();
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return () => (
|
|
66
|
+
<Fragment>
|
|
67
|
+
<button onClick={toggle}>TOGGLE</button>
|
|
68
|
+
{isShow ? <Depth1 logEl={logEl} /> : null}
|
|
69
|
+
</Fragment>
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const testWrap =
|
|
74
|
+
document.getElementById('root') || document.createElement('div');
|
|
75
|
+
|
|
76
|
+
render(<CallbackRoot />, testWrap);
|
|
77
|
+
|
|
78
|
+
if (import.meta.vitest) {
|
|
79
|
+
const { it, expect } = import.meta.vitest;
|
|
80
|
+
it('Test that the unmount callback works with nested components.', () => {
|
|
81
|
+
expect(testWrap.outerHTML).toBe(
|
|
82
|
+
'<div><button>TOGGLE</button><span>depth1</span><span>depth2</span></div>'
|
|
83
|
+
);
|
|
84
|
+
expect(mountCount1).toBe(1);
|
|
85
|
+
expect(mountCount2).toBe(1);
|
|
86
|
+
expect(unmountCount1).toBe(0);
|
|
87
|
+
expect(unmountCount2).toBe(0);
|
|
88
|
+
});
|
|
89
|
+
it('Unmount should work fine.', () => {
|
|
90
|
+
if (testChangeRef.value) {
|
|
91
|
+
testChangeRef.value();
|
|
92
|
+
}
|
|
93
|
+
nextTick().then(() => {
|
|
94
|
+
expect(testWrap.outerHTML).toBe('<div><button>TOGGLE</button></div>');
|
|
95
|
+
expect(mountCount1).toBe(1);
|
|
96
|
+
expect(mountCount2).toBe(1);
|
|
97
|
+
expect(unmountCount1).toBe(1);
|
|
98
|
+
expect(unmountCount2).toBe(1);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
it('mount should work fine.', () => {
|
|
102
|
+
if (testChangeRef.value) {
|
|
103
|
+
testChangeRef.value();
|
|
104
|
+
}
|
|
105
|
+
nextTick().then(() => {
|
|
106
|
+
expect(testWrap.outerHTML).toBe(
|
|
107
|
+
'<div><button>TOGGLE</button><span>depth1</span><span>depth2</span></div>'
|
|
108
|
+
);
|
|
109
|
+
expect(mountCount1).toBe(2);
|
|
110
|
+
expect(mountCount2).toBe(2);
|
|
111
|
+
expect(unmountCount1).toBe(1);
|
|
112
|
+
expect(unmountCount2).toBe(1);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { h, Fragment, render, mount, ref, nextTick } from '@/index';
|
|
2
|
+
|
|
3
|
+
const testChangeRef = ref<null | (() => void)>(null);
|
|
4
|
+
|
|
5
|
+
const Depth2 = mount<{ count: number }>(() => ({ count }) => (
|
|
6
|
+
<Fragment>
|
|
7
|
+
<span>depth2: {count}</span>
|
|
8
|
+
</Fragment>
|
|
9
|
+
));
|
|
10
|
+
|
|
11
|
+
const Depth1 = mount<{ count: number }>(() => ({ count }) => (
|
|
12
|
+
<Fragment>
|
|
13
|
+
<span>depth1: {count}</span> <Depth2 count={count} />
|
|
14
|
+
</Fragment>
|
|
15
|
+
));
|
|
16
|
+
|
|
17
|
+
const Root = mount(renew => {
|
|
18
|
+
let count = 0;
|
|
19
|
+
|
|
20
|
+
const toggle = () => {
|
|
21
|
+
count += 1;
|
|
22
|
+
renew();
|
|
23
|
+
};
|
|
24
|
+
testChangeRef.value = () => {
|
|
25
|
+
toggle();
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return () => (
|
|
29
|
+
<Fragment>
|
|
30
|
+
<button onClick={toggle}>increase</button>
|
|
31
|
+
<Depth1 count={count} />
|
|
32
|
+
</Fragment>
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const testWrap =
|
|
37
|
+
document.getElementById('root') || document.createElement('div');
|
|
38
|
+
|
|
39
|
+
render(<Root />, testWrap);
|
|
40
|
+
|
|
41
|
+
if (import.meta.vitest) {
|
|
42
|
+
const { it, expect } = import.meta.vitest;
|
|
43
|
+
it('Test that props are passed well to nested components.', () => {
|
|
44
|
+
expect(testWrap.outerHTML).toBe(
|
|
45
|
+
'<div><button>increase</button><span>depth1: 0</span> <span>depth2: 0</span></div>'
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
it('If we receive a prop from a parent whose value has been incremented by 1, they will all be incremented by 1.', () => {
|
|
49
|
+
if (testChangeRef.value) {
|
|
50
|
+
testChangeRef.value();
|
|
51
|
+
}
|
|
52
|
+
nextTick().then(() => {
|
|
53
|
+
expect(testWrap.outerHTML).toBe(
|
|
54
|
+
'<div><button>increase</button><span>depth1: 1</span> <span>depth2: 1</span></div>'
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
it('Once again, if we are passed a prop whose value has been incremented by 1 from the parent, they will all be incremented by 1.', () => {
|
|
59
|
+
if (testChangeRef.value) {
|
|
60
|
+
testChangeRef.value();
|
|
61
|
+
}
|
|
62
|
+
nextTick().then(() => {
|
|
63
|
+
expect(testWrap.outerHTML).toBe(
|
|
64
|
+
'<div><button>increase</button><span>depth1: 2</span> <span>depth2: 2</span></div>'
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { h, Fragment, render, mount, nextTick } from '@/index';
|
|
2
|
+
|
|
3
|
+
const Loop = mount(function () {
|
|
4
|
+
const list: { key: number; value: string }[] = [
|
|
5
|
+
{ key: 4, value: '4' },
|
|
6
|
+
{ key: 5, value: '5' },
|
|
7
|
+
{ key: 6, value: '6' },
|
|
8
|
+
{ key: 7, value: '7' },
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
return () => (
|
|
12
|
+
<Fragment>
|
|
13
|
+
{list.map(item => (
|
|
14
|
+
<div key={item.key} class="text-orange-300">
|
|
15
|
+
{item.value}
|
|
16
|
+
</div>
|
|
17
|
+
))}
|
|
18
|
+
</Fragment>
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
document.body.innerHTML =
|
|
23
|
+
'<ul id="root" > <li>1</li> <li>2</li> <li>3</li> <li id="next" id="nextTarget"> 8 </li> <li>9</li> </ul>';
|
|
24
|
+
|
|
25
|
+
const testWrap = document.getElementById('root') as HTMLElement;
|
|
26
|
+
|
|
27
|
+
const destroy = render(
|
|
28
|
+
<Loop />,
|
|
29
|
+
testWrap,
|
|
30
|
+
document.getElementById('nextTarget')
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
if (import.meta.vitest) {
|
|
34
|
+
const { it, expect } = import.meta.vitest;
|
|
35
|
+
it("The 'destroy function' also works well when the loop type is stuck in the middle of the actual DOM.", () => {
|
|
36
|
+
expect(testWrap.outerHTML).toBe(
|
|
37
|
+
'<ul id="root"> <li>1</li> <li>2</li> <li>3</li> <li id="next"> 8 </li> <li>9</li> <div class="text-orange-300">4</div><div class="text-orange-300">5</div><div class="text-orange-300">6</div><div class="text-orange-300">7</div></ul>'
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('The destroy function should work fine.', () => {
|
|
42
|
+
destroy();
|
|
43
|
+
nextTick().then(() => {
|
|
44
|
+
expect(testWrap.outerHTML).toBe(
|
|
45
|
+
'<ul id="root"> <li>1</li> <li>2</li> <li>3</li> <li id="next"> 8 </li> <li>9</li> </ul>'
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { h, Fragment, render, mount } from '@/index';
|
|
2
|
+
|
|
3
|
+
const Root = mount(() => () => (
|
|
4
|
+
<Fragment>
|
|
5
|
+
<svg
|
|
6
|
+
id="toggleSidebarMobileClose"
|
|
7
|
+
class="w-6 h-6 "
|
|
8
|
+
fill="currentColor"
|
|
9
|
+
viewBox="0 0 20 20"
|
|
10
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
11
|
+
>
|
|
12
|
+
<path
|
|
13
|
+
fill-rule="evenodd"
|
|
14
|
+
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
|
|
15
|
+
clip-rule="evenodd"
|
|
16
|
+
></path>
|
|
17
|
+
</svg>
|
|
18
|
+
<svg
|
|
19
|
+
class="w-6 h-6"
|
|
20
|
+
fill="currentColor"
|
|
21
|
+
viewBox="0 0 20 20"
|
|
22
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
23
|
+
>
|
|
24
|
+
<path
|
|
25
|
+
fill-rule="evenodd"
|
|
26
|
+
d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h6a1 1 0 110 2H4a1 1 0 01-1-1zM3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
|
|
27
|
+
clip-rule="evenodd"
|
|
28
|
+
></path>
|
|
29
|
+
<path
|
|
30
|
+
fill-rule="evenodd"
|
|
31
|
+
d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h6a1 1 0 110 2H4a1 1 0 01-1-1zM3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
|
|
32
|
+
clip-rule="evenodd"
|
|
33
|
+
></path>
|
|
34
|
+
</svg>
|
|
35
|
+
</Fragment>
|
|
36
|
+
));
|
|
37
|
+
|
|
38
|
+
const testWrap =
|
|
39
|
+
document.getElementById('root') || document.createElement('div');
|
|
40
|
+
|
|
41
|
+
render(<Root />, testWrap);
|
|
42
|
+
|
|
43
|
+
if (import.meta.vitest) {
|
|
44
|
+
const { it, expect } = import.meta.vitest;
|
|
45
|
+
it('Is renew working properly?', () => {
|
|
46
|
+
expect(testWrap.outerHTML).toBe(
|
|
47
|
+
'<div><svg id="toggleSidebarMobileClose" class="w-6 h-6 " fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg><svg class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h6a1 1 0 110 2H4a1 1 0 01-1-1zM3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z" clip-rule="evenodd"></path><path fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h6a1 1 0 110 2H4a1 1 0 01-1-1zM3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z" clip-rule="evenodd"></path></svg></div>'
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { h, render, mount, ref, nextTick } from '@/index';
|
|
2
|
+
const testChangeRef = ref<null | (() => void)>(null);
|
|
3
|
+
|
|
4
|
+
const storeGroup = new Map<string | symbol, unknown>();
|
|
5
|
+
const storeRenderList: {
|
|
6
|
+
[key: string | symbol]: (() => boolean)[];
|
|
7
|
+
} = {};
|
|
8
|
+
|
|
9
|
+
export const store = <T extends {}>(value: T) => {
|
|
10
|
+
const storeKey = Symbol();
|
|
11
|
+
storeGroup.set(storeKey, value);
|
|
12
|
+
|
|
13
|
+
return (renew: () => boolean) => {
|
|
14
|
+
storeRenderList[storeKey] ??= [];
|
|
15
|
+
storeRenderList[storeKey].push(renew);
|
|
16
|
+
|
|
17
|
+
return updater<T>(storeKey);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const updater = <T extends { [key: string | symbol]: unknown }>(
|
|
22
|
+
storeKey: string | symbol
|
|
23
|
+
) =>
|
|
24
|
+
new Proxy(storeGroup.get(storeKey) as T, {
|
|
25
|
+
get(target: T, prop: string) {
|
|
26
|
+
return target[prop];
|
|
27
|
+
},
|
|
28
|
+
set(target, prop: keyof T, value) {
|
|
29
|
+
target[prop] = value;
|
|
30
|
+
const renderList = storeRenderList[storeKey];
|
|
31
|
+
const trashCollections: (() => boolean)[] = [];
|
|
32
|
+
|
|
33
|
+
renderList.forEach(renew => {
|
|
34
|
+
if (!renew()) {
|
|
35
|
+
trashCollections.push(renew);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
trashCollections.forEach(deleteTarget =>
|
|
40
|
+
renderList.splice(renderList.indexOf(deleteTarget), 1)
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
return true;
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const assignShardStore = store<{ text: string; count: number }>({
|
|
48
|
+
text: 'sharedText',
|
|
49
|
+
count: 3,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const Component = mount(renew => {
|
|
53
|
+
const shardStore = assignShardStore(renew);
|
|
54
|
+
const changeInput = (event: InputEvent) => {
|
|
55
|
+
shardStore.text = (event.target as HTMLInputElement).value;
|
|
56
|
+
};
|
|
57
|
+
testChangeRef.value = () => {
|
|
58
|
+
shardStore.text = 'newSharedText';
|
|
59
|
+
};
|
|
60
|
+
return () => (
|
|
61
|
+
<textarea
|
|
62
|
+
type="text"
|
|
63
|
+
onInput={changeInput}
|
|
64
|
+
value={shardStore.text}
|
|
65
|
+
style={{ width: '100px', height: '100px' }}
|
|
66
|
+
/>
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
document.body.innerHTML = `<div id="root"><span>1</span><span>2</span><span>3</span></div>`;
|
|
71
|
+
|
|
72
|
+
const testWrap = document.getElementById('root') as HTMLElement;
|
|
73
|
+
|
|
74
|
+
render(
|
|
75
|
+
<Component />,
|
|
76
|
+
testWrap,
|
|
77
|
+
testWrap.querySelector('span:nth-of-type(2)') as HTMLElement
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
render(
|
|
81
|
+
<Component />,
|
|
82
|
+
testWrap,
|
|
83
|
+
testWrap.querySelector('span:nth-of-type(3)') as HTMLElement
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
if (import.meta.vitest) {
|
|
87
|
+
const { it, expect } = import.meta.vitest;
|
|
88
|
+
|
|
89
|
+
it('A DOM should be created with the textarea inserted in the middle, and the initial values you set for the textarea should be set.', () => {
|
|
90
|
+
expect(testWrap.outerHTML).toBe(
|
|
91
|
+
'<div id="root"><span>1</span><textarea type="text" style="width: 100px; height: 100px;"></textarea><span>2</span><textarea type="text" style="width: 100px; height: 100px;"></textarea><span>3</span></div>'
|
|
92
|
+
);
|
|
93
|
+
expect(testWrap?.querySelector('textarea')?.value).toBe('sharedText');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('If you change the value of the store, the value of the textarea should also change.', () => {
|
|
97
|
+
if (testChangeRef.value) {
|
|
98
|
+
testChangeRef.value();
|
|
99
|
+
}
|
|
100
|
+
nextTick().then(() => {
|
|
101
|
+
expect(testWrap?.querySelector('textarea')?.value).toBe('newSharedText');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { h, Fragment, render, mount, ref, nextTick } from '@/index';
|
|
2
|
+
const testChangeRef = ref<null | (() => void)>(null);
|
|
3
|
+
|
|
4
|
+
const storeGroup = new Map<string | symbol, unknown>();
|
|
5
|
+
const storeRenderList: {
|
|
6
|
+
[key: string | symbol]: (() => boolean)[];
|
|
7
|
+
} = {};
|
|
8
|
+
|
|
9
|
+
export const store = <T extends {}>(value: T) => {
|
|
10
|
+
const storeKey = Symbol();
|
|
11
|
+
storeGroup.set(storeKey, value);
|
|
12
|
+
|
|
13
|
+
return (renew: () => boolean) => {
|
|
14
|
+
storeRenderList[storeKey] ??= [];
|
|
15
|
+
storeRenderList[storeKey].push(renew);
|
|
16
|
+
|
|
17
|
+
return updater<T>(storeKey);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const updater = <T extends { [key: string | symbol]: unknown }>(
|
|
22
|
+
storeKey: string | symbol
|
|
23
|
+
) =>
|
|
24
|
+
new Proxy(storeGroup.get(storeKey) as T, {
|
|
25
|
+
get(target: T, prop: string) {
|
|
26
|
+
return target[prop];
|
|
27
|
+
},
|
|
28
|
+
set(target, prop: keyof T, value) {
|
|
29
|
+
target[prop] = value;
|
|
30
|
+
const renderList = storeRenderList[storeKey];
|
|
31
|
+
const trashCollections: (() => boolean)[] = [];
|
|
32
|
+
|
|
33
|
+
renderList.forEach(renew => {
|
|
34
|
+
if (!renew()) {
|
|
35
|
+
trashCollections.push(renew);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
trashCollections.forEach(deleteTarget =>
|
|
40
|
+
renderList.splice(renderList.indexOf(deleteTarget), 1)
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
return true;
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const Component = mount(r => {
|
|
48
|
+
const local = store<{ count1: number; count2: number; count3: number }>({
|
|
49
|
+
count1: 1,
|
|
50
|
+
count2: 1,
|
|
51
|
+
count3: 1,
|
|
52
|
+
})(r);
|
|
53
|
+
|
|
54
|
+
const click = () => {
|
|
55
|
+
local.count1 += 1;
|
|
56
|
+
local.count2 -= 1;
|
|
57
|
+
local.count3 *= 2;
|
|
58
|
+
};
|
|
59
|
+
testChangeRef.value = click;
|
|
60
|
+
return () => (
|
|
61
|
+
<>
|
|
62
|
+
<div>count1: {local.count1}</div>
|
|
63
|
+
<div>count2: {local.count2}</div>
|
|
64
|
+
<div>count3: {local.count3}</div>
|
|
65
|
+
<button type="text" onClick={click}>
|
|
66
|
+
change count
|
|
67
|
+
</button>
|
|
68
|
+
</>
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const testWrap =
|
|
73
|
+
document.getElementById('root') || document.createElement('div');
|
|
74
|
+
|
|
75
|
+
render(<Component />, testWrap);
|
|
76
|
+
|
|
77
|
+
if (import.meta.vitest) {
|
|
78
|
+
const { it, expect } = import.meta.vitest;
|
|
79
|
+
it('A DOM should be created that reflects the initial values of your store.', () => {
|
|
80
|
+
expect(testWrap.outerHTML).toBe(
|
|
81
|
+
'<div><div>count1: 1</div><div>count2: 1</div><div>count3: 1</div><button type="text">change count</button></div>'
|
|
82
|
+
);
|
|
83
|
+
if (testChangeRef.value) {
|
|
84
|
+
testChangeRef.value();
|
|
85
|
+
testChangeRef.value();
|
|
86
|
+
testChangeRef.value();
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
it('You should see a DOM that reflects the changed store values.', () => {
|
|
90
|
+
nextTick().then(() => {
|
|
91
|
+
expect(testWrap.outerHTML).toBe(
|
|
92
|
+
'<div><div>count1: 4</div><div>count2: -2</div><div>count3: 8</div><button type="text">change count</button></div>'
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|