Skip to content
Snippets Groups Projects
Commit 7b1549fe authored by snaqvi's avatar snaqvi
Browse files

5.1 links TDD BDD

parent 0972414f
No related branches found
No related tags found
1 merge request!1115.1 links TDD BDD
Pipeline #47149 passed
Feature: Header Links Display
Scenario: User sees the correct header links when not logged in
Given the user is not logged in
When the user navigates to the home page
Then the page should display "Home", "Our Team", "Login", and "Register" links
And the page should not display a "Logout" link
Scenario: User sees the correct header links when logged in
Given the user is logged in
When the user navigates to the home page
Then the page should display "Home", "Our Team", and "Logout" links
And the page should not display "Login" or "Register" links
......@@ -90,4 +90,8 @@ suite name: "ef02_Social_Suite",
context: ProptrackerWeb.SocialMediaContext,
feature_paths: ["features/ef02_social.feature"]
suite name: "ef02_HeaderLinks_Suite",
context: ProptrackerWeb.HeaderLinksContext,
feature_paths: ["features/5.1_homelinks.feature"]
end
defmodule ProptrackerWeb.HeaderLinksContext do
use WhiteBread.Context
alias Proptracker.Repo
alias Proptracker.Accounts.User
import Phoenix.ConnTest
@moduledoc """
Context for testing header links display based on user login state.
"""
# Given the user is not logged in
given_ ~r/^the user is not logged in$/ do
fn state ->
conn = Phoenix.ConnTest.build_conn()
{:ok, Map.put(state, :conn, conn)}
end
end
# Given the user is logged in
given_ ~r/^the user is logged in$/ do
fn state ->
user_data = %{username: "testuser", password: "password123", password_confirmation: "password123"}
changeset = User.registration_changeset(%User{}, user_data)
{:ok, user} = Repo.insert(changeset)
conn = %Plug.Conn{} |> Plug.Test.init_test_session(user_id: user.id)
{:ok, Map.put(state, :user, user) |> Map.put(state, :conn, conn)}
end
end
# When the user navigates to the home page
when_ ~r/^the user navigates to the home page$/ do
fn state ->
conn = state[:conn]
response = get(conn, "/")
{:ok, Map.put(state, :response, response)}
end
end
# Then the page should display "Home", "Our Team", "Login", and "Register" links
then_ ~r/^the page should display "Home", "Our Team", "Login", and "Register" links$/ do
fn state ->
response = state[:response]
html = html_response(response, 200)
assert html =~ "Home"
assert html =~ "Our Team"
assert html =~ "Login"
assert html =~ "Register"
{:ok, state}
end
end
# Then the page should not display a "Logout" link
then_ ~r/^the page should not display a "Logout" link$/ do
fn state ->
response = state[:response]
html = html_response(response, 200)
refute html =~ "Logout"
{:ok, state}
end
end
# Then the page should display "Home", "Our Team", and "Logout" links
then_ ~r/^the page should display "Home", "Our Team", and "Logout" links$/ do
fn state ->
response = state[:response]
html = html_response(response, 200)
assert html =~ "Home"
assert html =~ "Our Team"
assert html =~ "Logout"
{:ok, state}
end
end
# Then the page should not display "Login" or "Register" links
then_ ~r/^the page should not display "Login" or "Register" links$/ do
fn state ->
response = state[:response]
html = html_response(response, 200)
refute html =~ "Login"
refute html =~ "Register"
{:ok, state}
end
end
end
defmodule ProptrackerWeb.HomeLinkTest do
use ProptrackerWeb.ConnCase
import Floki
describe "GET /" do
test "renders the expected links in the header", %{conn: conn} do
conn = get(conn, "/") # Get the home page ("/")
# Parse the HTML response
html = html_response(conn, 200)
{:ok, parsed_html} = Floki.parse_document(html)
# Check if the header contains the expected links
assert Floki.find(parsed_html, "a[href='/']") != []
assert Floki.find(parsed_html, "a[href='/team']") != []
assert Floki.find(parsed_html, "a[href='/login']") != []
assert Floki.find(parsed_html, "a[href='/register']") != []
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment