-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from naymspace/feature/tests
Tests
- Loading branch information
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
defmodule Backpex.Controllers.CookieControllerTest do | ||
use ExUnit.Case, async: true | ||
|
||
import Plug.Conn | ||
import Phoenix.ConnTest | ||
|
||
alias Backpex.CookieController | ||
|
||
setup do | ||
conn = | ||
build_conn() | ||
|> Plug.Test.init_test_session(%{}) | ||
|> fetch_session() | ||
|
||
{:ok, conn: conn} | ||
end | ||
|
||
describe "update/2 with toggle_columns" do | ||
test "updates session and redirects", %{conn: conn} do | ||
params = %{ | ||
"toggle_columns" => %{ | ||
"_cookie_redirect_url" => "/redirect", | ||
"_resource" => "users", | ||
"column1" => "true", | ||
"column2" => "false" | ||
} | ||
} | ||
|
||
conn = CookieController.update(conn, params) | ||
|
||
assert redirected_to(conn) == "/redirect" | ||
|
||
assert get_session(conn, "backpex") == %{ | ||
"column_toggle" => %{ | ||
"users" => %{"column1" => "true", "column2" => "false"} | ||
} | ||
} | ||
end | ||
end | ||
|
||
describe "update/2 with toggle_metrics" do | ||
test "toggles metric visibility to false when not set", %{conn: conn} do | ||
params = %{ | ||
"toggle_metrics" => %{ | ||
"_cookie_redirect_url" => "/redirect", | ||
"_resource" => "users" | ||
} | ||
} | ||
|
||
conn = CookieController.update(conn, params) | ||
|
||
assert redirected_to(conn) == "/redirect" | ||
|
||
assert get_session(conn, "backpex") == %{ | ||
"metric_visibility" => %{"users" => false} | ||
} | ||
end | ||
|
||
test "toggles metric visibility to true when already set to false", %{conn: conn} do | ||
conn = put_session(conn, "backpex", %{"metric_visibility" => %{"users" => false}}) | ||
|
||
params = %{ | ||
"toggle_metrics" => %{ | ||
"_cookie_redirect_url" => "/redirect", | ||
"_resource" => "users" | ||
} | ||
} | ||
|
||
conn = CookieController.update(conn, params) | ||
|
||
assert redirected_to(conn) == "/redirect" | ||
|
||
assert get_session(conn, "backpex") == %{ | ||
"metric_visibility" => %{"users" => true} | ||
} | ||
end | ||
end | ||
|
||
describe "update/2 with select_theme" do | ||
test "updates theme in session", %{conn: conn} do | ||
params = %{"select_theme" => "dark"} | ||
|
||
conn = CookieController.update(conn, params) | ||
|
||
assert json_response(conn, 200) == %{} | ||
assert get_session(conn, "backpex") == %{"theme" => "dark"} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Plugs.ThemeSelectorPlugTest do | ||
use ExUnit.Case, async: true | ||
use Plug.Test | ||
|
||
alias Backpex.ThemeSelectorPlug | ||
|
||
describe "ThemeSelectorPlug" do | ||
test "assigns theme from session" do | ||
conn = | ||
conn(:get, "/") | ||
|> init_test_session(%{"backpex" => %{"theme" => "dark"}}) | ||
|> fetch_session() | ||
|
||
conn = ThemeSelectorPlug.call(conn, []) | ||
|
||
assert conn.assigns.theme == "dark" | ||
end | ||
|
||
test "assigns nil when no theme in session" do | ||
conn = | ||
conn(:get, "/") | ||
|> init_test_session(%{}) | ||
|> fetch_session() | ||
|
||
conn = ThemeSelectorPlug.call(conn, []) | ||
|
||
assert conn.assigns.theme == nil | ||
end | ||
|
||
test "assigns nil when backpex key exists but theme key doesn't" do | ||
conn = | ||
conn(:get, "/") | ||
|> init_test_session(%{"backpex" => %{}}) | ||
|> fetch_session() | ||
|
||
conn = ThemeSelectorPlug.call(conn, []) | ||
|
||
assert conn.assigns.theme == nil | ||
end | ||
|
||
test "init function returns the default value" do | ||
default = "some default value" | ||
assert ThemeSelectorPlug.init(default) == default | ||
end | ||
end | ||
end |