:phoenix, “~> 1.6.2”
:phoenix_live_view, “~> 0.17.5”
I am trying to follow this post [Phoenix LiveView] formatting date/time with local time zone - DEV Community to handle the proper local timezone for my Phoenix Live View app.
The implementation looks fine and working, but I am facing problems when it comes to testing.
Here is what I have in terms of code:
// app.js
const liveSocket = new LiveSocket('/live', Socket, {
params: {
_csrf_token: csrfToken,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
}
})
# my_file.ex
def mount(%{"state" => state, "user_id" => user_id}, session, socket) do
{:ok, socket} = assign_defaults(session, socket)
offices = Locations.list_offices_for_state(state)
time_zone = get_connect_params(socket)["timezone"] || @default_time_zone
socket =
socket
|> assign(:page_title, "Registration - Schedule | Mindful Care")
|> assign(:state, state)
|> assign(:user_id, user_id)
|> assign(:offices, offices)
|> assign(:time_zone, time_zone)
|> reset_state()
|> assign(:step, "select_office")
{:ok, socket}
end
# my_file_test.exs
test "loads offices with providers and sets state", %{
socket: socket,
user: user,
office: office,
provider: provider
} do
params = %{"user_id" => user.id, "state" => office.state_abbr}
{:ok, socket} = Schedule.mount(params, nil, socket)
assert socket.assigns.page_title == "Registration - Schedule | Mindful Care"
assert socket.assigns.step == "select_office"
assert [off] = socket.assigns.offices
assert off.id == office.id
assert [prov] = off.providers
assert prov.id == provider.id
end
user
,office
,provider
are just factories
So I wanna test if calling this mount will return the data I am expecting. My mount function uses another function Phoenix.LiveView — Phoenix LiveView v0.20.2 to get timezone value, but the call to get_connect_params
is raising an error that I can not fix to make my test work.
Error:
1) test mount/3 loads offices with providers and sets state (MindfulWeb.OnboardingLive.ScheduleTest)
** (RuntimeError) attempted to read connect_params outside of MindfulWeb.OnboardingLive.Schedule.mount/3.
connect_params only exists while mounting. If you require access to this information
after mount, store the state in socket assigns.
Is there any way to bypass this, to preset a value, or even a better approach to test if the mount will set the values I am expecting?
Thanks ahead!