63 lines
2.1 KiB
Elixir
63 lines
2.1 KiB
Elixir
defmodule MvWeb.Helpers.DateFormatterTest do
|
|
@moduledoc """
|
|
Tests for DateFormatter: date/datetime formatting and timezone conversion for display.
|
|
"""
|
|
use ExUnit.Case, async: true
|
|
|
|
alias MvWeb.Helpers.DateFormatter
|
|
|
|
describe "format_date/1" do
|
|
test "formats Date to European format (dd.mm.yyyy)" do
|
|
assert DateFormatter.format_date(~D[2024-03-15]) == "15.03.2024"
|
|
end
|
|
|
|
test "returns empty string for nil" do
|
|
assert DateFormatter.format_date(nil) == ""
|
|
end
|
|
|
|
test "returns 'Invalid date' for non-Date" do
|
|
assert DateFormatter.format_date("2024-03-15") == "Invalid date"
|
|
end
|
|
end
|
|
|
|
describe "format_datetime/1 and format_datetime/2" do
|
|
test "formats UTC DateTime without timezone (European format)" do
|
|
dt = ~U[2024-03-15 10:30:00Z]
|
|
assert DateFormatter.format_datetime(dt) == "15.03.2024 10:30"
|
|
end
|
|
|
|
test "format_datetime with nil timezone same as no timezone (UTC)" do
|
|
dt = ~U[2024-03-15 10:30:00Z]
|
|
assert DateFormatter.format_datetime(dt, nil) == "15.03.2024 10:30"
|
|
end
|
|
|
|
test "formats DateTime in Europe/Berlin (CET/CEST)" do
|
|
# Winter: 10:30 UTC = 11:30 CET (UTC+1)
|
|
dt = ~U[2024-01-15 10:30:00Z]
|
|
assert DateFormatter.format_datetime(dt, "Europe/Berlin") == "15.01.2024 11:30"
|
|
|
|
# Summer: 10:30 UTC = 12:30 CEST (UTC+2)
|
|
dt_summer = ~U[2024-07-15 10:30:00Z]
|
|
assert DateFormatter.format_datetime(dt_summer, "Europe/Berlin") == "15.07.2024 12:30"
|
|
end
|
|
|
|
test "empty string timezone falls back to UTC" do
|
|
dt = ~U[2024-03-15 10:30:00Z]
|
|
assert DateFormatter.format_datetime(dt, "") == "15.03.2024 10:30"
|
|
end
|
|
|
|
test "invalid timezone falls back to UTC" do
|
|
dt = ~U[2024-03-15 10:30:00Z]
|
|
assert DateFormatter.format_datetime(dt, "Invalid/Zone") == "15.03.2024 10:30"
|
|
end
|
|
|
|
test "returns empty string for nil datetime" do
|
|
assert DateFormatter.format_datetime(nil) == ""
|
|
assert DateFormatter.format_datetime(nil, "Europe/Berlin") == ""
|
|
end
|
|
|
|
test "returns 'Invalid datetime' for non-DateTime" do
|
|
assert DateFormatter.format_datetime("2024-03-15 10:30") == "Invalid datetime"
|
|
end
|
|
end
|
|
end
|