Skip to content

Commit e273008

Browse files
committed
lifecycle events are mostly already supported
adding tests to prove it
1 parent 82ab12f commit e273008

File tree

1 file changed

+145
-94
lines changed

1 file changed

+145
-94
lines changed

test/withFx.test.js

Lines changed: 145 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("withFx", () => {
2323
it("should be a function", () => expect(withFx).toBeInstanceOf(Function))
2424
it("should call view without actions", done =>
2525
withFx(app)(undefined, undefined, () => done()))
26-
it("should not interfere with non effect actions", done => {
26+
it("should not interfere with non fx actions", done => {
2727
const main = withFx(app)(
2828
{
2929
value: 0
@@ -57,9 +57,9 @@ describe("withFx", () => {
5757

5858
main.finish()
5959
})
60-
it("should handle empty effects", () =>
60+
it("should handle empty fx", () =>
6161
withFx(app)({}, { foo: () => [] }, Function.prototype).foo())
62-
it("should throw for unknown effects", () =>
62+
it("should throw for unknown fx", () =>
6363
expect(() =>
6464
withFx(app)(
6565
{},
@@ -69,7 +69,7 @@ describe("withFx", () => {
6969
Function.prototype
7070
).foo()
7171
).toThrow("no such fx type: unknown"))
72-
describe("built-in effect", () => {
72+
describe("built-in fx", () => {
7373
describe("action", () => {
7474
it("should throw for unknown actions", () =>
7575
expect(() =>
@@ -145,6 +145,22 @@ describe("withFx", () => {
145145
},
146146
Function.prototype
147147
).foo())
148+
it("should attach to lifecycle events in view", done => {
149+
withFx(app)(
150+
{},
151+
{
152+
foo: data => {
153+
expect(data).toEqual({ some: "data" })
154+
done()
155+
}
156+
},
157+
() =>
158+
h("main", {
159+
oncreate: action("foo", { some: "data" })
160+
}),
161+
document.body
162+
)
163+
})
148164
it("should attach to listeners in view", done => {
149165
withFx(app)(
150166
{
@@ -436,6 +452,22 @@ describe("withFx", () => {
436452
})
437453
})
438454
describe("event", () => {
455+
it("should attach to lifecycle events in view", done => {
456+
withFx(app)(
457+
{},
458+
{
459+
foo(element) {
460+
expect(element.outerHTML).toBe("<main></main>")
461+
done()
462+
}
463+
},
464+
() =>
465+
h("main", {
466+
oncreate: event("foo")
467+
}),
468+
document.body
469+
)
470+
})
439471
it("should attach to listeners in view", done => {
440472
withFx(app)(
441473
{
@@ -664,108 +696,127 @@ describe("withFx", () => {
664696
}
665697
})
666698
})
667-
})
668-
it("should allow combining action and event fx in view", done => {
669-
withFx(app)(
670-
{
671-
message: "hello"
672-
},
673-
{
674-
foo: data => {
675-
expect(data).toEqual({ button: 0 })
699+
it("should allow combining fx in view", done => {
700+
withFx(app)(
701+
{
702+
message: "hello"
676703
},
677-
bar: data => {
678-
expect(data).toEqual({ some: "data" })
679-
done()
680-
}
681-
},
682-
({ message }, actions) =>
683-
h(
684-
"main",
685-
{
686-
oncreate: () => {
687-
expect(actions).toEqual({
688-
foo: expect.any(Function),
689-
bar: expect.any(Function)
690-
})
691-
expect(document.body.innerHTML).toBe(
692-
"<main><h1>hello</h1><button></button></main>"
693-
)
694-
const buttonElement = document.body.firstChild.lastChild
695-
buttonElement.onclick({ button: 0 })
696-
}
704+
{
705+
foo: data => {
706+
expect(data).toEqual({ button: 0 })
697707
},
698-
h("h1", {}, message),
699-
h("button", {
700-
onclick: [event("foo"), action("bar", { some: "data" })]
701-
})
702-
),
703-
document.body
704-
)
708+
bar: data => {
709+
expect(data).toEqual({ some: "data" })
710+
done()
711+
}
712+
},
713+
({ message }, actions) =>
714+
h(
715+
"main",
716+
{
717+
oncreate: () => {
718+
expect(actions).toEqual({
719+
foo: expect.any(Function),
720+
bar: expect.any(Function)
721+
})
722+
expect(document.body.innerHTML).toBe(
723+
"<main><h1>hello</h1><button></button></main>"
724+
)
725+
const buttonElement = document.body.firstChild.lastChild
726+
buttonElement.onclick({ button: 0 })
727+
}
728+
},
729+
h("h1", {}, message),
730+
h("button", {
731+
onclick: [event("foo"), action("bar", { some: "data" })]
732+
})
733+
),
734+
document.body
735+
)
736+
})
705737
})
706-
it("should allow adding new custom effect", () => {
707-
const externalState = { value: 2 }
738+
describe("custom fx", () => {
739+
it("should allow adding new custom effect", () => {
740+
const externalState = { value: 2 }
708741

709-
const main = withFx({
710-
set(props, getAction) {
711-
getAction(props.action)(externalState)
712-
}
713-
})(app)(
714-
{
715-
value: 0
716-
},
717-
{
718-
foo: () => ["set", { action: "set" }],
719-
set: state => state,
720-
get: () => state => state
721-
},
722-
Function.prototype
723-
)
742+
const main = withFx({
743+
set(props, getAction) {
744+
getAction(props.action)(externalState)
745+
}
746+
})(app)(
747+
{
748+
value: 0
749+
},
750+
{
751+
foo: () => ["set", { action: "set" }],
752+
set: state => state,
753+
get: () => state => state
754+
},
755+
Function.prototype
756+
)
724757

725-
expect(main.get()).toEqual({
726-
value: 0
727-
})
758+
expect(main.get()).toEqual({
759+
value: 0
760+
})
728761

729-
main.foo()
730-
expect(main.get()).toEqual({
731-
value: 2
732-
})
762+
main.foo()
763+
expect(main.get()).toEqual({
764+
value: 2
765+
})
733766

734-
externalState.value = 1
767+
externalState.value = 1
735768

736-
main.foo()
737-
expect(main.get()).toEqual({
738-
value: 1
769+
main.foo()
770+
expect(main.get()).toEqual({
771+
value: 1
772+
})
739773
})
740-
})
741-
it("should allow overriding built-in fx", () => {
742-
const actionLog = []
774+
it("should allow overriding built-in fx", () => {
775+
const actionLog = []
743776

744-
withFx({
745-
action(props) {
746-
actionLog.push(props)
747-
}
748-
})(app)(
749-
{},
750-
{
751-
foo: () => action("bar", { some: "data" }),
752-
bar: () => {
753-
throw new Error(
754-
"expected bar not to be called with overridden action effect!"
755-
)
777+
withFx({
778+
action(props) {
779+
actionLog.push(props)
756780
}
757-
},
758-
Function.prototype
759-
).foo()
781+
})(app)(
782+
{},
783+
{
784+
foo: () => action("bar", { some: "data" }),
785+
bar: () => {
786+
throw new Error(
787+
"expected bar not to be called with overridden action effect!"
788+
)
789+
}
790+
},
791+
Function.prototype
792+
).foo()
760793

761-
expect(actionLog).toEqual([
762-
{
763-
name: "bar",
764-
event: null,
765-
data: {
766-
some: "data"
794+
expect(actionLog).toEqual([
795+
{
796+
name: "bar",
797+
event: null,
798+
data: {
799+
some: "data"
800+
}
801+
}
802+
])
803+
})
804+
it("should attach to lifecycle events in view", done => {
805+
withFx({
806+
yolo(props) {
807+
props.event.innerHTML = "#YOLO"
808+
expect(document.body.innerHTML).toBe("<main>#YOLO</main>")
809+
done()
767810
}
768-
}
769-
])
811+
})(app)(
812+
{},
813+
{},
814+
() =>
815+
h("main", {
816+
oncreate: ["yolo", {}]
817+
}),
818+
document.body
819+
)
820+
})
770821
})
771822
})

0 commit comments

Comments
 (0)