Skip to content

Commit

Permalink
Merge pull request #4 from functionalland/feature/types
Browse files Browse the repository at this point in the history
Fix all types and update tests
  • Loading branch information
sebastienfilion authored Jun 7, 2022
2 parents 2854bf2 + 57bbbe8 commit d3f4d87
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 1,230 deletions.
699 changes: 0 additions & 699 deletions library/asserts.js

This file was deleted.

118 changes: 0 additions & 118 deletions library/component.d.ts

This file was deleted.

26 changes: 11 additions & 15 deletions library/component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { maybeCall, parseSpineCaseToCamelCase } from "./utilities.js";
import { maybeCall, parseSpineCaseToCamelCase } from "./utilities.ts";

export const StateSymbol = Symbol();
export const ValidateAttributeSymbol = Symbol();
Expand Down Expand Up @@ -42,8 +42,8 @@ export type ValidateAttributeCallback<
> = (
a: {
name: string;
oldValue: Partial<S>[Extract<keyof Partial<S>, string>];
value: Partial<S>[Extract<keyof Partial<S>, string>];
oldValue: S[Extract<keyof S, string>];
value: S[Extract<keyof S, string>];
},
e: CustomElement<S>,
s: S,
Expand Down Expand Up @@ -161,7 +161,7 @@ const asyncRender = <
const factorizeFunctionalComponentClass = <
S extends State,
E extends CustomElement<S> = CustomElement<S>,
>(TargetConstructor = HTMLElement) => (
>(TargetConstructor = globalThis.HTMLElement) => (
class FunctionalComponent extends TargetConstructor {
constructor(gs: Array<ConstructCallback<S, E>>) {
super();
Expand Down Expand Up @@ -356,7 +356,7 @@ export const factorizeComponent = <
configurable: true,
enumerable: false,
value() {
return maybeCall(_connectedCallback, this)
return maybeCall(() => _connectedCallback.call(this))
.then((s = {}) => {
this[StateSymbol] = Object.assign({}, state, s);
_render(this, this[StateSymbol], {
Expand Down Expand Up @@ -497,14 +497,10 @@ export const useAttributes = <
// Overwrite the type because sometime it's just a string
oldValue: (Reflect.has(map, name)
? map[name](oldValue)
: oldValue) as unknown as Partial<
S
>[Extract<keyof Partial<S>, string>],
: oldValue) as unknown as S[Extract<keyof S, string>],
value: (Reflect.has(map, name)
? map[name](value)
: value) as unknown as Partial<
S
>[Extract<keyof Partial<S>, string>],
: value) as unknown as S[Extract<keyof S, string>],
},
this,
Object.assign({}, this[StateSymbol]),
Expand Down Expand Up @@ -552,7 +548,7 @@ export const useAttributes = <
configurable: true,
enumerable: true,
value(this: E) {
return maybeCall(_connectedCallback, this)
return maybeCall(() => _connectedCallback.call(this))
.then(() => {
for (const key of observedAttributes) {
const normalizedKey = parseDatasetToState(key) as keyof S;
Expand Down Expand Up @@ -664,7 +660,7 @@ export const useCallbacks = <
configurable: true,
enumerable: true,
value(...xs: [string, string, string]) {
return maybeCall(g, this, ...xs)
return maybeCall(() => g.call(this, ...xs))
.then(() =>
f(
this,
Expand Down Expand Up @@ -796,8 +792,8 @@ export const useTemplate = <
configurable: true,
enumerable: true,
value(this: E) {
return maybeCall(_connectedCallback, this)
.then(() => maybeCall(f))
return maybeCall(() => _connectedCallback.call(this))
.then(() => maybeCall<HTMLTemplateElement>(f))
.then((template) => {
if (!template) return;
(this.shadowRoot || this).appendChild(
Expand Down
50 changes: 25 additions & 25 deletions library/component_test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { assert, assertEquals } from "./asserts.js";
import { assert, assertEquals } from "./asserts.ts";
import {
AsyncRenderCallback,
CustomElement,
ConstructHOF,
CustomElement,
factorizeComponent,
HOF,
LifeCycleCallback,
State,
StateSymbol,
factorizeComponent,
useAttributes,
useCallbacks,
useShadow,
useTemplate,
ValidateAttributeCallback,
} from "./component.ts";
// @deno-types="./testing.d.ts"
import { constructComponent, factorizeSpy, test, withDom } from "./testing.js";
import { deferUntil, deferUntilNextFrame, noop } from "./utilities.js";
import { constructComponent, factorizeSpy, test, withDom } from "./testing.ts";
import { deferUntil, deferUntilNextFrame, noop } from "./utilities.ts";

type RenderSpyFunction<
S extends State,
Expand Down Expand Up @@ -44,7 +44,6 @@ test(
});
});
}),
() => "ShadowRoot" in globalThis,
);

test(
Expand Down Expand Up @@ -91,7 +90,6 @@ test(
});
});
}),
() => "ShadowRoot" in globalThis,
);

test(
Expand All @@ -111,15 +109,17 @@ test(
const e = constructComponent<ComponentState>(Component);
assert(e.shadowRoot);
}),
() => "ShadowRoot" in globalThis,
() => "DocumentFragment" in globalThis,
);

test(
"useAttributes",
withDom(() => {
type ComponentState = { value: number };
const [attributeMapSpy, assertAttributeMapSpy] = factorizeSpy(Number);
const [validateAttributeSpy, assertValidateAttributeSpy] = factorizeSpy(
const [validateAttributeSpy, assertValidateAttributeSpy] = factorizeSpy<
ValidateAttributeCallback<ComponentState>
>(
({ name: _name, oldValue, value }) => (oldValue !== value && value >= 0),
);
const [renderSpy, assertRenderSpy] = factorizeSpy<
Expand Down Expand Up @@ -159,7 +159,6 @@ test(
});
});
}),
() => "ShadowRoot" in globalThis,
);

test(
Expand All @@ -170,16 +169,15 @@ test(
active: false,
count: 0,
};
const callback =
((
_e: CustomElement<ComponentState>,
render: AsyncRenderCallback<ComponentState>,
...xs: [string, string, string]
) => {
render((_, { count }) => ({ active: true, count: ++count }))(
{} as unknown as Event,
);
}) as LifeCycleCallback<ComponentState>;
const callback = ((
_e: CustomElement<ComponentState>,
render: AsyncRenderCallback<ComponentState>,
..._xs: [string, string, string]
) => {
render((_, { count }) => ({ active: true, count: ++count }))(
{} as unknown as Event,
);
}) as LifeCycleCallback<ComponentState>;
const [adoptedCallbackSpy, assertAdoptedCallbackSpy] = factorizeSpy(
callback,
);
Expand Down Expand Up @@ -210,13 +208,13 @@ test(

const e = constructComponent(Component);

e.connectedCallback && e.connectedCallback();

e.adoptedCallback && e.adoptedCallback();

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

e.connectedCallback && e.connectedCallback();

e.disconnectedCallback && e.disconnectedCallback();

return deferUntilNextFrame()
Expand All @@ -234,7 +232,6 @@ test(
assert(assertRenderSpy.callCount === 5);
});
}),
() => "ShadowRoot" in globalThis,
);

test(
Expand Down Expand Up @@ -280,7 +277,10 @@ test(
addButton: (e) => e.querySelector("button"),
number: (e) => e.querySelector("span"),
},
)((f) => f(Component, renderSpy, initialState), noop as ConstructHOF<ComponentState>);
)(
(f) => f(Component, renderSpy, initialState),
noop as ConstructHOF<ComponentState>,
);

const e = constructComponent<ComponentState>(Component);

Expand Down
36 changes: 0 additions & 36 deletions library/testing.d.ts

This file was deleted.

Loading

0 comments on commit d3f4d87

Please sign in to comment.