From 91b68cce664c378b05d3bae851ce6fe9ab6c8e01 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Thu, 28 Jun 2018 17:23:33 -0400 Subject: [PATCH] Modify tests to account for new design --- .../test/explorer/chain/statistics_test.exs | 4 +- .../templates/address/_link.html.eex | 2 +- .../templates/chain/_blocks.html.eex | 2 +- .../templates/chain/_transactions.html.eex | 4 +- .../lib/explorer_web/views/address_view.ex | 8 +- .../explorer_web/views/transaction_view.ex | 119 +++++++++--------- .../explorer_web/features/pages/home_page.ex | 2 +- .../features/viewing_blocks_test.exs | 2 +- 8 files changed, 74 insertions(+), 69 deletions(-) diff --git a/apps/explorer/test/explorer/chain/statistics_test.exs b/apps/explorer/test/explorer/chain/statistics_test.exs index ba8a45a239..4a4544880e 100644 --- a/apps/explorer/test/explorer/chain/statistics_test.exs +++ b/apps/explorer/test/explorer/chain/statistics_test.exs @@ -54,11 +54,11 @@ defmodule Explorer.Chain.StatisticsTest do end test "returns the last five blocks" do - insert_list(6, :block) + insert_list(5, :block) statistics = Statistics.fetch() - assert statistics.blocks |> Enum.count() == 5 + assert statistics.blocks |> Enum.count() == 4 end test "returns the last five transactions with blocks" do diff --git a/apps/explorer_web/lib/explorer_web/templates/address/_link.html.eex b/apps/explorer_web/lib/explorer_web/templates/address/_link.html.eex index 6f90b258e7..5297edad0f 100644 --- a/apps/explorer_web/lib/explorer_web/templates/address/_link.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/address/_link.html.eex @@ -11,6 +11,6 @@ <% else %> <%= link to: address_path(ExplorerWeb.Endpoint, :show, @locale, @address_hash), "data-address-hash": @address_hash do %> <%= @address_hash %> - <%= @address |> hash() |> String.slice(0..5) %>–<%= @address |> hash() |> String.slice(-6..-1) %> + <%= to_string(@address_hash) |> String.slice(0..5) %>–<%= to_string(@address_hash) |> String.slice(-6..-1) %> <% end %> <% end %> diff --git a/apps/explorer_web/lib/explorer_web/templates/chain/_blocks.html.eex b/apps/explorer_web/lib/explorer_web/templates/chain/_blocks.html.eex index 2eb4eb4274..d8f5346434 100644 --- a/apps/explorer_web/lib/explorer_web/templates/chain/_blocks.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/chain/_blocks.html.eex @@ -8,7 +8,7 @@ <%= for block <- @chain.blocks do %>
-
+
<%= link(block, to: block_path(@conn, :show, @conn.assigns.locale, block), class: "tile-title") %>
<%= block.transactions |> Enum.count %> Transactions diff --git a/apps/explorer_web/lib/explorer_web/templates/chain/_transactions.html.eex b/apps/explorer_web/lib/explorer_web/templates/chain/_transactions.html.eex index b1dd37eba3..4c478ab400 100644 --- a/apps/explorer_web/lib/explorer_web/templates/chain/_transactions.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/chain/_transactions.html.eex @@ -3,8 +3,8 @@ <%= link(gettext("View All Transactions →"), to: transaction_path(@conn, :index, Gettext.get_locale), class: "button button--secondary button--xsmall float-right") %>

<%= gettext "Transactions" %>

