diff --git a/src/ElmComponent.ts b/src/ElmComponent.ts index 7a8111e..71c1aef 100644 --- a/src/ElmComponent.ts +++ b/src/ElmComponent.ts @@ -1,6 +1,7 @@ import { InitFunction, Nullable, UpdateFunction } from "./Types"; import { Message, Services } from "./Init"; import { Cmd } from "./Cmd"; +import { getFakeInitResultOnce } from "./Testing/fakeInitResult"; import React from "react"; /** @@ -31,7 +32,7 @@ abstract class ElmComponent extends React. public constructor (props: TProps, init: InitFunction, name: string) { super(props); - const [model, cmd] = init(this.props); + const [model, cmd] = getFakeInitResultOnce() ?? init(this.props); this.componentName = name; this.currentModel = model; diff --git a/src/Testing/fakeInitResult.ts b/src/Testing/fakeInitResult.ts new file mode 100644 index 0000000..2ba094c --- /dev/null +++ b/src/Testing/fakeInitResult.ts @@ -0,0 +1,22 @@ +import { InitResult, MessageBase, Nullable } from "../Types"; + +type FakeInitResult = Nullable<[unknown, unknown?]>; + +let initResult: FakeInitResult; + +function setFakeInitResult (model: FakeInitResult): void { + initResult = model; +} + +function getFakeInitResultOnce (): Nullable> { + const temp = initResult as Nullable>; + + initResult = null; + + return temp; +} + +export { + setFakeInitResult, + getFakeInitResultOnce, +}; \ No newline at end of file diff --git a/src/Testing/index.ts b/src/Testing/index.ts index 7266f37..2d31ccb 100644 --- a/src/Testing/index.ts +++ b/src/Testing/index.ts @@ -1,6 +1,8 @@ -import { MessageBase, Nullable, UpdateMap, UpdateReturnType } from "../Types"; +import { InitResult, MessageBase, Nullable, UpdateMap, UpdateReturnType } from "../Types"; import { callUpdateMap } from "../useElmish"; import { Cmd } from "../Cmd"; +import React from "react"; +import { setFakeInitResult } from "./fakeInitResult"; /** * Extracts the messages out of a command. @@ -86,6 +88,12 @@ function createUpdateArgsFactory }; } +function renderWithFakeInit (initResult: InitResult, render: () => React.ReactNode): void { + setFakeInitResult(initResult); + render(); + setFakeInitResult(null); +} + export type { UpdateArgsFactory, }; @@ -95,4 +103,5 @@ export { execCmd, getUpdateFn, createUpdateArgsFactory, + renderWithFakeInit, }; \ No newline at end of file diff --git a/src/useElmish.ts b/src/useElmish.ts index b05655c..08b823b 100644 --- a/src/useElmish.ts +++ b/src/useElmish.ts @@ -2,6 +2,7 @@ import { Cmd, Dispatch } from "./Cmd"; import { InitFunction, MessageBase, Nullable, UpdateFunction, UpdateMap, UpdateReturnType } from "./Types"; import { useCallback, useEffect, useRef, useState } from "react"; +import { getFakeInitResultOnce } from "./Testing/fakeInitResult"; import { Services } from "./Init"; type SubscriptionResult = [Cmd, (() => void)?]; @@ -102,7 +103,7 @@ function useElmish ({ name, props, }, []); if (!initializedModel) { - const [initModel, initCmd] = init(props); + const [initModel, initCmd] = getFakeInitResultOnce() ?? init(props); initializedModel = initModel; setModel(initializedModel);