docbrown/spec/requests/response_templates_spec.rb
Andy Zhao 1a989bef07
[deploy] Add response templates to user settings (#6823)
* Add response templates controller, routes, and view

* Add response templates settings page

* Remove dead code

* Use proper variable oops

* Use proper indentation

* Use before action for checking current_user

* Remove index action temporarily

* Use url helpers for redirects

* Use form_with over form_tag and form_for

* Persist previous content if edit fails

* Use exists for performance

* Remove index spec and add CRUD specs

* Memoize @response_template

* Use workaround for sad paths b/c render doesn't work

* Refactor and extract permitted_attributes into var

* Use present over exists to preload into memory

* Use last over first

* Lint for codeclimate

* Use two redirects for happy and sad create path

* Remove proper index route oops

* Codeclimate comma sigh
2020-03-26 14:48:00 -04:00

85 lines
2.8 KiB
Ruby

require "rails_helper"
RSpec.describe "ResponseTemplate", type: :request do
let(:user) { create(:user) }
let(:moderator) { create(:user, :tag_moderator) }
let(:admin) { create(:user, :admin) }
describe "POST /response_templates #create" do
before { sign_in user }
let(:attributes) do
{
title: "some_title",
content: "some content",
type_of: "personal_comment"
}
end
it "successfully creates the proper response template" do
post response_templates_path, params: {
response_template: {
title: attributes[:title],
content: attributes[:content]
}
}
response_template = ResponseTemplate.last
expect(response_template.user_id).to eq user.id
expect(response_template.title).to eq attributes[:title]
expect(response_template.content).to eq attributes[:content]
expect(response_template.type_of).to eq attributes[:type_of]
end
it "redirects to the edit page upon success" do
post response_templates_path, params: {
response_template: {
title: attributes[:title],
content: attributes[:content]
}
}
expect(response.redirect_url).to include user_settings_path(tab: "response-templates", id: ResponseTemplate.last.id)
end
end
describe "PATCH /response_templates/:id #update" do
before { sign_in user }
let(:response_template) { create(:response_template, user: user) }
it "successfully updates the response template" do
title = "something else"
patch response_template_path(response_template.id), params: { response_template: { title: title } }
expect(ResponseTemplate.first.title).to eq title
end
it "redirects back to the response template" do
patch response_template_path(response_template.id), params: { response_template: { title: "something else" } }
expect(response.redirect_url).to include user_settings_path(tab: "response-templates", id: ResponseTemplate.first.id)
end
it "shows the previously written content on a failed submission" do
content = "something something something"
patch response_template_path(response_template.id), params: { response_template: { title: "", content: content } }
follow_redirect!
expect(response.body).to include content
end
end
describe "DELETE /response_templates/:id #destroy" do
before do
sign_in user
delete response_template_path(response_template.id)
end
let(:response_template) { create(:response_template, user: user) }
it "successfully destroys the response template" do
expect(ResponseTemplate.count).to eq 0
end
it "redirects to /settings/response_templates" do
expect(response.redirect_url).to include user_settings_path(tab: "response-templates")
end
end
end