Skip to content

Commit d3f4d87

Browse files
Merge pull request #4 from functionalland/feature/types
Fix all types and update tests
2 parents 2854bf2 + 57bbbe8 commit d3f4d87

File tree

14 files changed

+415
-1230
lines changed

14 files changed

+415
-1230
lines changed

library/asserts.js

Lines changed: 0 additions & 699 deletions
This file was deleted.

library/component.d.ts

Lines changed: 0 additions & 118 deletions
This file was deleted.

library/component.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { maybeCall, parseSpineCaseToCamelCase } from "./utilities.js";
1+
import { maybeCall, parseSpineCaseToCamelCase } from "./utilities.ts";
22

33
export const StateSymbol = Symbol();
44
export const ValidateAttributeSymbol = Symbol();
@@ -42,8 +42,8 @@ export type ValidateAttributeCallback<
4242
> = (
4343
a: {
4444
name: string;
45-
oldValue: Partial<S>[Extract<keyof Partial<S>, string>];
46-
value: Partial<S>[Extract<keyof Partial<S>, string>];
45+
oldValue: S[Extract<keyof S, string>];
46+
value: S[Extract<keyof S, string>];
4747
},
4848
e: CustomElement<S>,
4949
s: S,
@@ -161,7 +161,7 @@ const asyncRender = <
161161
const factorizeFunctionalComponentClass = <
162162
S extends State,
163163
E extends CustomElement<S> = CustomElement<S>,
164-
>(TargetConstructor = HTMLElement) => (
164+
>(TargetConstructor = globalThis.HTMLElement) => (
165165
class FunctionalComponent extends TargetConstructor {
166166
constructor(gs: Array<ConstructCallback<S, E>>) {
167167
super();
@@ -356,7 +356,7 @@ export const factorizeComponent = <
356356
configurable: true,
357357
enumerable: false,
358358
value() {
359-
return maybeCall(_connectedCallback, this)
359+
return maybeCall(() => _connectedCallback.call(this))
360360
.then((s = {}) => {
361361
this[StateSymbol] = Object.assign({}, state, s);
362362
_render(this, this[StateSymbol], {
@@ -497,14 +497,10 @@ export const useAttributes = <
497497
// Overwrite the type because sometime it's just a string
498498
oldValue: (Reflect.has(map, name)
499499
? map[name](oldValue)
500-
: oldValue) as unknown as Partial<
501-
S
502-
>[Extract<keyof Partial<S>, string>],
500+
: oldValue) as unknown as S[Extract<keyof S, string>],
503501
value: (Reflect.has(map, name)
504502
? map[name](value)
505-
: value) as unknown as Partial<
506-
S
507-
>[Extract<keyof Partial<S>, string>],
503+
: value) as unknown as S[Extract<keyof S, string>],
508504
},
509505
this,
510506
Object.assign({}, this[StateSymbol]),
@@ -552,7 +548,7 @@ export const useAttributes = <
552548
configurable: true,
553549
enumerable: true,
554550
value(this: E) {
555-
return maybeCall(_connectedCallback, this)
551+
return maybeCall(() => _connectedCallback.call(this))
556552
.then(() => {
557553
for (const key of observedAttributes) {
558554
const normalizedKey = parseDatasetToState(key) as keyof S;
@@ -664,7 +660,7 @@ export const useCallbacks = <
664660
configurable: true,
665661
enumerable: true,
666662
value(...xs: [string, string, string]) {
667-
return maybeCall(g, this, ...xs)
663+
return maybeCall(() => g.call(this, ...xs))
668664
.then(() =>
669665
f(
670666
this,
@@ -796,8 +792,8 @@ export const useTemplate = <
796792
configurable: true,
797793
enumerable: true,
798794
value(this: E) {
799-
return maybeCall(_connectedCallback, this)
800-
.then(() => maybeCall(f))
795+
return maybeCall(() => _connectedCallback.call(this))
796+
.then(() => maybeCall<HTMLTemplateElement>(f))
801797
.then((template) => {
802798
if (!template) return;
803799
(this.shadowRoot || this).appendChild(

library/component_test.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { assert, assertEquals } from "./asserts.js";
1+
import { assert, assertEquals } from "./asserts.ts";
22
import {
33
AsyncRenderCallback,
4-
CustomElement,
54
ConstructHOF,
5+
CustomElement,
6+
factorizeComponent,
67
HOF,
78
LifeCycleCallback,
89
State,
910
StateSymbol,
10-
factorizeComponent,
1111
useAttributes,
1212
useCallbacks,
1313
useShadow,
1414
useTemplate,
15+
ValidateAttributeCallback,
1516
} from "./component.ts";
16-
// @deno-types="./testing.d.ts"
17-
import { constructComponent, factorizeSpy, test, withDom } from "./testing.js";
18-
import { deferUntil, deferUntilNextFrame, noop } from "./utilities.js";
17+
import { constructComponent, factorizeSpy, test, withDom } from "./testing.ts";
18+
import { deferUntil, deferUntilNextFrame, noop } from "./utilities.ts";
1919

2020
type RenderSpyFunction<
2121
S extends State,
@@ -44,7 +44,6 @@ test(
4444
});
4545
});
4646
}),
47-
() => "ShadowRoot" in globalThis,
4847
);
4948

5049
test(
@@ -91,7 +90,6 @@ test(
9190
});
9291
});
9392
}),
94-
() => "ShadowRoot" in globalThis,
9593
);
9694

9795
test(
@@ -111,15 +109,17 @@ test(
111109
const e = constructComponent<ComponentState>(Component);
112110
assert(e.shadowRoot);
113111
}),
114-
() => "ShadowRoot" in globalThis,
112+
() => "DocumentFragment" in globalThis,
115113
);
116114

117115
test(
118116
"useAttributes",
119117
withDom(() => {
120118
type ComponentState = { value: number };
121119
const [attributeMapSpy, assertAttributeMapSpy] = factorizeSpy(Number);
122-
const [validateAttributeSpy, assertValidateAttributeSpy] = factorizeSpy(
120+
const [validateAttributeSpy, assertValidateAttributeSpy] = factorizeSpy<
121+
ValidateAttributeCallback<ComponentState>
122+
>(
123123
({ name: _name, oldValue, value }) => (oldValue !== value && value >= 0),
124124
);
125125
const [renderSpy, assertRenderSpy] = factorizeSpy<
@@ -159,7 +159,6 @@ test(
159159
});
160160
});
161161
}),
162-
() => "ShadowRoot" in globalThis,
163162
);
164163

165164
test(
@@ -170,16 +169,15 @@ test(
170169
active: false,
171170
count: 0,
172171
};
173-
const callback =
174-
((
175-
_e: CustomElement<ComponentState>,
176-
render: AsyncRenderCallback<ComponentState>,
177-
...xs: [string, string, string]
178-
) => {
179-
render((_, { count }) => ({ active: true, count: ++count }))(
180-
{} as unknown as Event,
181-
);
182-
}) as LifeCycleCallback<ComponentState>;
172+
const callback = ((
173+
_e: CustomElement<ComponentState>,
174+
render: AsyncRenderCallback<ComponentState>,
175+
..._xs: [string, string, string]
176+
) => {
177+
render((_, { count }) => ({ active: true, count: ++count }))(
178+
{} as unknown as Event,
179+
);
180+
}) as LifeCycleCallback<ComponentState>;
183181
const [adoptedCallbackSpy, assertAdoptedCallbackSpy] = factorizeSpy(
184182
callback,
185183
);
@@ -210,13 +208,13 @@ test(
210208

211209
const e = constructComponent(Component);
212210

211+
e.connectedCallback && e.connectedCallback();
212+
213213
e.adoptedCallback && e.adoptedCallback();
214214

215215
e.attributeChangedCallback &&
216216
e.attributeChangedCallback("value", null, "42");
217217

218-
e.connectedCallback && e.connectedCallback();
219-
220218
e.disconnectedCallback && e.disconnectedCallback();
221219

222220
return deferUntilNextFrame()
@@ -234,7 +232,6 @@ test(
234232
assert(assertRenderSpy.callCount === 5);
235233
});
236234
}),
237-
() => "ShadowRoot" in globalThis,
238235
);
239236

240237
test(
@@ -280,7 +277,10 @@ test(
280277
addButton: (e) => e.querySelector("button"),
281278
number: (e) => e.querySelector("span"),
282279
},
283-
)((f) => f(Component, renderSpy, initialState), noop as ConstructHOF<ComponentState>);
280+
)(
281+
(f) => f(Component, renderSpy, initialState),
282+
noop as ConstructHOF<ComponentState>,
283+
);
284284

285285
const e = constructComponent<ComponentState>(Component);
286286

library/testing.d.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)