Skip to content

Commit

Permalink
added description of rendering logic tests to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRund committed Nov 28, 2024
1 parent 49b2b88 commit 96e8159
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
128 changes: 128 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,138 @@ end
```

## Deletion
The deletion test suite is testing behavior involved in removing
data from the screen, either with the `backspace` or `clear` operator.

### Backspace

We test two behaviors (branches) of our `backspace` logic, what happens after
we click backspace when the calculator is in "display" mode and when it is not
(i.e when it is in "number" or "operator" mode).

Clearly, when not in "display" mode we'd like the the `backspace` event to
remove the last digit of whatever is currently on the screen. After using
our `apply_sequence()` with numbers `3`, `2`, `1` we can just check that the
`1` was removed with:

```elixir
render_click(view, "backspace")

assert render(view) =~ ~s(<div id="screen" class="mr-4">32</div>)
```

And then for "display" mode the backspace event shouldn't do anything,
so we just assert that the result of the calculation is still present
after the `apply_sequence` followed by the `equals` event:

```elixir
render_click(view, "backspace")

# nothing should happen
assert render(view) =~ "1.0"
```

### Clear

To test the `clear` event, all we need to do is trigger a `number` event
followed by the `clear` event and then assert whether the screen is empty:

```elixir
render_click(view, "number", number: "1")
render_click(view, "clear")

# screen should be empty
assert render(view) =~ ~s(<div id="screen" class="mr-4"></div>)
```

## Rendering logic

This test suite is for testing the behavior ensuring legal calculations
are being typed (e.g stopping a calculation like `1+-+=`). We'll quickly
examine each test.

```elixir
test "number after equals", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")

apply_sequence([
%{event: "number", value: "1"},
%{event: "operator", value: "/"},
%{event: "number", value: "1"}
], view, true)
render_click(view, "number", %{number: "2"})

assert render(view) =~ ~s(<div id="screen" class="mr-4">2</div>)
end
```
Here we are checking that when entering a number after the calculator
is displaying a result a new calculation (`calc` string) is started.
So when we use `render_click(view, "number", %{number: "2"})` after
calculating the sequence the screen should only contain the `2`.

Next we test the behavior of two operators being clicked in a row:

```elixir
test "operator after operator", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")

apply_sequence([
%{event: "number", value: "1"},
%{event: "operator", value: "+"},
%{event: "operator", value: "-"}
], view, false)

# new operator replaces old operator
assert render(view) =~ "1-"
end
```

As we have seen, in this design the desired action is to replace the
first operator with the second, so we simply assert that all that is
being displayed is `1-` after the sequence `1`, `+`, `-`.

The last two tests are examining the behavior of an operator _followed_
by an equals sign (e.g. `1+=`) and the operator _following_ a result
(e.g. `1+1=+1).

In each case nothing should happen:

```elixir
test "operator with equals", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")

apply_sequence([
%{event: "number", value: "1"},
%{event: "operator", value: "/"},
%{event: "number", value: "1"}
], view, true)
render_click(view, "operator", operator: "+")

# nothing should happen
assert render(view) =~ "1.0"
end
```
So we assert that the result is still being displayed after inputting
an operator, and..

```elixir
test "equals after operator", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")

apply_sequence([
%{event: "number", value: "1"},
%{event: "operator", value: "+"}
], view, true)

# nothing should happen
assert render(view) =~ "1+"
end
```

We check that the `equals` event (which we triggered by passing
`true` to the helper function) does nothing.


## Brackets


2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"file_system": {:hex, :file_system, "1.0.1", "79e8ceaddb0416f8b8cd02a0127bdbababe7bf4a23d2a395b983c1f8b3f73edd", [:mix], [], "hexpm", "4414d1f38863ddf9120720cd976fce5bdde8e91d8283353f0e31850fa89feb9e"},
"floki": {:hex, :floki, "0.36.3", "1102f93b16a55bc5383b85ae3ec470f82dee056eaeff9195e8afdf0ef2a43c30", [:mix], [], "hexpm", "fe0158bff509e407735f6d40b3ee0d7deb47f3f3ee7c6c182ad28599f9f6b27a"},
"hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"},
"heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized"]},
"heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized", depth: 1]},
"hpax": {:hex, :hpax, "1.0.0", "28dcf54509fe2152a3d040e4e3df5b265dcb6cb532029ecbacf4ce52caea3fd2", [:mix], [], "hexpm", "7f1314731d711e2ca5fdc7fd361296593fc2542570b3105595bb0bc6d0fad601"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
Expand Down
8 changes: 6 additions & 2 deletions test/phx_calculator_web/live/calculator_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ defmodule PhxCalculatorWeb.CalculatorLiveTest do
test "backspace with number", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")

render_click(view, "number", %{number: "321"})
apply_sequence([
%{event: "number", value: "3"},
%{event: "number", value: "2"},
%{event: "number", value: "1"}
], view, false)
render_click(view, "backspace")

assert render(view) =~ ~s(<div id="screen" class="mr-4">32</div>)
Expand All @@ -171,7 +175,7 @@ defmodule PhxCalculatorWeb.CalculatorLiveTest do
%{event: "operator", value: "/"},
%{event: "number", value: "1"}
], view, true)
render_click(view, "backspace")
render_click(view, "backspace")

# nothing should happen
assert render(view) =~ "1.0"
Expand Down

0 comments on commit 96e8159

Please sign in to comment.