require 'rails_helper' module Mks module Edm RSpec.describe EquipmentLocationsController, 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) { { code: FFaker::Name.unique.name, name: FFaker::Name.name, description: FFaker::Name.name, address: FFaker::Name.name, location_type_id: create(:location_type).id } } let(:invalid_attributes) { { code: nil, name: nil, description: nil, address: FFaker::Name.name, location_type_id: create(:location_type).id } } describe 'GET #index' do it 'returns all equipment locations' do 3.times { create(:equipment_location) } get :index result = JSON(response.body) expect(result['data'].count).to eq 3 end end describe 'POST #create' do context 'with valid params' do it 'creates a new EquipmentLocation' do expect { post :create, params: { equipment_location: valid_attributes }, format: :json }.to change(EquipmentLocation, :count).by(1) end it 'returns a success message' do post :create, params: { equipment_location: valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Equipment location saved successfully !') end end context 'with invalid params' do it 'returns error message(s)' do post :create, params: { equipment_location: invalid_attributes }, format: :json result = JSON(response.body) expect(result['code'].count).to be > 0 end end end describe 'PUT #update' do context 'with valid params' do it 'updates the requested equipment location' do el = create(:equipment_location) put :update, params: { id: el.id, equipment_location: { code: 'AA' } }, format: :json el.reload expect(el.code).to eq('AA') end it 'returns a success message' do el = create(:equipment_location) put :update, params: { id: el.id, equipment_location: { code: 'AA' } }, format: :json result = JSON(response.body) expect(result['message']).to eq('Equipment location updated successfully !') end end context 'with invalid params' do it 'returns error message(s)' do el = create(:equipment_location) put :update, params: { id: el.id, equipment_location: { code: nil } }, format: :json result = JSON(response.body) expect(result['code'].count).to be > 0 end end end describe 'GET #stores' do it 'fetches stores only' do l1 = create(:location_type, name: 'Store') l2 = create(:location_type, name: 'Service Provider') 3.times { create(:equipment_location, location_type: l1) } 3.times { create(:equipment_location, location_type: l2) } get :stores result = JSON(response.body) expect(result['data'].count).to eq 3 end end describe 'GET #service_providers' do it 'fetches service providers only' do l1 = create(:location_type, name: 'Store') l2 = create(:location_type, name: 'Service Provider') 3.times { create(:equipment_location, location_type: l1) } 3.times { create(:equipment_location, location_type: l2) } get :stores result = JSON(response.body) expect(result['data'].count).to eq 3 end end end end end