<%= for transaction <- @chain.transactions do %> -
-
+
+
<%= ExplorerWeb.TransactionView.transaction_display_type(transaction) %>
diff --git a/apps/explorer_web/lib/explorer_web/views/address_view.ex b/apps/explorer_web/lib/explorer_web/views/address_view.ex index dadaa7e9e7..0e657c0902 100644 --- a/apps/explorer_web/lib/explorer_web/views/address_view.ex +++ b/apps/explorer_web/lib/explorer_web/views/address_view.ex @@ -8,9 +8,6 @@ defmodule ExplorerWeb.AddressView do @dialyzer :no_match - def contract?(%Address{contract_code: nil}), do: false - def contract?(%Address{contract_code: _}), do: true - def address_title(%Address{} = address) do if contract?(address) do gettext("Contract Address") @@ -22,7 +19,6 @@ defmodule ExplorerWeb.AddressView do @doc """ Returns a formatted address balance and includes the unit. """ - def balance(%Address{fetched_balance: nil}), do: "" def balance(%Address{fetched_balance: balance}) do @@ -35,6 +31,10 @@ defmodule ExplorerWeb.AddressView do to_string(fetched_balance_block_number) end + def contract?(%Address{contract_code: nil}), do: false + + def contract?(%Address{contract_code: _}), do: true + def formatted_usd(%Address{fetched_balance: nil}, _), do: nil def formatted_usd(%Address{fetched_balance: balance}, %Token{} = exchange_rate) do diff --git a/apps/explorer_web/lib/explorer_web/views/transaction_view.ex b/apps/explorer_web/lib/explorer_web/views/transaction_view.ex index 59ceeb0b94..d23ab0e115 100644 --- a/apps/explorer_web/lib/explorer_web/views/transaction_view.ex +++ b/apps/explorer_web/lib/explorer_web/views/transaction_view.ex @@ -10,26 +10,12 @@ defmodule ExplorerWeb.TransactionView do import ExplorerWeb.Gettext - def contract_creation?(%Transaction{created_contract_address_hash: nil}), do: false - def contract_creation?(_), do: true - - def contract?(%Transaction{from_address: from_address, to_address: to_address}) do - AddressView.contract?(from_address) || AddressView.contract?(to_address) - end - - def tile_class(%Transaction{} = transaction) do - cond do - contract_creation?(transaction) -> "tile-type-contract-creation" - contract?(transaction) -> "tile-type-contract" - true -> "tile-type-transaction" - end - end + defguardp is_transaction_type(mod) when mod in [InternalTransaction, Transaction] - def transaction_display_type(%Transaction{} = transaction) do - cond do - contract_creation?(transaction) -> gettext("Contract Creation") - contract?(transaction) -> gettext("Contract") - true -> gettext("Transaction") + def confirmations(%Transaction{block: block}, named_arguments) when is_list(named_arguments) do + case block do + nil -> 0 + _ -> Chain.confirmations(block, named_arguments) end end @@ -39,11 +25,15 @@ defmodule ExplorerWeb.TransactionView do def display_to_address(%Transaction{to_address: address}), do: [address: address] - def confirmations(%Transaction{block: block}, named_arguments) when is_list(named_arguments) do - case block do - nil -> 0 - _ -> Chain.confirmations(block, named_arguments) - end + def formatted_fee(%Transaction{} = transaction, opts) do + transaction + |> Chain.fee(:wei) + |> fee_to_currency(opts) + |> case do + {_, nil} -> nil + {:actual, value} -> value + {:maximum, value} -> "<= " <> value + end end def gas_used(%Transaction{gas_used: nil}), do: gettext("Pending") @@ -52,17 +42,18 @@ defmodule ExplorerWeb.TransactionView do Number.to_string!(gas_used) end - def formatted_fee(%Transaction{} = transaction, opts) do - transaction - |> Chain.fee(:wei) - |> fee_to_currency(opts) - |> case do - {_, nil} -> nil - {:actual, value} -> value - {:maximum, value} -> "<= " <> value - end + def involves_contract?(%Transaction{from_address: from_address, to_address: to_address}) do + AddressView.contract?(from_address) || AddressView.contract?(to_address) end + def involves_contract_creation?(%Transaction{created_contract_address_hash: nil}), do: false + + def involves_contract_creation?(_), do: true + + def contract_creation?(%Transaction{created_contract_address_hash: nil}), do: false + + def contract_creation?(_), do: true + def qr_code(%Transaction{hash: hash}) do hash |> to_string() @@ -70,24 +61,21 @@ defmodule ExplorerWeb.TransactionView do |> Base.encode64() end - defp fee_to_currency({fee_type, fee}, denomination: denomination) do - {fee_type, format_wei_value(Wei.from(fee, :wei), denomination)} - end - - defp fee_to_currency({fee_type, fee}, exchange_rate: %Token{} = exchange_rate) do - formatted = - fee - |> Wei.from(:wei) - |> USD.from(exchange_rate) - |> format_usd_value() - - {fee_type, formatted} - end - def format_gas_limit(gas) do Number.to_string!(gas) end + def formatted_status(transaction) do + transaction + |> Chain.transaction_to_status() + |> case do + :failed -> gettext("Failed") + :out_of_gas -> gettext("Out of Gas") + :pending -> gettext("Pending") + :success -> gettext("Success") + end + end + def formatted_usd_value(%Transaction{value: nil}, _token), do: nil def formatted_usd_value(%Transaction{value: value}, token) do @@ -96,8 +84,6 @@ defmodule ExplorerWeb.TransactionView do defdelegate formatted_timestamp(block), to: BlockView - defguardp is_transaction_type(mod) when mod in [InternalTransaction, Transaction] - def gas(%type{gas: gas}) when is_transaction_type(type) do Cldr.Number.to_string!(gas) end @@ -117,14 +103,19 @@ defmodule ExplorerWeb.TransactionView do Chain.transaction_to_status(transaction) end - def formatted_status(transaction) do - transaction - |> Chain.transaction_to_status() - |> case do - :failed -> gettext("Failed") - :out_of_gas -> gettext("Out of Gas") - :pending -> gettext("Pending") - :success -> gettext("Success") + def type_suffix(%Transaction{} = transaction) do + cond do + involves_contract_creation?(transaction) -> "contract-creation" + involves_contract?(transaction) -> "contract" + true -> "transaction" + end + end + + def transaction_display_type(%Transaction{} = transaction) do + cond do + involves_contract_creation?(transaction) -> gettext("Contract Creation") + involves_contract?(transaction) -> gettext("Contract") + true -> gettext("Transaction") end end @@ -139,4 +130,18 @@ defmodule ExplorerWeb.TransactionView do include_label? = Keyword.get(opts, :include_label, true) format_wei_value(value, :ether, include_unit_label: include_label?) end + + defp fee_to_currency({fee_type, fee}, denomination: denomination) do + {fee_type, format_wei_value(Wei.from(fee, :wei), denomination)} + end + + defp fee_to_currency({fee_type, fee}, exchange_rate: %Token{} = exchange_rate) do + formatted = + fee + |> Wei.from(:wei) + |> USD.from(exchange_rate) + |> format_usd_value() + + {fee_type, formatted} + end end diff --git a/apps/explorer_web/test/explorer_web/features/pages/home_page.ex b/apps/explorer_web/test/explorer_web/features/pages/home_page.ex index affe0b970a..804bba10e3 100644 --- a/apps/explorer_web/test/explorer_web/features/pages/home_page.ex +++ b/apps/explorer_web/test/explorer_web/features/pages/home_page.ex @@ -12,7 +12,7 @@ defmodule ExplorerWeb.HomePage do end def contract_creation(%InternalTransaction{created_contract_address_hash: hash}) do - css("[data-address-hash='#{hash}']", text: "Contract Creation") + css("[data-test='contract-creation'] [data-address-hash='#{hash}']") end def search(session, text) do diff --git a/apps/explorer_web/test/explorer_web/features/viewing_blocks_test.exs b/apps/explorer_web/test/explorer_web/features/viewing_blocks_test.exs index bc0c465544..01d43f6f77 100644 --- a/apps/explorer_web/test/explorer_web/features/viewing_blocks_test.exs +++ b/apps/explorer_web/test/explorer_web/features/viewing_blocks_test.exs @@ -23,7 +23,7 @@ defmodule ExplorerWeb.ViewingBlocksTest do test "viewing blocks on the home page", %{session: session} do session |> HomePage.visit_page() - |> assert_has(HomePage.blocks(count: 5)) + |> assert_has(HomePage.blocks(count: 4)) end test "search for blocks from home page", %{session: session} do