require 'rails_helper' module Mks module Edm RSpec.describe EquipmentTypesController, type: :controller do routes { Mks::Edm::Engine.routes } before(:each) do u = create(:user) token = Mks::Auth::TokenAuth.issue(name: u.full_name, email: u.email, id: u.id) request.headers['Authorization'] = "Bearer #{token}" end let(:valid_attributes) do { code: FFaker::Name.name, name: FFaker::Name.name, description: FFaker::Lorem.paragraph } end let(:invalid_attributes) do { code: nil, name: FFaker::Name.name, description: FFaker::Lorem.paragraph } end describe 'GET #index' do it 'returns a success response' do create(:equipment_type) get :index, params: {} expect(response).to be_successful end end describe 'GET #nodes' do it 'returns equipment types as nodes' do 2.times { create(:equipment_type) } get :nodes result = JSON(response.body) expect(result.count).to eq 2 expect(result[0].keys[0]).to eq 'data' end end describe 'GET #show' do it 'returns a success response' do equipment_type = create(:equipment_type) get :show, params: { id: equipment_type.to_param } expect(response).to be_successful end end describe 'POST #create' do context 'with valid params' do it 'creates a new EquipmentType' do expect do post :create, params: { equipment_type: valid_attributes } end.to change(EquipmentType, :count).by(1) end it 'renders a JSON response with the new equipment_type' do post :create, params: { equipment_type: valid_attributes } expect(response).to have_http_status(:created) expect(response.content_type).to eq('application/json') expect(response.location).to eq(equipment_type_url(EquipmentType.last)) end end context 'with invalid params' do it 'renders a JSON response with errors for the new equipment_type' do post :create, params: { equipment_type: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end describe 'PUT #update' do context 'with valid params' do let(:new_attributes) do { name: FFaker::Name.name } end it 'updates the requested equipment_type' do equipment_type = create(:equipment_type) put :update, params: { id: equipment_type.to_param, equipment_type: new_attributes } equipment_type.reload expect(equipment_type.name).to eq new_attributes[:name] end it 'renders a JSON response with the equipment_type' do equipment_type = create(:equipment_type) put :update, params: { id: equipment_type.to_param, equipment_type: valid_attributes } expect(response).to have_http_status(:ok) expect(response.content_type).to eq('application/json') end end context 'with invalid params' do it 'renders a JSON response with errors for the equipment_type' do equipment_type = create(:equipment_type) put :update, params: { id: equipment_type.to_param, equipment_type: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end end end end