mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 17:03:13 +08:00
3e50313fdc
Since rspec-rails 3, the default installation creates two helper files: * `spec_helper.rb` * `rails_helper.rb` `spec_helper.rb` is intended as a way of running specs that do not require Rails, whereas `rails_helper.rb` loads Rails (as Discourse's current `spec_helper.rb` does). For more information: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files In this commit, I've simply replaced all instances of `spec_helper` with `rails_helper`, and renamed the original `spec_helper.rb`. This brings the Discourse project closer to the standard usage of RSpec in a Rails app. At present, every spec relies on loading Rails, but there are likely many that don't need to. In a future pull request, I hope to introduce a separate, minimal `spec_helper.rb` which can be used in tests which don't rely on Rails.
101 lines
3.6 KiB
Ruby
101 lines
3.6 KiB
Ruby
require 'rails_helper'
|
|
|
|
describe Admin::UserFieldsController do
|
|
|
|
it "is a subclass of AdminController" do
|
|
expect(Admin::UserFieldsController < Admin::AdminController).to eq(true)
|
|
end
|
|
|
|
context "when logged in" do
|
|
let!(:user) { log_in(:admin) }
|
|
|
|
context '.create' do
|
|
it "creates a user field" do
|
|
expect {
|
|
xhr :post, :create, {user_field: {name: 'hello', description: 'hello desc', field_type: 'text'} }
|
|
expect(response).to be_success
|
|
}.to change(UserField, :count).by(1)
|
|
end
|
|
|
|
it "creates a user field with options" do
|
|
expect {
|
|
xhr :post, :create, {user_field: {name: 'hello',
|
|
description: 'hello desc',
|
|
field_type: 'dropdown',
|
|
options: ['a', 'b', 'c']} }
|
|
expect(response).to be_success
|
|
}.to change(UserField, :count).by(1)
|
|
|
|
expect(UserFieldOption.count).to eq(3)
|
|
end
|
|
end
|
|
|
|
context '.index' do
|
|
let!(:user_field) { Fabricate(:user_field) }
|
|
|
|
it "returns a list of user fields" do
|
|
xhr :get, :index
|
|
expect(response).to be_success
|
|
json = ::JSON.parse(response.body)
|
|
expect(json['user_fields']).to be_present
|
|
end
|
|
end
|
|
|
|
context '.destroy' do
|
|
let!(:user_field) { Fabricate(:user_field) }
|
|
|
|
it "deletes the user field" do
|
|
expect {
|
|
xhr :delete, :destroy, id: user_field.id
|
|
expect(response).to be_success
|
|
}.to change(UserField, :count).by(-1)
|
|
end
|
|
end
|
|
|
|
context '.update' do
|
|
let!(:user_field) { Fabricate(:user_field) }
|
|
|
|
it "updates the user field" do
|
|
xhr :put, :update, id: user_field.id, user_field: {name: 'fraggle', field_type: 'confirm', description: 'muppet'}
|
|
expect(response).to be_success
|
|
user_field.reload
|
|
expect(user_field.name).to eq('fraggle')
|
|
expect(user_field.field_type).to eq('confirm')
|
|
end
|
|
|
|
it "updates the user field options" do
|
|
xhr :put, :update, id: user_field.id, user_field: {name: 'fraggle',
|
|
field_type: 'dropdown',
|
|
description: 'muppet',
|
|
options: ['hello', 'hello', 'world']}
|
|
expect(response).to be_success
|
|
user_field.reload
|
|
expect(user_field.name).to eq('fraggle')
|
|
expect(user_field.field_type).to eq('dropdown')
|
|
expect(user_field.user_field_options.size).to eq(2)
|
|
end
|
|
|
|
it "keeps options when updating the user field" do
|
|
xhr :put, :update, id: user_field.id, user_field: {name: 'fraggle',
|
|
field_type: 'dropdown',
|
|
description: 'muppet',
|
|
options: ['hello', 'hello', 'world'],
|
|
position: 1}
|
|
expect(response).to be_success
|
|
user_field.reload
|
|
expect(user_field.user_field_options.size).to eq(2)
|
|
|
|
xhr :put, :update, id: user_field.id, user_field: {name: 'fraggle',
|
|
field_type: 'dropdown',
|
|
description: 'muppet',
|
|
position: 2}
|
|
expect(response).to be_success
|
|
user_field.reload
|
|
expect(user_field.user_field_options.size).to eq(2)
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|
|
|