defmodule SurfaceBulma.Components.Form.CheckboxTest do
  use SurfaceBulma.ConnCase

  alias SurfaceBulma.Form
  alias SurfaceBulma.Form.Checkbox

  test "checkbox" do
    html =
      render_surface do
        ~F"""
        <Checkbox form="user" field="admin" />
        """
      end

    assert html =~ """
           <div class="field">
             <div class="control">
               <label class="checkbox">
                 <input name="user[admin]" type="hidden" value="false">\
           <input id="user_admin" name="user[admin]" type="checkbox" value="true">
               </label>
             </div>
           </div>
           """
  end

  test "checkbox with atom field" do
    html =
      render_surface do
        ~F"""
        <Checkbox form="user" field={:admin} />
        """
      end

    assert html =~ """
           <input id="user_admin" name="user[admin]" type="checkbox" value="true">
           """
  end

  test "with form context" do
    html =
      render_surface do
        ~F"""
        <Form for={%{}} as={:user} csrf_token="test">
          <Checkbox field={:admin} />
        </Form>
        """
      end

    assert html =~ """
           <form action="#" method="post">
               <input name="_csrf_token" type="hidden" hidden value="test">
             <div class="field">
             <div class="control">
               <label class="checkbox">
                 <input name="user[admin]" type="hidden" value="false">\
           <input id="user_admin" name="user[admin]" type="checkbox" value="true">
               </label>
             </div>
           </div>
           </form>
           """
  end

  test "setting the class" do
    html =
      render_surface do
        ~F"""
        <Checkbox form="user" field="admin" class="checkbox" />
        """
      end

    assert html =~ ~r/class="checkbox"/
  end

  test "setting multiple classes" do
    html =
      render_surface do
        ~F"""
        <Checkbox form="user" field="admin" class="checkbox primary" />
        """
      end

    assert html =~ ~r/class="checkbox primary"/
  end

  test "passing checked value" do
    html =
      render_surface do
        ~F"""
        <Checkbox form="user" field="admin" checked_value="admin" />
        """
      end

    assert html =~ """
           <input id="user_admin" name="user[admin]" type="checkbox" value="admin">
           """
  end

  test "setting the value" do
    html =
      render_surface do
        ~F"""
        <Checkbox value />
        """
      end

    assert html =~ ~r/checked/

    html =
      render_surface do
        ~F"""
        <Checkbox value={false} />
        """
      end

    refute html =~ ~r/checked/
  end

  test "setting the hidden_input" do
    html =
      render_surface do
        ~F"""
        <Checkbox hidden_input />
        """
      end

    assert html =~ ~r/hidden/

    #    html =
    #      render_surface do
    #        ~F"""
    #        <Checkbox hidden_input={false} />
    #        """
    #      end
    #
    #    refute html =~ ~r/hidden/
  end

  test "events with parent live view as target" do
    html =
      render_surface do
        ~F"""
        <Checkbox form="user" field="admin" click="my_click" />
        """
      end

    assert html =~ ~s(phx-click="my_click")
  end

  test "passing other options" do
    html =
      render_surface do
        ~F"""
        <Checkbox form="user" field="admin" opts={disabled: "disabled"} />
        """
      end

    assert html =~ """
           <input disabled="disabled" id="user_admin" name="user[admin]" type="checkbox" value="true">
           """
  end

  test "setting id and name through props" do
    html =
      render_surface do
        ~F"""
        <Checkbox form="user" field="admin" id="is_admin" name="is_admin" />
        """
      end

    assert html =~ """
           <input id="is_admin" name="is_admin" type="checkbox" value="true">
           """
  end

  test "setting the phx-value-* values" do
    html =
      render_surface do
        ~F"""
        <Checkbox form="user" field="admin" values={a: "one", b: :two, c: 3} />
        """
      end

    assert html =~ """
           <input id="user_admin" name="user[admin]" phx-value-a="one" phx-value-b="two" phx-value-c="3" type="checkbox" value="true">
           """
  end
end
