Skip to content

Commit afdc9fb

Browse files
committed
add more tests and comment
1 parent 2bac269 commit afdc9fb

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/Feliz/Html.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module HtmlHelper =
1212
let inline createElementWithChildren (tag: string) (children: seq<ReactElement>) =
1313
ReactLegacy.createElement(tag, children = children)
1414

15-
/// Iterates once, returns (firstMatch, restWithoutMatch).
15+
/// This function changes the order of the input array. Last item replaces children key, then we remove the last item.
1616
let extractByKeyFast (key: string) (arr: seq<(string * obj)>) : (string * obj)[] * (string * obj) option =
1717
Fable.Core.JsInterop.emitJsStatement (arr, key) """const arrNext_ = Array.from($0);
1818
for (let i = 0; i < arrNext_.length; i++) {

tests/Feliz/ExtractChildren.test.fs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,47 @@ describe "HtmlHelper.createElement" <| fun _ ->
101101
let propsContainChildren: bool = extractedProps |> Array.exists (fun (k, _) -> k = "children")
102102
expect(propsContainChildren).toBeFalsy()
103103
expect(extractedProps.Length).toBe((props |> Seq.length) - 1)
104+
105+
test "extract as last" <| fun _ ->
106+
let child = [ Html.span []; Html.div [] ]
107+
let props =
108+
[
109+
"id", box "my-div"
110+
"data-test", box true
111+
"style", box {| color = "red" |}
112+
"onClick", box (fun _ -> ())
113+
"tabIndex", box 0
114+
"className", box "container"
115+
"children", box child
116+
]
117+
let extractedProps, childOption = HtmlHelper.extractByKeyFast "children" props
118+
expect(child).toBeTruthy()
119+
let key, child =
120+
match childOption with
121+
| Some kvp -> kvp
122+
| None -> failwith "Expected to find 'children' key"
123+
expect(key).toBe("children")
124+
expect(child).toEqual(child)
125+
let propsContainChildren: bool = extractedProps |> Array.exists (fun (k, _) -> k = "children")
126+
expect(propsContainChildren).toBeFalsy()
127+
expect(extractedProps.Length).toBe((props |> Seq.length) - 1)
128+
129+
test "extract as exactlyOne" <| fun _ ->
130+
let child = [ Html.span []; Html.div [] ]
131+
let props =
132+
[
133+
"children", box child
134+
]
135+
let extractedProps, childOption = HtmlHelper.extractByKeyFast "children" props
136+
Browser.Dom.console.log(extractedProps)
137+
Browser.Dom.console.log(childOption)
138+
expect(child).toBeTruthy()
139+
let key, child =
140+
match childOption with
141+
| Some kvp -> kvp
142+
| None -> failwith "Expected to find 'children' key"
143+
expect(key).toBe("children")
144+
expect(child).toEqual(child)
145+
let propsContainChildren: bool = extractedProps |> Array.exists (fun (k, _) -> k = "children")
146+
expect(propsContainChildren).toBeFalsy()
147+
expect(extractedProps.Length).toBe((props |> Seq.length) - 1)

0 commit comments

Comments
 (0)