-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22e82c1
commit 6fc4cbe
Showing
3 changed files
with
118 additions
and
30 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
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,85 @@ | ||
defmodule CrawlyUIWeb.ItemViewTest do | ||
import CrawlyUI.DataCase | ||
use CrawlyUIWeb.ConnCase | ||
|
||
import Phoenix.View | ||
import Ecto.Query | ||
|
||
describe "render jobs/:job_id/items/index.html" do | ||
test "shows list items" do | ||
%{id: job_id} = insert_job() | ||
insert_item(job_id, nil, %{"field_1" => "data_1_1", "field_2" => "data_2_1"}) | ||
insert_item(job_id, nil, %{"field_1" => "data_1_2", "field_2" => "data_2_2"}) | ||
|
||
query = from i in CrawlyUI.Manager.Item, where: i.job_id == ^job_id | ||
items = CrawlyUI.Repo.all(query) | ||
page = CrawlyUI.Repo.paginate(query, %{}) | ||
|
||
params = [items: items, page: page, search: nil] | ||
|
||
rendered_string = render_to_string(CrawlyUIWeb.ItemView, "index.html", params) | ||
|
||
assert rendered_string =~ | ||
"<td class=\"w\"><b>field_1</b></td>\n <td class=\"c\">data_1_1</td>" | ||
|
||
assert rendered_string =~ | ||
"<td class=\"w\"><b>field_2</b></td>\n <td class=\"c\">data_2_1</td>" | ||
|
||
assert rendered_string =~ | ||
"<td class=\"w\"><b>field_1</b></td>\n <td class=\"c\">data_1_2</td>" | ||
|
||
assert rendered_string =~ | ||
"<td class=\"w\"><b>field_2</b></td>\n <td class=\"c\">data_2_2</td>" | ||
end | ||
|
||
test " with field value of list type in item data" do | ||
params = index_params(%{"list" => ["data_1", "data_2"]}) | ||
|
||
assert render_to_string(CrawlyUIWeb.ItemView, "index.html", params) =~ | ||
"<td class=\"c\">data_2data_1</td>" | ||
end | ||
|
||
test " with image field in item data" do | ||
params = index_params(%{"image" => "image_src"}) | ||
|
||
assert render_to_string(CrawlyUIWeb.ItemView, "index.html", params) =~ | ||
"<td class=\"c\"><img width='150px' src='image_src' /></td>" | ||
end | ||
|
||
test " with url field in item data" do | ||
params = index_params(%{"url" => "https://example.com"}) | ||
|
||
rendered_string = render_to_string(CrawlyUIWeb.ItemView, "index.html", params) | ||
|
||
assert rendered_string =~ | ||
"<td class=\"c\"><a target='blank' href='https://example.com'>https://example.com</a></td>" | ||
|
||
assert rendered_string =~ | ||
~r/<th>Item<\/th>\n <th>Discovery time: (.)* \[<a href='\/jobs\/(.)*\/items\/(.)*'> Preview <\/a>\]<\/th>/ | ||
end | ||
|
||
test "with empty search string" do | ||
params = index_params(%{"field" => "value"}, nil) | ||
|
||
assert render_to_string(CrawlyUIWeb.ItemView, "index.html", params) =~ | ||
"<div class=\"column\"><input type=\"text\" placeholder=\"Search\" name=\"search\"></div>" | ||
end | ||
|
||
test "with search param" do | ||
params = index_params(%{"field" => "value"}, "search string") | ||
|
||
assert render_to_string(CrawlyUIWeb.ItemView, "index.html", params) =~ | ||
"<div class=\"column\"><input type=\"text\" placeholder=\"search string\" name=\"search\"></div>" | ||
end | ||
end | ||
|
||
defp index_params(data, search \\ nil) do | ||
%{id: job_id} = insert_job() | ||
insert_item(job_id, nil, data) | ||
query = from i in CrawlyUI.Manager.Item, where: i.job_id == ^job_id | ||
items = CrawlyUI.Repo.all(query) | ||
page = CrawlyUI.Repo.paginate(query, %{}) | ||
|
||
[items: items, page: page, search: search] | ||
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