<commit_before>require 'test_helper'

class FollowingTest < ActionDispatch::IntegrationTest
  def setup
    @user = users(:gai)
    log_in_as(@user)
  end

  # test for following/follower page (not comprehensive since we want to make sure it's working)
  # 1. get following for the specific user
  # 2. verify the user following list is not empty
  # 3. verify "following" word is on the page
  # 4. verify following list count is in the body
  # 5. verify the link for each item in the following list is active
  test "following page" do
    get following_user_path(@user)  #1
    assert_not @user.following.empty?   #2
    assert_match "Following", response.body   #3
    assert_match @user.following.count.to_s, response.body  #4
    @user.following.each { |user| assert_select "a[href=?]", user_path(user)}   #5
  end

  # test for followers page
  test "follower page" do
    get followers_user_path(@user)
    assert_not @user.followers.empty?
    assert_match "Followers", response.body
    assert_match @user.followers.count.to_s, response.body
    @user.followers.each { |user| assert_select "a[href=?]", user_path(user)}
  end
end<commit_msg>add integration test for ajax feature of follow/unfollow button<commit_after>require 'test_helper'

class FollowingTest < ActionDispatch::IntegrationTest
  def setup
    @user = users(:gai)
    @third_user = users(:third_user)  # third user has not been followed by @user in relationships fixture
    log_in_as(@user)
  end

  # test for following/follower page (not comprehensive since we want to make sure it's working)
  # 1. get following for the specific user
  # 2. verify the user following list is not empty
  # 3. verify "following" word is on the page
  # 4. verify following list count is in the body
  # 5. verify the link for each item in the following list is active
  test "following page" do
    get following_user_path(@user)  #1
    assert_not @user.following.empty?   #2
    assert_match "Following", response.body   #3
    assert_match @user.following.count.to_s, response.body  #4
    @user.following.each { |user| assert_select "a[href=?]", user_path(user)}   #5
  end

  # test for followers page
  test "follower page" do
    get followers_user_path(@user)
    assert_not @user.followers.empty?
    assert_match "Followers", response.body
    assert_match @user.followers.count.to_s, response.body
    @user.followers.each { |user| assert_select "a[href=?]", user_path(user)}
  end

  # follow/unfollow without Ajax

  test "should follow the user via html (no js or js is disabled)" do
    assert_difference "@user.following.count", 1 do
      post relationships_path, followed_id: @third_user.id
    end
  end

  # follow another user
  # define the relationship
  # verify the count is 1 less
  test "should unfollow the user via html (no js or js is disabled)" do
    @user.follow(@third_user)
    relationship = @user.active_relationships.find_by(followed_id: @third_user.id)
    assert_difference '@user.following.count', -1 do
      delete(relationship_path(relationship))
    end
  end

  # follow/unfollow with Ajax

  test "should follow the user via Ajax call" do
    assert_difference "@user.following.count", 1 do
      xhr(:post, relationships_path, followed_id: @third_user.id)
    end
  end

  test "should unfollow the user via Ajax call" do
    @user.follow(@third_user)
    relationship = @user.active_relationships.find_by(followed_id: @third_user.id)
    assert_difference '@user.following.count', -1 do
      xhr(:delete, relationship_path(relationship))
    end
  end
end
{'fraction_non_alphanumeric': 0.08991596638655462, 'fraction_numerical': 0.0070028011204481795, 'mean_word_length': 3.7613333333333334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1061748', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1079, 'n_words': 369}