Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cdccollins committed Sep 19, 2024
1 parent 653279f commit 3901dd5
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
4 changes: 4 additions & 0 deletions test/application_system_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]

def assert_field_has_errors(label_text)
find_field(label_text).assert_ancestor(".input.field_with_errors")
end
end
14 changes: 14 additions & 0 deletions test/models/group_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "test_helper"

class GroupTest < ActiveSupport::TestCase
def setup
@subject = build(:group)
end

test "should be valid" do
assert @subject.valid?
end

test("name required") { assert_present(:name) }
test("age_in_months required") { assert_present(:age_in_months) }
end
19 changes: 19 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,23 @@ class UserTest < ActiveSupport::TestCase

assert_equal user.child_age_in_months_today, 5
end

test "#next_content method returns next ranked content for age group" do
group = create(:group, age_in_months: @subject.child_age_in_months_today)
content1 = create(:content, group:, position: 1)
content2 = create(:content, group:, position: 2)
create(:message, content: content1, user: @subject)

assert_equal @subject.next_content(group), content2
end

test "#next_content method returns nothing if no appropriate content" do
group = create(:group, age_in_months: @subject.child_age_in_months_today)
content1 = create(:content, group:, position: 1)
content2 = create(:content, group:, position: 2)
create(:message, content: content1, user: @subject)
create(:message, content: content2, user: @subject)

assert_nil @subject.next_content(group)
end
end
16 changes: 15 additions & 1 deletion test/system/contents_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,21 @@ class ContentsTest < ApplicationSystemTestCase
assert_text "New content"
end

test "updating a content" do
test "shows errors" do
create(:content, body: "Old Content", group: @group)

sign_in
visit group_path(@group)

click_on "Add new message"

click_on "Create"

assert_field_has_errors("Body")
assert_field_has_errors("Link")
end

test "updating content" do
create(:content, body: "Old Content", group: @group)

sign_in
Expand Down
60 changes: 60 additions & 0 deletions test/system/groups_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require "application_system_test_case"

class ContentsTest < ApplicationSystemTestCase
setup do
@admin = create(:admin)
end

test "creating new group" do
sign_in
visit groups_path

click_on "Create content group"

fill_in "Name", with: "Default content"
fill_in "Age in months", with: "17"
click_on "Create"

assert_text "Content group successfully created"
assert_text "Default content"
end

test "shows errors" do
sign_in
visit groups_path

click_on "Create content group"

click_on "Create"

assert_field_has_errors("Name")
assert_field_has_errors("Age in months")
end

test "updating a group" do
create(:group, name: "Old group name")

sign_in
visit groups_path

assert_text "Old group name"

click_on "Edit", match: :first

fill_in "Name", with: "New group name"
click_on "Update"

assert_text "Content group updated"
assert_text "New group name"
end

private

def sign_in
visit new_admin_session_path
fill_in "Email", with: @admin.email
fill_in "Password", with: @admin.password
click_on "Log in"
assert_text "Signed in successfully."
end
end

0 comments on commit 3901dd5

Please sign in to comment.