Skip to content

Commit

Permalink
Add tests for element form type
Browse files Browse the repository at this point in the history
  • Loading branch information
beque committed Oct 11, 2023
1 parent 8d0679a commit 6f14833
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
86 changes: 86 additions & 0 deletions spec/api/chemotion/element_form_type_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# frozen_string_literal: true

describe Chemotion::ElementFormTypeAPI do
include_context 'api request authorization context'

let(:new_element_form_type) { create(:element_form_type, :sample_type, creator: user) }
let(:element_form_type_with_structure) { create(:element_form_type, :sample_type, :default_sample_structure) }

describe 'GET /api/v1/element_form_types' do
before do
new_element_form_type
end

context 'without params' do
it 'fetches all ElementFormTypes' do
get '/api/v1/element_form_types'

expect(parsed_json_response['element_form_types'].length).to eq(1)
end
end

context 'with id' do
it 'fetches an ElementFormType' do
get "/api/v1/element_form_types/#{new_element_form_type.id}"

expect(parsed_json_response['name']).to eql(new_element_form_type.name)
end
end
end

describe 'POST /api/v1/element_form_types' do
let(:element_form_type_params) do
{
name: 'chemical',
element_type: 'sample',
enabled: false,
}
end

let(:expected_result) do
{
name: 'chemical',
description: nil,
element_type: 'sample',
structure: {},
enabled: false,
}.stringify_keys
end

it 'creates an element form type' do
post '/api/v1/element_form_types', params: element_form_type_params

expect(parsed_json_response).to include(expected_result)
end
end

describe 'PUT /api/v1/element_form_types/:id' do
context 'when updating an element form type with structure' do
let(:params) do
{
name: 'chemical',
description: nil,
element_type: 'sample',
structure: element_form_type_with_structure.structure,
enabled: true,
}
end

it 'returns the updated ElementFormType' do
put "/api/v1/element_form_types/#{new_element_form_type.id}", params: params

expect(parsed_json_response['structure']).to eql(params[:structure])
end
end
end

describe 'DELETE /api/v1/element_form_types/:id' do
context 'when element form type exist' do
it 'deletes the element form type' do
delete "/api/v1/element_form_types/#{new_element_form_type.id}"

expect(parsed_json_response).to include('deleted' => new_element_form_type.id)
end
end
end
end
83 changes: 83 additions & 0 deletions spec/factories/element_form_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# frozen_string_literal: true

FactoryBot.define do
factory :element_form_type do
name { 'Organic chemistry' }
description { 'default' }
enabled { true }
creator { create(:user) }

trait :sample_type do
element_type { 'sample' }
end

trait :default_sample_structure do
structure do
{
'columns' => [
{
'key' => 'basic',
'rows' => [
{
'key' => 'name_external_label_xref_inventory_label',
'cols' => '4',
'fields' => [
{
'key' => 'name',
'type' => 'text',
'label' => 'Name',
'column' => 'name',
'default' => 'Name of sample',
'visible' => 'true',
'required' => 'false',
'column_size' => 'column',
'description' => '',
},
{
'key' => 'external_label',
'type' => 'text',
'label' => 'External Label',
'column' => 'external_label',
'default' => '',
'visible' => 'true',
'required' => 'false',
'column_size' => 'column',
'description' => '',
},
{
'key' => 'xref_inventory_label',
'opt' => 'inventory_label',
'type' => 'text',
'label' => 'Inventory label',
'column' => 'xref',
'default' => '',
'visible' => 'true',
'required' => 'false',
'column_size' => 'column',
'description' => '',
},
{
'key' => 'dry_solvent',
'type' => 'checkbox',
'label' => 'Dry Solvent',
'column' => 'dry_solvent',
'default' => '',
'visible' => 'true',
'required' => 'false',
'conditions' => {
'can_update' => 'true',
},
'column_size' => 'column',
'description' => '',
},
],
'visible' => 'true',
},
],
},
],
}
end
end
end
end

0 comments on commit 6f14833

Please sign in to comment.