diff --git a/spec/controllers/concerns/searchable_spec.rb b/spec/controllers/concerns/searchable_spec.rb new file mode 100644 index 000000000..ce6413a08 --- /dev/null +++ b/spec/controllers/concerns/searchable_spec.rb @@ -0,0 +1,165 @@ +require 'rails_helper' + +RSpec.describe Searchable do + let(:controller) { FakesController.new } + let(:params) { {} } + + before do + class FakesController < ApplicationController + include Searchable + end + + allow(controller).to receive(:params).and_return( + ActionController::Parameters.new(params) + ) + end + + after do + Object.send :remove_const, :FakesController + end + + describe '#sort_params' do + subject(:sort_params) { controller.send(:sort_params) } + + context 'turn collection id as array' do + let(:params) do + { + data: { + collection_id: '1' + } + } + end + + it 'collection id as array' do + expect(sort_params.dig('collection_id')).to eq (['1']) + end + end + + context 'require params' do + let(:params) do + { + data: { + sort_id: 'asc', + text: 'any-text', + q: 'any-query', + institution_id: 'any institution id', + theme: ['theme_1'], + collection_id: ['1'], + unrequired_params: 1 + } + } + end + + it 'returns required params' do + expect(sort_params.keys).to contain_exactly( + "sort_id", "text", "q", "institution_id", "theme", "collection_id" + ) + end + end + end + + describe '#build_params' do + subject(:build_params) { controller.send(:build_params) } + + let(:params) do + { + data: { + sort_id: 'asc', + text: 'any-text', + q: 'any-query', + institution_id: 'any institution id', + theme: ['theme_1'], + collection_id: ['1'], + unrequired_params: 1 + } + } + end + + context 'when all params is present' do + it 'returns all params' do + expect(build_params.keys).to contain_exactly( + "sort_id", "text", "q", "institution_id", "theme", "collection_id" + ) + end + end + + context 'when collection_id value is 0' do + let(:params) do + { + data: { + sort_id: 'asc', + text: 'any-text', + q: 'any-query', + institution_id: 'any institution id', + theme: ['theme_1'], + collection_id: ['0'], + unrequired_params: 1 + } + } + end + + it 'returns all params except collection id' do + expect(build_params.keys).to contain_exactly( + "sort_id", "text", "q", "institution_id", "theme" + ) + end + end + end + + describe '#load_institutions' do + subject(:load_institutions) { controller.send(:load_institutions) } + + context 'when collection id is present' do + let(:collection) { create(:collection) } + let(:params) do + { + data: { + collection_id: [collection.id] + } + } + end + + it 'it returns institution' do + expect(load_institutions).to include(collection.institution) + end + end + + context 'when collection id is not present' do + it 'returns all institution' do + institution_a = create(:institution) + institution_b = create(:institution) + + expect(load_institutions).to include(institution_a, institution_b) + end + end + end + + describe '#load_collection' do + subject(:load_collection) { controller.send(:load_collection) } + + let(:collection) { create(:collection) } + + context 'when institution params is present' do + let(:params) do + { + data: { + institution_id: collection.institution.id + } + } + end + + it 'returns collection' do + expect(load_collection).to contain_exactly(collection) + end + end + + context 'when institution params is not present' do + it 'returns all collection' do + collection_a = create(:collection) + collection_b = create(:collection) + + expect(load_collection).to contain_exactly(collection_a, collection_b) + end + end + end +end diff --git a/spec/decorators/transcript_line_decorator_spec.rb b/spec/decorators/transcript_line_decorator_spec.rb new file mode 100644 index 000000000..83a1596ee --- /dev/null +++ b/spec/decorators/transcript_line_decorator_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +RSpec.describe TranscriptLineDecorator do + let(:decorator) { described_class.new(transcript_line) } + let(:transcript_line) { create(:transcript_line) } + + describe '#search_title' do + subject(:search_title) { decorator.search_title } + + let(:expectation) do + "#{transcript_line.transcript.decorate.collection_title}"\ + " - #{transcript_line.transcript.title}" + end + + it { is_expected.to eq expectation } + end + + describe '#image_url' do + subject(:image_url) { decorator.image_url } + + it { is_expected.to eq transcript_line.transcript.image_url } + end + + describe '#humanize_duration' do + subject(:humanize_duration) { decorator.humanize_duration(12345) } + + it { is_expected.to eq '(3h 25m 45s)' } + end +end diff --git a/spec/jobs/delete_institution_job_spec.rb b/spec/jobs/delete_institution_job_spec.rb new file mode 100644 index 000000000..e70d52ef8 --- /dev/null +++ b/spec/jobs/delete_institution_job_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe DeleteInstitutionJob do + let!(:institution) { create(:institution) } + let(:user) { create(:user) } + + describe "#perform_later" do + it "enqueues job" do + ActiveJob::Base.queue_adapter = :test + + expect { + described_class.perform_later(institution.name, institution.id, user.email) + }.to have_enqueued_job + end + + it 'destroy institution and execute mailer' do + mailer = double(deliver_now: true) + allow(InstitutionMailer).to receive(:delete_institution).and_return(mailer) + + expect(InstitutionMailer).to receive(:delete_institution).and_return(mailer) + expect { + described_class.perform_now(institution.name, institution.id, user.email) + }.to change { Institution.count }.by(-1) + end + end +end diff --git a/spec/jobs/recalculate_transcripts_job_spec.rb b/spec/jobs/recalculate_transcripts_job_spec.rb new file mode 100644 index 000000000..5fc0a248d --- /dev/null +++ b/spec/jobs/recalculate_transcripts_job_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe RecalculateTranscriptsJob do + describe "#perform_later" do + it "enqueues job" do + ActiveJob::Base.queue_adapter = :test + + expect { described_class.perform_later }.to have_enqueued_job + end + end +end diff --git a/spec/jobs/voice_base_upload_job_spec.rb b/spec/jobs/voice_base_upload_job_spec.rb new file mode 100644 index 000000000..d661f2a97 --- /dev/null +++ b/spec/jobs/voice_base_upload_job_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe VoiceBaseUploadJob do + let!(:transcript) { create(:transcript) } + let(:user) { create(:user) } + + describe "#perform_later" do + it "enqueues job" do + ActiveJob::Base.queue_adapter = :test + + expect { described_class.perform_later(transcript) }.to have_enqueued_job + end + + it 'execute VoiceBase API' do + allow(VoiceBase::VoicebaseApiService).to receive(:upload_media).with(transcript.id) + + expect(VoiceBase::VoicebaseApiService).to receive(:upload_media).with(transcript.id) + described_class.perform_now(transcript.id) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7d8445011..162bbb9c5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,6 +22,10 @@ add_filter 'app/controllers/users/omniauth_callbacks_controller.rb' add_filter 'app/controllers/users/registrations_controller.rb' add_filter 'app/controllers/users/sessions_controller.rb' + # nothing to test here + add_filter 'app/decorators/admin/institution_decorator.rb' + add_filter 'app/decorators/institution_decorator.rb' + add_filter 'app/jobs/daily_analytics_job.rb' end SimpleCov.start 'rails'