@tempots/dom 5.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.
Files changed (109) hide show
  1. package/.eslintignore +10 -0
  2. package/.eslintrc.cjs +28 -0
  3. package/coverage/clover.xml +39 -0
  4. package/coverage/coverage-final.json +2 -0
  5. package/coverage/lcov-report/OneOf.ts.html +256 -0
  6. package/coverage/lcov-report/base.css +224 -0
  7. package/coverage/lcov-report/block-navigation.js +87 -0
  8. package/coverage/lcov-report/dom-context.ts.html +928 -0
  9. package/coverage/lcov-report/favicon.png +0 -0
  10. package/coverage/lcov-report/index.html +116 -0
  11. package/coverage/lcov-report/prettify.css +1 -0
  12. package/coverage/lcov-report/prettify.js +2 -0
  13. package/coverage/lcov-report/prop.ts.html +691 -0
  14. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  15. package/coverage/lcov-report/sorter.js +196 -0
  16. package/coverage/lcov-report/src/components/Attribute.ts.html +154 -0
  17. package/coverage/lcov-report/src/components/BooleanAttribute.ts.html +154 -0
  18. package/coverage/lcov-report/src/components/ClassName.ts.html +151 -0
  19. package/coverage/lcov-report/src/components/El.ts.html +166 -0
  20. package/coverage/lcov-report/src/components/FadeIn.ts.html +262 -0
  21. package/coverage/lcov-report/src/components/FadeOut.ts.html +226 -0
  22. package/coverage/lcov-report/src/components/For.ts.html +151 -0
  23. package/coverage/lcov-report/src/components/Fragment.ts.html +142 -0
  24. package/coverage/lcov-report/src/components/HiddenWhenEmpty.ts.html +133 -0
  25. package/coverage/lcov-report/src/components/If.ts.html +217 -0
  26. package/coverage/lcov-report/src/components/InnerHTML.ts.html +178 -0
  27. package/coverage/lcov-report/src/components/Lifecycle.ts.html +157 -0
  28. package/coverage/lcov-report/src/components/Match.ts.html +286 -0
  29. package/coverage/lcov-report/src/components/NotEmpty.tsx.html +184 -0
  30. package/coverage/lcov-report/src/components/On.ts.html +151 -0
  31. package/coverage/lcov-report/src/components/OnRemove.ts.html +145 -0
  32. package/coverage/lcov-report/src/components/OneOf.ts.html +256 -0
  33. package/coverage/lcov-report/src/components/Portal.ts.html +175 -0
  34. package/coverage/lcov-report/src/components/Property.ts.html +154 -0
  35. package/coverage/lcov-report/src/components/Provider.ts.html +244 -0
  36. package/coverage/lcov-report/src/components/Repeat.ts.html +223 -0
  37. package/coverage/lcov-report/src/components/Show.tsx.html +190 -0
  38. package/coverage/lcov-report/src/components/Text.ts.html +229 -0
  39. package/coverage/lcov-report/src/components/TextContent.ts.html +178 -0
  40. package/coverage/lcov-report/src/components/Tween.tsx.html +943 -0
  41. package/coverage/lcov-report/src/components/animatable.ts.html +619 -0
  42. package/coverage/lcov-report/src/components/index.html +476 -0
  43. package/coverage/lcov-report/src/dom-context.ts.html +928 -0
  44. package/coverage/lcov-report/src/helpers/handle-anchor-click.ts.html +277 -0
  45. package/coverage/lcov-report/src/helpers/handle-text-input.ts.html +100 -0
  46. package/coverage/lcov-report/src/helpers/index.html +146 -0
  47. package/coverage/lcov-report/src/helpers/is-empty-element.ts.html +112 -0
  48. package/coverage/lcov-report/src/index.html +176 -0
  49. package/coverage/lcov-report/src/index.ts.html +412 -0
  50. package/coverage/lcov-report/src/jsx-runtime.ts.html +601 -0
  51. package/coverage/lcov-report/src/prop.ts.html +691 -0
  52. package/coverage/lcov-report/src/render.ts.html +112 -0
  53. package/coverage/lcov-report/src/types/idom-context.ts.html +184 -0
  54. package/coverage/lcov-report/src/types/index.html +116 -0
  55. package/coverage/lcov-report/test/common.ts.html +112 -0
  56. package/coverage/lcov-report/test/index.html +116 -0
  57. package/coverage/lcov.info +57 -0
  58. package/dist/index.js +32 -0
  59. package/jest.config.js +5 -0
  60. package/package.json +39 -0
  61. package/src/clean.ts +2 -0
  62. package/src/components/Attribute.ts +23 -0
  63. package/src/components/BooleanAttribute.ts +23 -0
  64. package/src/components/ClassName.ts +22 -0
  65. package/src/components/El.ts +27 -0
  66. package/src/components/FadeIn.ts +59 -0
  67. package/src/components/FadeOut.ts +47 -0
  68. package/src/components/For.ts +22 -0
  69. package/src/components/Fragment.ts +19 -0
  70. package/src/components/HiddenWhenEmpty.ts +16 -0
  71. package/src/components/If.ts +44 -0
  72. package/src/components/InnerHTML.ts +31 -0
  73. package/src/components/Lifecycle.ts +24 -0
  74. package/src/components/Match.ts +67 -0
  75. package/src/components/NotEmpty.tsx +33 -0
  76. package/src/components/On.ts +22 -0
  77. package/src/components/OnRemove.ts +20 -0
  78. package/src/components/OneOf.ts +57 -0
  79. package/src/components/Portal.ts +30 -0
  80. package/src/components/Property.ts +23 -0
  81. package/src/components/Provider.ts +53 -0
  82. package/src/components/Repeat.ts +46 -0
  83. package/src/components/Show.tsx +35 -0
  84. package/src/components/Text.ts +48 -0
  85. package/src/components/TextContent.ts +31 -0
  86. package/src/components/animatable.ts +178 -0
  87. package/src/dom-context.ts +281 -0
  88. package/src/helpers/handle-anchor-click.ts +64 -0
  89. package/src/helpers/handle-text-input.ts +5 -0
  90. package/src/helpers/is-empty-element.ts +9 -0
  91. package/src/index.ts +109 -0
  92. package/src/jsx-dev-runtime.ts +8 -0
  93. package/src/jsx-runtime.ts +172 -0
  94. package/src/jsx.ts +1046 -0
  95. package/src/prop.ts +202 -0
  96. package/src/render.ts +9 -0
  97. package/src/renderable.ts +6 -0
  98. package/test/common.ts +9 -0
  99. package/test/component.spec.tsx +27 -0
  100. package/test/domcontext.spec.ts +36 -0
  101. package/test/fadein.spec.tsx +36 -0
  102. package/test/fadeout.spec.tsx +41 -0
  103. package/test/if.spec.tsx +30 -0
  104. package/test/innerhtml.spec.tsx +45 -0
  105. package/test/prop.spec.ts +10 -0
  106. package/test/render.spec.tsx +19 -0
  107. package/test/textcontent.spec.tsx +45 -0
  108. package/test/when.spec.tsx +30 -0
  109. package/tsconfig.json +21 -0
