dawarich/spec/requests/visits_spec.rb

104 lines
2.9 KiB
Ruby
Raw Permalink Normal View History

2024-07-27 06:35:47 -04:00
# frozen_string_literal: true
2024-07-24 14:25:16 -04:00
2024-07-27 06:35:47 -04:00
require 'rails_helper'
2024-07-24 14:25:16 -04:00
RSpec.describe '/visits', type: :request do
2024-07-27 06:35:47 -04:00
let(:user) { create(:user) }
2024-07-24 14:25:16 -04:00
2024-07-27 06:35:47 -04:00
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
sign_in user
end
2024-07-24 14:25:16 -04:00
describe 'GET /index' do
it 'renders a successful response' do
2024-07-24 14:25:16 -04:00
get visits_url
expect(response).to be_successful
end
2024-08-05 15:23:08 -04:00
context 'with confirmed visits' do
let!(:confirmed_visits) { create_list(:visit, 3, user:, status: :confirmed) }
it 'returns confirmed visits' do
get visits_url
2024-12-11 08:42:26 -05:00
expect(@controller.instance_variable_get(:@visits)).to match_array(confirmed_visits)
2024-08-05 15:23:08 -04:00
end
end
context 'with suggested visits' do
let!(:suggested_visits) { create_list(:visit, 3, user:, status: :suggested) }
it 'does not return suggested visits' do
get visits_url
2024-12-11 08:42:26 -05:00
expect(@controller.instance_variable_get(:@visits)).not_to include(suggested_visits)
2024-08-05 15:23:08 -04:00
end
it 'returns suggested visits' do
get visits_url, params: { status: 'suggested' }
2024-12-11 08:42:26 -05:00
expect(@controller.instance_variable_get(:@visits)).to match_array(suggested_visits)
2024-08-05 15:23:08 -04:00
end
end
context 'with declined visits' do
let!(:declined_visits) { create_list(:visit, 3, user:, status: :declined) }
it 'does not return declined visits' do
get visits_url
2024-12-11 08:42:26 -05:00
expect(@controller.instance_variable_get(:@visits)).not_to include(declined_visits)
2024-08-05 15:23:08 -04:00
end
it 'returns declined visits' do
get visits_url, params: { status: 'declined' }
2024-12-11 08:42:26 -05:00
expect(@controller.instance_variable_get(:@visits)).to match_array(declined_visits)
2024-08-05 15:23:08 -04:00
end
end
context 'with suggested visits' do
let!(:suggested_visits) { create_list(:visit, 3, user:, status: :suggested) }
it 'does not return suggested visits' do
get visits_url
2024-12-11 08:42:26 -05:00
expect(@controller.instance_variable_get(:@visits)).not_to include(suggested_visits)
2024-08-05 15:23:08 -04:00
end
it 'returns suggested visits' do
get visits_url, params: { status: 'suggested' }
2024-12-11 08:42:26 -05:00
expect(@controller.instance_variable_get(:@visits)).to match_array(suggested_visits)
2024-08-05 15:23:08 -04:00
end
end
2024-07-24 14:25:16 -04:00
end
2024-07-27 06:35:47 -04:00
describe 'PATCH /update' do
context 'with valid parameters' do
2024-08-05 15:23:08 -04:00
let(:visit) { create(:visit, user:, status: :suggested) }
2024-07-24 14:25:16 -04:00
2024-07-27 06:35:47 -04:00
it 'confirms the requested visit' do
patch visit_url(visit), params: { visit: { status: :confirmed } }
2024-07-24 14:25:16 -04:00
2024-07-27 06:35:47 -04:00
expect(visit.reload.status).to eq('confirmed')
2024-07-24 14:25:16 -04:00
end
2024-07-27 06:35:47 -04:00
it 'rejects the requested visit' do
patch visit_url(visit), params: { visit: { status: :declined } }
2024-07-24 14:25:16 -04:00
2024-07-27 06:35:47 -04:00
expect(visit.reload.status).to eq('declined')
2024-07-24 14:25:16 -04:00
end
it 'redirects to the visits index page' do
2024-07-27 06:35:47 -04:00
patch visit_url(visit), params: { visit: { status: :confirmed } }
2024-07-24 14:25:16 -04:00
expect(response).to redirect_to(visits_url(status: :suggested))
2024-07-24 14:25:16 -04:00
end
end
end
end