require 'test_helper'

class MemesControllerTest < ActionDispatch::IntegrationTest
  test '#index should deliver memes' do
    create_list(:meme, 10)

    get memes_url, params: {
      page: 1,
      per_page: 10
    }

    assert_response :success

    response_body = JSON.parse(response.body)
    assert_not response_body[0]['voted']
  end

  test '#index should deliver vote information when signed in' do
    user = create(:user)

    sign_in user
    create_list(:vote, 10, user: user)

    get memes_url, params: {
      page: 1,
      per_page: 10
    }

    assert_response :success

    response_body = JSON.parse(response.body)
    assert response_body[0]['voted']
  end

  test '#POST should create meme' do
    sign_in create(:user)

    assert_difference 'Meme.count', 1 do
      post memes_url, params: {
        file: fixture_file_upload('meme.png', 'image/png'),
        slot: rand(1..16)
      }

      assert_response :success
    end
  end
end