package/src/prop.ts ADDED
@@ -0,0 +1,202 @@
1
+ export type ArgsToSignals<T extends [...unknown[]]> = T extends [] ? [] : T extends [infer U, ...infer V] ? [Signal<U>, ...ArgsToSignals<V>] : []
2
+
3
+ const $isSignal = Symbol('isSignal')
4
+ const $isProp = Symbol('isProp')
5
+
6
+ export type Value<T> = T | Signal<T>
7
+
8
+ export class Signal<T> {
9
+ static of<T>(value: T): Signal<T> {
10
+ return new Signal(value)
11
+ }
12
+
13
+ static ofValue<T>(value: Value<T> | null | undefined): Signal<T> | undefined {
14
+ return value == null ? undefined : Signal.wrap(value)
15
+ }
16
+
17
+ static wrap<T>(value: T | Signal<T>): Signal<T> {
18
+ return Signal.isSignal<T>(value) ? value : new Signal(value)
19
+ }
20
+
21
+ static isSignal<T = unknown>(x: unknown): x is Signal<T> {
22
+ const s = x as Signal<T>
23
+ return s != null && typeof s.get === 'function' && typeof s.subscribe === 'function'
24
+ }
25
+
26
+ /**
27
+ * Combines many into one using a merging function
28
+ */
29
+ static combine<Args extends [...unknown[]], Out>(
30
+ others: ArgsToSignals<Args>,
31
+ f: (...args: Args) => Out
32
+ ): Signal<Out> {
33
+ function getValues(others: Array<Prop<unknown>>): Args {
34
+ return others.map(other => other.get()) as Args
35
+ }
36
+ const prop = new Prop(f(...getValues(others)))
37
+ others.forEach((other: Prop<unknown>) => {
38
+ other.subscribe(() => {
39
+ prop.set(f(...getValues(others)))
40
+ })
41
+ })
42
+ return prop
43
+ }
44
+
45
+ public readonly [$isSignal] = true
46
+
47
+ protected readonly _listeners: Array<(value: T) => void> = []
48
+ constructor(protected _value: T) { }
49
+ readonly get = (): T => {
50
+ return this._value
51
+ }
52
+
53
+ readonly subscribe = (listener: (value: T) => void): () => void => {
54
+ this._listeners.push(listener)
55
+ return () => {
56
+ const index = this._listeners.indexOf(listener)
57
+ if (index >= 0) { this._listeners.splice(index, 1) }
58
+ }
59
+ }
60
+
61
+ readonly map = <V>(f: (value: T) => V): Signal<V> => {
62
+ const prop = new Prop(f(this._value))
63
+ this.subscribe(value => { prop.set(f(value)) })
64
+ return prop
65
+ }
66
+
67
+ readonly flatMap = <V>(f: (value: T) => Signal<V>): Signal<V> => {
68
+ let signal = f(this._value)
69
+ const prop = new Prop(signal.get())
70
+ this.subscribe(value => {
71
+ signal.clean()
72
+ signal = f(value)
73
+ signal.subscribe(value => { prop.set(value) })
74
+ })
75
+ return prop
76
+ }
77
+
78
+ readonly tap = (f: (value: T) => void): this => {
79
+ f(this._value)
80
+ this.subscribe(f)
81
+ return this
82
+ }
83
+
84
+ readonly at = <K extends keyof T>(key: K): Signal<T[K]> => {
85
+ return this.map(value => value[key])
86
+ }
87
+
88
+ readonly filter = (predicate: (value: T) => boolean): Signal<T> => {
89
+ const prop = new Prop(this._value)
90
+ this.subscribe(value => {
91
+ if (predicate(value)) {
92
+ prop.set(value)
93
+ }
94
+ })
95
+ return prop
96
+ }
97
+
98
+ readonly distinct = (equality: (a: T, b: T) => boolean): Signal<T> => {
99
+ let lastValue = this._value
100
+ return this.filter(value => {
101
+ const result = !equality(value, lastValue)
102
+ lastValue = value
103
+ return result
104
+ })
105
+ }
106
+
107
+ readonly mapAsync = <V>(f: (value: T) => Promise<V>, alt: V): Signal<V> => {
108
+ let counter = 0
109
+ const prop = new Prop(alt)
110
+ f(this._value)
111
+ .then(value => {
112
+ if (counter === 0) prop.set(value)
113
+ })
114
+ .catch(e => {
115
+ throw e
116
+ })
117
+ this.subscribe(value => {
118
+ const matchCounter = ++counter
119
+ f(value)
120
+ .then(value => {
121
+ if (matchCounter === counter) { prop.set(value) }
122
+ })
123
+ .catch(e => {
124
+ throw e
125
+ })
126
+ })
127
+ return prop
128
+ }
129
+
130
+ readonly mapMaybe = <V>(f: (value: T) => V | null | undefined, alt: V): Signal<V> => {
131
+ const prop = new Prop(f(this._value) ?? alt)
132
+ this.subscribe(value => {
133
+ const newValue = f(value)
134
+ if (newValue != null) prop.set(newValue)
135
+ })
136
+ return prop
137
+ }
138
+
139
+ readonly combine = <V, Z>(other: Signal<V>, f: (a: T, b: V) => Z): Signal<Z> => {
140
+ const prop = new Prop(f(this._value, other.get()))
141
+ this.subscribe(value => { prop.set(f(value, other.get())) })
142
+ other.subscribe(value => { prop.set(f(this._value, value)) })
143
+ return prop
144
+ }
145
+
146
+ readonly feed = (prop: Prop<T>): Prop<T> => {
147
+ this.subscribe(value => { prop.set(value) })
148
+ return prop
149
+ }
150
+
151
+ readonly deriveProp = (): Prop<T> => {
152
+ return new Prop(this._value)
153
+ }
154
+
155
+ readonly clean = (): void => {
156
+ this._listeners.length = 0
157
+ }
158
+ }
159
+
160
+ export class Prop<T> extends Signal<T> {
161
+ static isProp<T = unknown>(x: unknown): x is Prop<T> {
162
+ const p = x as Prop<T>
163
+ return Signal.isSignal(x) && typeof p.set === 'function'
164
+ // return x != null && (x as Prop<T>)[$isProp] === true
165
+ }
166
+
167
+ static override of<T>(value: T): Prop<T> {
168
+ return new Prop(value)
169
+ }
170
+
171
+ public readonly [$isProp] = true
172
+
173
+ readonly set = (value: T): void => {
174
+ if (this._value === value) return
175
+ this._value = value
176
+ this._listeners.forEach(listener => { listener(value) })
177
+ }
178
+
179
+ readonly update = (f: (value: T) => T): void => {
180
+ this.set(f(this._value))
181
+ }
182
+
183
+ readonly atLens = <K extends keyof T>(key: K): Prop<T[K]> => {
184
+ return this.iso(
185
+ value => value[key],
186
+ value => ({ ...this._value, [key]: value })
187
+ )
188
+ }
189
+
190
+ readonly reducer = <V>(f: (state: T, action: V) => T) => {
191
+ return (action: V) => {
192
+ this.set(f(this._value, action))
193
+ }
194
+ }
195
+
196
+ readonly iso = <V>(map: (value: T) => V, reverse: (value: V) => T): Prop<V> => {
197
+ const prop = new Prop(map(this._value))
198
+ this.subscribe(value => { prop.set(map(value)) })
199
+ prop.subscribe(value => { this.set(reverse(value)) })
200
+ return prop
201
+ }
202
+ }
package/src/render.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { DOMContext } from './dom-context'
2
+ import { type JSX, makeRenderable } from './jsx-runtime'
3
+ import { type Clean } from './clean'
4
+
5
+ export function render(renderable: JSX.DOMNode, element: HTMLElement): Clean {
6
+ const ctx = DOMContext.of(element)
7
+ const clear = makeRenderable(renderable).appendTo(ctx)
8
+ return () => { clear(true) }
9
+ }
@@ -0,0 +1,6 @@
1
+ import { type DOMContext } from './dom-context'
2
+ import { type Clear } from './clean'
3
+
4
+ export interface Renderable {
5
+ appendTo: (ctx: DOMContext) => Clear
6
+ }
package/test/common.ts ADDED
@@ -0,0 +1,9 @@
1
+ export type Assertion = ReturnType<typeof expect>
2
+
3
+ export function expectBody() {
4
+ return expect(document.body.innerHTML)
5
+ }
6
+
7
+ export function expectHead() {
8
+ return expect(document.head.innerHTML)
9
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @jsxImportSource ../src
3
+ */
4
+
5
+ import { DOMContext, Prop } from '../src'
6
+ import { expectBody } from './common'
7
+ import { makeRenderable } from '../src/jsx-runtime'
8
+
9
+ export const Custom = ({ name, email }: { name: Prop<string>, email: string }) => {
10
+ return (
11
+ <div>
12
+ {name}: {email}
13
+ </div>
14
+ )
15
+ }
16
+
17
+ describe('Component', () => {
18
+ afterEach(() => {
19
+ document.getElementsByTagName('html')[0].innerHTML = ''
20
+ })
21
+
22
+ test('should access scope', () => {
23
+ // Tempo.mount(<Custom name="some" />)
24
+ makeRenderable(<Custom name={Prop.of("foo")} email="bar@gmail.com" />).appendTo(DOMContext.of(document.body))
25
+ expectBody().toBe('<div>foo: bar@gmail.com</div>')
26
+ })
27
+ })
@@ -0,0 +1,36 @@
1
+ import { DOMContext } from '../src/dom-context'
2
+
3
+ describe('DOMContext', () => {
4
+ beforeEach(() => {
5
+ document.body.innerHTML = ''
6
+ document.head.innerHTML = ''
7
+ })
8
+
9
+ test('setAttribute', () => {
10
+ const ctx = DOMContext.of(document.createElement('div'))
11
+ expect(ctx.getElement()).not.toBeNull()
12
+ ctx.setAttribute('id', 'foo')
13
+ expect(ctx.getElement().outerHTML).toBe('<div id="foo"></div>')
14
+ ctx.setAttribute('id', null)
15
+ expect(ctx.getElement().outerHTML).toBe('<div></div>')
16
+ })
17
+
18
+ test('setBooleanAttribute', () => {
19
+ const ctx = DOMContext.of(document.createElement('div'))
20
+ expect(ctx.getElement()).not.toBeNull()
21
+ ctx.setBooleanAttribute('hidden', true)
22
+ expect(ctx.getElement().outerHTML).toBe('<div hidden=""></div>')
23
+ ctx.setBooleanAttribute('hidden', false)
24
+ expect(ctx.getElement().outerHTML).toBe('<div></div>')
25
+ })
26
+
27
+ test('createClass', () => {
28
+ const ctx = DOMContext.of(document.createElement('div'))
29
+ const [setClass, clear] = ctx.createClass('foo')
30
+ expect(ctx.getElement().outerHTML).toBe('<div class="foo"></div>')
31
+ setClass('bar')
32
+ expect(ctx.getElement().outerHTML).toBe('<div class="bar"></div>')
33
+ clear(true)
34
+ expect(ctx.getElement().outerHTML).toBe('<div></div>')
35
+ })
36
+ })
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @jsxImportSource ../src
3
+ */
4
+
5
+ import { render, FadeIn } from '../src'
6
+
7
+ function sleep(ms: number) {
8
+ return new Promise(resolve => setTimeout(resolve, ms))
9
+ }
10
+
11
+ describe('FadeIn', () => {
12
+ afterEach(() => {
13
+ document.body.innerHTML = ''
14
+ document.head.innerHTML = ''
15
+ })
16
+
17
+ test('with delay and start', async () => {
18
+ const view = <div><FadeIn delay={10} duration={10} start={{ opacity: 0 }} opacity={1} /></div>
19
+ render(view, document.body)
20
+ expect(document.body.innerHTML).toBe('<div style="opacity: 0;"></div>')
21
+ await sleep(1)
22
+ expect(document.body.innerHTML).toBe('<div style="opacity: 0;"></div>')
23
+ await sleep(20)
24
+ expect(document.body.innerHTML).toBe('<div style="opacity: 1;"></div>')
25
+ })
26
+
27
+ test('without delay and start', async () => {
28
+ const view = <div><FadeIn duration={10} opacity={1} /></div>
29
+ render(view, document.body)
30
+ expect(document.body.innerHTML).toBe('<div style="opacity: 0;"></div>')
31
+ await sleep(1)
32
+ expect(document.body.innerHTML).not.toBe('<div style="opacity: 0;"></div>')
33
+ await sleep(20)
34
+ expect(document.body.innerHTML).toBe('<div style="opacity: 1;"></div>')
35
+ })
36
+ })
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @jsxImportSource ../src
3
+ */
4
+
5
+ import { render, FadeOut, Prop, When } from '../src'
6
+
7
+ function sleep(ms: number) {
8
+ return new Promise(resolve => setTimeout(resolve, ms))
9
+ }
10
+
11
+ describe('FadeOut', () => {
12
+ afterEach(() => {
13
+ document.body.innerHTML = ''
14
+ document.head.innerHTML = ''
15
+ })
16
+
17
+ test('removed with clear', async () => {
18
+ const view = <div style="opacity: 1"><FadeOut duration={10} opacity={0} /></div>
19
+ const clear = render(view, document.body)
20
+ expect(document.body.innerHTML).toBe('<div style="opacity: 1;"></div>')
21
+ clear()
22
+ expect(document.body.innerHTML).toBe('<div style="opacity: 1;"></div>')
23
+ await sleep(1)
24
+ expect(document.body.innerHTML).not.toBe('<div style="opacity: 1;"></div>')
25
+ await sleep(20)
26
+ expect(document.body.innerHTML).toBe('')
27
+ })
28
+
29
+ test('with delay', async () => {
30
+ const cond = Prop.of(true)
31
+ const view = <When is={cond}><div style="opacity: 1"><FadeOut delay={10} duration={10} opacity={0} /></div></When>
32
+ render(view, document.body)
33
+ expect(document.body.innerHTML).toBe('<div style="opacity: 1;"></div>')
34
+ cond.set(false)
35
+ expect(document.body.innerHTML).toBe('<div style="opacity: 1;"></div>')
36
+ await sleep(1)
37
+ expect(document.body.innerHTML).toBe('<div style="opacity: 1;"></div>')
38
+ await sleep(20)
39
+ expect(document.body.innerHTML).toBe('')
40
+ })
41
+ })
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @jsxImportSource ../src
3
+ */
4
+
5
+ import { render, If, Prop } from '../src'
6
+
7
+ describe('If', () => {
8
+ afterEach(() => {
9
+ document.body.innerHTML = ''
10
+ document.head.innerHTML = ''
11
+ })
12
+
13
+ test('when true/false', () => {
14
+ const prop = Prop.of(true)
15
+ const view = <If is={prop} then={<div>foo</div>} otherwise={<span>bar</span>} />
16
+ render(view, document.body)
17
+ expect(document.body.innerHTML).toBe('<div>foo</div>')
18
+ prop.set(false)
19
+ expect(document.body.innerHTML).toBe('<span>bar</span>')
20
+ })
21
+
22
+ test('without false', () => {
23
+ const prop = Prop.of(true)
24
+ const view = <If is={prop} then={<div>foo</div>} />
25
+ render(view, document.body)
26
+ expect(document.body.innerHTML).toBe('<div>foo</div>')
27
+ prop.set(false)
28
+ expect(document.body.innerHTML).toBe('')
29
+ })
30
+ })
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @jsxImportSource ../src
3
+ */
4
+
5
+ import { render, Prop, InnerHTML, When } from '../src'
6
+
7
+ describe('InnerHTML', () => {
8
+ afterEach(() => {
9
+ document.body.innerHTML = ''
10
+ document.head.innerHTML = ''
11
+ })
12
+
13
+ test('set', () => {
14
+ const prop = Prop.of("<b>foo</b>")
15
+ const view = <div><InnerHTML html={prop} /></div>
16
+ render(view, document.body)
17
+ expect(document.body.innerHTML).toBe('<div><b>foo</b></div>')
18
+ prop.set("<i>bar</i>")
19
+ expect(document.body.innerHTML).toBe('<div><i>bar</i></div>')
20
+ })
21
+
22
+ test('restore', () => {
23
+ const cond = Prop.of(true)
24
+ const prop = Prop.of("foo")
25
+ document.body.textContent = "default"
26
+ const view = <When is={cond}><InnerHTML html={prop} /></When>
27
+ render(view, document.body)
28
+ expect(document.body.innerHTML).toBe('foo')
29
+ prop.set("bar")
30
+ expect(document.body.innerHTML).toBe('bar')
31
+ cond.set(false)
32
+ expect(document.body.innerHTML).toBe('default')
33
+ })
34
+
35
+ test('set nullable', () => {
36
+ const prop = Prop.of(undefined as string | undefined)
37
+ const view = <div><InnerHTML html={prop} /></div>
38
+ render(view, document.body)
39
+ expect(document.body.innerHTML).toBe('<div></div>')
40
+ prop.set("bar")
41
+ expect(document.body.innerHTML).toBe('<div>bar</div>')
42
+ prop.set(undefined)
43
+ expect(document.body.innerHTML).toBe('<div></div>')
44
+ })
45
+ })
@@ -0,0 +1,10 @@
1
+ import { Prop } from '../src/prop'
2
+
3
+ describe('Prop', () => {
4
+ test('get/set', () => {
5
+ const prop = new Prop(1)
6
+ expect(prop.get()).toBe(1)
7
+ prop.set(2)
8
+ expect(prop.get()).toBe(2)
9
+ })
10
+ })
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @jsxImportSource ../src
3
+ */
4
+
5
+ import { render } from '../src'
6
+
7
+ describe('render', () => {
8
+ afterEach(() => {
9
+ document.body.innerHTML = ''
10
+ document.head.innerHTML = ''
11
+ })
12
+
13
+ test('render div', () => {
14
+ const clear = render(<div>foo</div>, document.body)
15
+ expect(document.body.innerHTML).toBe('<div>foo</div>')
16
+ clear()
17
+ expect(document.body.innerHTML).toBe('')
18
+ })
19
+ })
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @jsxImportSource ../src
3
+ */
4
+
5
+ import { render, If, Prop, TextContent, When } from '../src'
6
+
7
+ describe('TextContent', () => {
8
+ afterEach(() => {
9
+ document.body.innerHTML = ''
10
+ document.head.innerHTML = ''
11
+ })
12
+
13
+ test('set', () => {
14
+ const prop = Prop.of("foo")
15
+ const view = <div><TextContent value={prop} /></div>
16
+ render(view, document.body)
17
+ expect(document.body.innerHTML).toBe('<div>foo</div>')
18
+ prop.set("bar")
19
+ expect(document.body.innerHTML).toBe('<div>bar</div>')
20
+ })
21
+
22
+ test('restore', () => {
23
+ const cond = Prop.of(true)
24
+ const prop = Prop.of("foo")
25
+ document.body.textContent = "default"
26
+ const view = <When is={cond}><TextContent value={prop} /></When>
27
+ render(view, document.body)
28
+ expect(document.body.innerHTML).toBe('foo')
29
+ prop.set("bar")
30
+ expect(document.body.innerHTML).toBe('bar')
31
+ cond.set(false)
32
+ expect(document.body.innerHTML).toBe('default')
33
+ })
34
+
35
+ test('set nullable', () => {
36
+ const prop = Prop.of(undefined as string | undefined)
37
+ const view = <div><TextContent value={prop} /></div>
38
+ render(view, document.body)
39
+ expect(document.body.innerHTML).toBe('<div></div>')
40
+ prop.set("bar")
41
+ expect(document.body.innerHTML).toBe('<div>bar</div>')
42
+ prop.set(undefined)
43
+ expect(document.body.innerHTML).toBe('<div></div>')
44
+ })
45
+ })
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @jsxImportSource ../src
3
+ */
4
+
5
+ import { render, When, Unless, Prop } from '../src'
6
+
7
+ describe('When', () => {
8
+ afterEach(() => {
9
+ document.body.innerHTML = ''
10
+ document.head.innerHTML = ''
11
+ })
12
+
13
+ test('when true/false', () => {
14
+ const prop = Prop.of(true)
15
+ const view = <When is={prop}><div>foo</div></When>
16
+ render(view, document.body)
17
+ expect(document.body.innerHTML).toBe('<div>foo</div>')
18
+ prop.set(false)
19
+ expect(document.body.innerHTML).toBe('')
20
+ })
21
+
22
+ test('unless true/false', () => {
23
+ const prop = Prop.of(false)
24
+ const view = <Unless is={prop}><div>foo</div></Unless>
25
+ render(view, document.body)
26
+ expect(document.body.innerHTML).toBe('<div>foo</div>')
27
+ prop.set(true)
28
+ expect(document.body.innerHTML).toBe('')
29
+ })
30
+ })
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "rootDir": "src",
4
+ "outDir": "dist",
5
+ "target": "ESNext",
6
+ "declaration": true,
7
+ "useDefineForClassFields": true,
8
+ "module": "ESNext",
9
+ "lib": ["ESNext", "DOM"],
10
+ "moduleResolution": "Node",
11
+ "strict": true,
12
+ "resolveJsonModule": true,
13
+ "isolatedModules": true,
14
+ "esModuleInterop": true,
15
+ "noImplicitReturns": true,
16
+ "skipLibCheck": true,
17
+ "jsx": "react-jsx",
18
+ "jsxImportSource": "./src",
19
+ },
20
+ "include": ["src/**/*"]
21
+ }