require 'rails_helper' module Mks module Edm RSpec.describe EquipmentsController, 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, equipment_category_id: create(:equipment_category).id } end let(:invalid_attributes) do { code: nil, name: FFaker::Name.name, description: FFaker::Lorem.paragraph, equipment_category_id: create(:equipment_category).id } end describe 'GET #index' do it 'returns a success response' do ec = create(:equipment_category) create(:equipment, equipment_category: ec) get :index, params: { id: ec.id } expect(response).to be_successful result = JSON(response.body) expect(result['data'].count).to eq 1 end end describe 'GET #filter' do it 'filters by equipment name' do ec = create(:equipment_category, name: 'Construction Machines') create(:equipment, name: 'Dozer', equipment_category: ec) create(:equipment, name: 'Excavater', equipment_category: ec) create(:equipment, name: 'Crane', equipment_category: ec) create(:equipment, name: 'Loader', equipment_category: ec) get :filter, params: { search: 'er' } result = JSON(response.body) expect(result.count).to eq 3 end it 'filters by category name' do ec1 = create(:equipment_category, name: 'Earth Moving Machines') ec2 = create(:equipment_category, name: 'Loaders') create(:equipment, name: 'Dozer', equipment_category: ec1) create(:equipment, name: 'Excavater / Mover', equipment_category: ec2) create(:equipment, name: 'Crane', equipment_category: ec1) create(:equipment, name: 'Loader', equipment_category: ec2) get :filter, params: { search: 'mov' } result = JSON(response.body) expect(result.count).to eq 3 end end describe 'GET #show' do it 'returns a success response' do equipment = create(:equipment) get :show, params: { id: equipment.to_param } expect(response).to be_successful end end describe 'POST #create' do context 'with valid params' do it 'creates a new Equipment' do expect do post :create, params: { equipment: valid_attributes } end.to change(Equipment, :count).by(1) end it 'renders a JSON response with the new equipment' do post :create, params: { equipment: valid_attributes } expect(response).to have_http_status(:created) expect(response.content_type).to eq('application/json') expect(response.location).to eq(equipment_url(Equipment.last)) end end context 'with invalid params' do it 'renders a JSON response with errors for the new equipment' do post :create, params: { equipment: 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' do equipment = create(:equipment) put :update, params: { id: equipment.to_param, equipment: new_attributes } equipment.reload expect(equipment.name).to eq new_attributes[:name] end it 'renders a JSON response with the equipment' do equipment = create(:equipment) put :update, params: { id: equipment.to_param, equipment: 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' do equipment = create(:equipment) put :update, params: { id: equipment.to_param, equipment: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end end end end