require 'rails_helper' module Logistics module Core RSpec.describe AdditionalDocumentsController, type: :controller do routes { Logistics::Core::Engine.routes } describe 'GET #get_lookup_values' do it 'gets all values for a lookup entity' do am1 = build(:acquisition_mode) am2 = build(:acquisition_mode) data = [am1, am2] expect_any_instance_of(AdditionalDocumentService).to receive(:get_lookup_values).and_return(data) get :get_lookup_values, params: { :entity => 'Logistics::Core::AcquisitionMode' }, format: :json result = JSON(response.body) expect(result['data'].count).to eq 2 end end describe 'GET #get_additional_documents' do it 'gets all values for a lookup entity' do am = build(:acquisition_mode) tt = build(:transaction_type) ad1 = create(:additional_document, document_type: build(:document_type), documentable: am, transaction_type: tt) ad2 = create(:additional_document, document_type: build(:document_type), documentable: am, transaction_type: tt) data = [ad1, ad2] expect_any_instance_of(AdditionalDocumentService).to receive(:get_additional_documents).and_return(data) get :get_document_types, params: { :entity => 'Logistics::Core::AcquisitionMode', :rec_id => am.id }, format: :json result = JSON(response.body) expect(result['data'].count).to eq 2 end end describe 'POST #bulk_save' do it 'saves additional documents in bulk' do tt = create(:transaction_type) data = [ { :document_type_id => 1, :documentable_type => 'Lookup', :documentable_id => 1, :original => 2, :copy => 2, :certified => 1, :is_endorsed => true, :is_mandatory => true, :transaction_type_id => tt.id }, { :document_type_id => 2, :documentable_type => 'Lookup', :documentable_id => 1, :original => 1, :copy => 2, :certified => 1, :is_endorsed => true, :is_mandatory => true, :transaction_type_id => tt.id } ] response = Mks::Common::MethodResponse.new(true, nil, nil, nil, nil) expect_any_instance_of(AdditionalDocumentService).to receive(:bulk_save).and_return(response) post :bulk_save, params: { :payload => data }, format: :json expect(response.success).to be_truthy end end describe 'POST #bulk_update' do it 'saves additional documents in bulk' do tt = create(:transaction_type) data = [ { :id => 1, :document_type_id => 1, :documentable_type => 'Lookup', :documentable_id => 1, :original => 2, :copy => 2, :certified => 1, :is_endorsed => true, :is_mandatory => true, :transaction_type_id => tt.id }, { :id => 2, :document_type_id => 2, :documentable_type => 'Lookup', :documentable_id => 1, :original => 1, :copy => 2, :certified => 1, :is_endorsed => true, :is_mandatory => true, :transaction_type_id => tt.id } ] response = Mks::Common::MethodResponse.new(true, nil, nil, nil, nil) expect_any_instance_of(AdditionalDocumentService).to receive(:bulk_update).and_return(response) post :bulk_update, params: { :payload => data }, format: :json expect(response.success).to be_truthy end end describe 'POST #bulk_delete' do it 'delete additional documents in bulk' do response = Mks::Common::MethodResponse.new(true, nil, nil, nil, nil) expect_any_instance_of(AdditionalDocumentService).to receive(:bulk_delete).with(['1', '2']).and_return(response) delete :bulk_delete, params: { :ids => [1, 2] }, format: :json expect(response.success).to be_truthy end end end end end