Hi folks,
I am trying to test a LiveView that renders a page with a form. This form only contains 2 inputs: one hidden_input
that gets populate by a JS hook and one file_input
that is ignored by phoenix liveview and has the JS hook.
<%= hidden_input f, :file_base64, id: "hidden-input-base64", phx_update: "ignore" %>
<%= file_input f, :file, phx_hook: "ConvertToBase64", accept: ".csv", multiple: false, required: true %>
Here is my JS hook:
const ConvertToBase64 = {
mounted() {
this.el.addEventListener("change", () => {
if (this.el.files[0]) {
const $fileName = document.querySelector(`#${FILE_NAME_ID}`);
$fileName.textContent = this.el.files[0].name;
toBase64(this.el.files[0]).then((base64) => {
const $hiddenInput = document.querySelector(`#${HIDDEN_INPUT_ID}`);
$hiddenInput.setAttribute("value", base64);
$hiddenInput.focus(); // this is needed to register the new value with live view
});
}
});
},
};
export default ConvertToBase64;
So everything works well in dev and I am now trying to test my liveview. I wrote an integration test that opens the page, file the form (the hidden input) and render it but I get this error:
(ArgumentError) value for hidden "import[file_base64]" must be one of [""], got: "data/csv;base64,VGhvbWFz"
.
For info here is my incomplete test:
@base_import %{
# TODO: remove this once liveview supports file upload
file: "[object Object]",
file_base64: "data/csv;base64,VGhvbWFz"
}
test "import transactions", %{conn: conn, platform: platform} do
{:ok, index_live, _html} = live(conn, Routes.transaction_index_path(conn, :index))
assert index_live |> element(@selectors[:import_transactions_button]) |> render_click() =~
"Import transactions"
assert_patch(index_live, Routes.transaction_index_path(conn, :import))
{:ok, _, html} =
index_live
|> form(@selectors[:import_form], import: @base_import)
|> render_submit()
|> follow_redirect(conn, Routes.transaction_index_path(conn, :index))
assert html =~ "Import complete"
end
Any idea what might I do wrong?
Thanks in advance