rules_controller_spec.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Admin::RulesController do
  4. render_views
  5. let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  6. before do
  7. sign_in user, scope: :user
  8. end
  9. describe 'GET #index' do
  10. it 'returns http success' do
  11. get :index
  12. expect(response).to have_http_status(:success)
  13. end
  14. end
  15. describe 'GET #edit' do
  16. let(:rule) { Fabricate(:rule) }
  17. it 'returns http success and renders edit' do
  18. get :edit, params: { id: rule.id }
  19. expect(response).to have_http_status(:success)
  20. expect(response).to render_template(:edit)
  21. end
  22. end
  23. describe 'POST #create' do
  24. context 'with valid data' do
  25. it 'creates a new rule and redirects' do
  26. expect do
  27. post :create, params: { rule: { text: 'The rule text.' } }
  28. end.to change(Rule, :count).by(1)
  29. expect(response).to redirect_to(admin_rules_path)
  30. end
  31. end
  32. context 'with invalid data' do
  33. it 'does creates a new rule and renders index' do
  34. expect do
  35. post :create, params: { rule: { text: '' } }
  36. end.to_not change(Rule, :count)
  37. expect(response).to render_template(:index)
  38. end
  39. end
  40. end
  41. describe 'PUT #update' do
  42. let(:rule) { Fabricate(:rule, text: 'Original text') }
  43. context 'with valid data' do
  44. it 'updates the rule and redirects' do
  45. put :update, params: { id: rule.id, rule: { text: 'Updated text.' } }
  46. expect(response).to redirect_to(admin_rules_path)
  47. end
  48. end
  49. context 'with invalid data' do
  50. it 'does not update the rule and renders index' do
  51. put :update, params: { id: rule.id, rule: { text: '' } }
  52. expect(response).to render_template(:edit)
  53. end
  54. end
  55. end
  56. describe 'DELETE #destroy' do
  57. let!(:rule) { Fabricate(:rule) }
  58. it 'destroys the rule and redirects' do
  59. delete :destroy, params: { id: rule.id }
  60. expect(rule.reload).to be_discarded
  61. expect(response).to redirect_to(admin_rules_path)
  62. end
  63. end
  64. end