Skip to content
Snippets Groups Projects
Commit d25afd6c authored by Taico Aerts's avatar Taico Aerts
Browse files

Add basic post creation test

parent 27a3c09b
Branches
Tags
2 merge requests!51Upgrade to latest qpixel,!50Update to latest codidact changes
...@@ -62,4 +62,42 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase ...@@ -62,4 +62,42 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
users(user_or_fixture) users(user_or_fixture)
end end
end end
# In the post form, this method will select the given tag.
#
# @param tag_name [String] the name of the tag
# @param create_new [Boolean] whether creating a new tag is allowed (default false)
def post_form_select_tag(tag_name, create_new = false)
# First enter the tag name into the select2 search field for the tag
within find_field('Tags (at least one):').find(:xpath, '..') do
find('.select2-search__field').fill_in(with: tag_name)
end
# Get the first item listed that is not the "Searching..." item
first_option = find('#select2-post_tags_cache-results li:first-child') { |el| el.text != 'Searching…' }
if first_option.first('span').text == tag_name
# If the text matches the tag name, first check whether we are creating a new tag.
# If so, confirm that we are allowed to. If all is good, actually click on the item.
if create_new || !first_option.text.include?('Create new tag')
first_option.click
else
raise "Expected to find tag with the name #{tag_name}, " \
'but could not select it from options without creating a new tag.'
end
elsif create_new
# The first item returned is not the tag we were looking for (another tag partial match + not existing)
# If we are allowed to create a tag, select the last option from the list, which is always the tag creation.
last_option = find('#select2-post_tags_cache-results li:last-child')
if last_option.first('span').text == tag_name
last_option.click
else
raise "Tried to select tag #{tag_name} for creation, but it does not seem to be a presented option."
end
else
# The first item returned is not the tag we were looking for, and we are not allowed to create a tag.
raise "Expected to find tag with the name #{tag_name}, " \
'but could not select it from options without creating a new tag.'
end
end
end end
require 'application_system_test_case'
class PostTest < ApplicationSystemTestCase
test 'Not-signed in user cannot create a post' do
visit root_url
click_on 'Create Post'
assert_current_path new_user_session_url
end
test 'Signed in user can create a post' do
category = categories(:meta)
log_in :standard_user
visit category_path(category)
click_on 'Create Post'
fill_in 'Body', with: "When running QPixel, users are generally supposed to be able to create posts.\n" \
'Does that actually work?'
fill_in 'Summarize your post with a title:', with: 'Can a signed-in user create a post?'
post_form_select_tag tags(:faq).name
click_on "Save Post in #{category.name}a"
end
# TODO: Post validations
# TODO: Sort urls
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment