From 9266600aa79951e7405e6f93f592ff57ee1c1f10 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 20 Dec 2024 11:13:58 -0500 Subject: [PATCH] Convert `admin/rules` spec controller->system --- .../admin/rules_controller_spec.rb | 85 ------------------- spec/system/admin/rules_spec.rb | 83 ++++++++++++++++++ spec/system/admin/webhooks_spec.rb | 2 +- 3 files changed, 84 insertions(+), 86 deletions(-) delete mode 100644 spec/controllers/admin/rules_controller_spec.rb create mode 100644 spec/system/admin/rules_spec.rb diff --git a/spec/controllers/admin/rules_controller_spec.rb b/spec/controllers/admin/rules_controller_spec.rb deleted file mode 100644 index 1b2a2010d0..0000000000 --- a/spec/controllers/admin/rules_controller_spec.rb +++ /dev/null @@ -1,85 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Admin::RulesController do - render_views - - let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) } - - before do - sign_in user, scope: :user - end - - describe 'GET #index' do - it 'returns http success' do - get :index - - expect(response).to have_http_status(:success) - end - end - - describe 'GET #edit' do - let(:rule) { Fabricate(:rule) } - - it 'returns http success and renders edit' do - get :edit, params: { id: rule.id } - - expect(response).to have_http_status(:success) - expect(response).to render_template(:edit) - end - end - - describe 'POST #create' do - context 'with valid data' do - it 'creates a new rule and redirects' do - expect do - post :create, params: { rule: { text: 'The rule text.' } } - end.to change(Rule, :count).by(1) - - expect(response).to redirect_to(admin_rules_path) - end - end - - context 'with invalid data' do - it 'does creates a new rule and renders index' do - expect do - post :create, params: { rule: { text: '' } } - end.to_not change(Rule, :count) - - expect(response).to render_template(:index) - end - end - end - - describe 'PUT #update' do - let(:rule) { Fabricate(:rule, text: 'Original text') } - - context 'with valid data' do - it 'updates the rule and redirects' do - put :update, params: { id: rule.id, rule: { text: 'Updated text.' } } - - expect(response).to redirect_to(admin_rules_path) - end - end - - context 'with invalid data' do - it 'does not update the rule and renders index' do - put :update, params: { id: rule.id, rule: { text: '' } } - - expect(response).to render_template(:edit) - end - end - end - - describe 'DELETE #destroy' do - let!(:rule) { Fabricate(:rule) } - - it 'destroys the rule and redirects' do - delete :destroy, params: { id: rule.id } - - expect(rule.reload).to be_discarded - expect(response).to redirect_to(admin_rules_path) - end - end -end diff --git a/spec/system/admin/rules_spec.rb b/spec/system/admin/rules_spec.rb new file mode 100644 index 0000000000..4718909ab0 --- /dev/null +++ b/spec/system/admin/rules_spec.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Admin Rules' do + describe 'Managing rules' do + before { sign_in Fabricate(:user, role: UserRole.find_by(name: 'Admin')) } + + describe 'Viewing rules' do + let!(:rule) { Fabricate :rule, text: 'This is a rule' } + + it 'lists existing records' do + visit admin_rules_path + + expect(page) + .to have_content(I18n.t('admin.rules.title')) + .and have_content(rule.text) + + click_on(rule.text) + expect(page) + .to have_content(I18n.t('admin.rules.title')) + end + end + + describe 'Creating a new rule' do + it 'creates new record with valid attributes' do + visit admin_rules_path + + # Invalid submission + fill_in 'rule_text', with: '' + expect { submit_form } + .to_not change(Rule, :count) + expect(page) + .to have_content(/error below/) + + # Valid submission + fill_in 'rule_text', with: 'No yelling on the bus!' + expect { submit_form } + .to change(Rule, :count).by(1) + expect(page) + .to have_content(I18n.t('admin.rules.title')) + end + + def submit_form + click_on I18n.t('admin.rules.add_new') + end + end + + describe 'Editing an existing rule' do + let!(:rule) { Fabricate :rule, text: 'Rule text' } + + it 'updates with valid attributes' do + visit admin_rules_path + + # Invalid submission + click_on rule.text + fill_in 'rule_text', with: '' + expect { submit_form } + .to_not change(rule.reload, :updated_at) + + # Valid update + fill_in 'rule_text', with: 'What day is this?' + expect { submit_form } + .to(change { rule.reload.text }) + end + + def submit_form + click_on I18n.t('generic.save_changes') + end + end + + describe 'Destroy a rule' do + let!(:rule) { Fabricate :rule } + + it 'removes the record' do + visit admin_rules_path + + expect { click_on I18n.t('admin.rules.delete') } + .to change { rule.reload.discarded? }.to(true) + end + end + end +end diff --git a/spec/system/admin/webhooks_spec.rb b/spec/system/admin/webhooks_spec.rb index 69e8a7d69f..eb3138c380 100644 --- a/spec/system/admin/webhooks_spec.rb +++ b/spec/system/admin/webhooks_spec.rb @@ -77,7 +77,7 @@ RSpec.describe 'Admin Webhooks' do # Valid update fill_in 'webhook_url', with: 'https://host.example/new/value/123' expect { submit_form } - .to_not change(webhook.reload, :url) + .to(change { webhook.reload.url }) end def submit_form