2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-09-25 11:32:08 -04:00
|
|
|
|
|
|
|
describe Admin::UserFieldsController do
|
|
|
|
|
|
|
|
it "is a subclass of AdminController" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(Admin::UserFieldsController < Admin::AdminController).to eq(true)
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in" do
|
|
|
|
let!(:user) { log_in(:admin) }
|
|
|
|
|
|
|
|
context '.create' do
|
|
|
|
it "creates a user field" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect {
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
user_field: { name: 'hello', description: 'hello desc', field_type: 'text' }
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
}.to change(UserField, :count).by(1)
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
2015-07-28 12:29:40 -04:00
|
|
|
|
|
|
|
it "creates a user field with options" do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :create, params: {
|
|
|
|
user_field: {
|
|
|
|
name: 'hello',
|
|
|
|
description: 'hello desc',
|
|
|
|
field_type: 'dropdown',
|
|
|
|
options: ['a', 'b', 'c']
|
|
|
|
}
|
|
|
|
}, format: :json
|
|
|
|
|
2015-07-28 12:29:40 -04:00
|
|
|
expect(response).to be_success
|
2017-08-31 00:06:56 -04:00
|
|
|
end.to change(UserField, :count).by(1)
|
2015-07-28 12:29:40 -04:00
|
|
|
|
|
|
|
expect(UserFieldOption.count).to eq(3)
|
|
|
|
end
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context '.index' do
|
|
|
|
let!(:user_field) { Fabricate(:user_field) }
|
|
|
|
|
|
|
|
it "returns a list of user fields" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :index, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-09-25 11:32:08 -04:00
|
|
|
json = ::JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json['user_fields']).to be_present
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.destroy' do
|
|
|
|
let!(:user_field) { Fabricate(:user_field) }
|
|
|
|
|
2014-10-08 14:38:18 -04:00
|
|
|
it "deletes the user field" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect {
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { id: user_field.id }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
}.to change(UserField, :count).by(-1)
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.update' do
|
|
|
|
let!(:user_field) { Fabricate(:user_field) }
|
|
|
|
|
2014-10-08 14:38:18 -04:00
|
|
|
it "updates the user field" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: user_field.id,
|
|
|
|
user_field: { name: 'fraggle', field_type: 'confirm', description: 'muppet' }
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-09-25 11:32:08 -04:00
|
|
|
user_field.reload
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(user_field.name).to eq('fraggle')
|
|
|
|
expect(user_field.field_type).to eq('confirm')
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
2015-07-28 12:29:40 -04:00
|
|
|
|
|
|
|
it "updates the user field options" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: user_field.id,
|
|
|
|
user_field: {
|
|
|
|
name: 'fraggle',
|
|
|
|
field_type: 'dropdown',
|
|
|
|
description: 'muppet',
|
|
|
|
options: ['hello', 'hello', 'world']
|
|
|
|
}
|
|
|
|
}, format: :json
|
|
|
|
|
2015-07-28 12:29:40 -04:00
|
|
|
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
|
2015-08-17 13:01:15 -04:00
|
|
|
|
|
|
|
it "keeps options when updating the user field" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: user_field.id,
|
|
|
|
user_field: {
|
|
|
|
name: 'fraggle',
|
|
|
|
field_type: 'dropdown',
|
|
|
|
description: 'muppet',
|
|
|
|
options: ['hello', 'hello', 'world'],
|
|
|
|
position: 1
|
|
|
|
}
|
|
|
|
}, format: :json
|
|
|
|
|
2015-08-17 13:01:15 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
user_field.reload
|
|
|
|
expect(user_field.user_field_options.size).to eq(2)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: user_field.id,
|
|
|
|
user_field: {
|
|
|
|
name: 'fraggle',
|
|
|
|
field_type: 'dropdown',
|
|
|
|
description: 'muppet',
|
|
|
|
position: 2
|
|
|
|
}
|
|
|
|
}, format: :json
|
|
|
|
|
2015-08-17 13:01:15 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
user_field.reload
|
|
|
|
expect(user_field.user_field_options.size).to eq(2)
|
|
|
|
end
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|