Skip to content

Commit

Permalink
On #8: Article units integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed May 4, 2024
1 parent e610b2a commit 02bec16
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ RSpec/BeforeAfterAll:
# Configuration parameters: EnabledMethods.
RSpec/Capybara/FeatureMethods:
Exclude:
- 'spec/integration/article_units_spec.rb'
- 'spec/integration/articles_spec.rb'
- 'spec/integration/balancing_spec.rb'
- 'spec/integration/config_spec.rb'
Expand Down Expand Up @@ -433,6 +434,7 @@ RSpec/ExampleLength:
# Include: **/*_spec*rb*, **/spec/**/*
RSpec/FilePath:
Exclude:
- 'spec/integration/article_units_spec.rb'
- 'spec/integration/articles_spec.rb'
- 'spec/integration/login_spec.rb'
- 'spec/lib/bank_account_information_importer_spec.rb'
Expand Down Expand Up @@ -524,6 +526,7 @@ RSpec/RepeatedExample:
# Include: **/*_spec.rb
RSpec/SpecFilePathFormat:
Exclude:
- 'spec/integration/article_units_spec.rb'
- 'spec/integration/articles_spec.rb'
- 'spec/integration/login_spec.rb'
- 'spec/lib/bank_account_information_importer_spec.rb'
Expand Down
6 changes: 6 additions & 0 deletions app/assets/javascripts/article-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ArticleForm {
this.unitsReceived$ = $('#order_article_units_received', this.articleForm$);
this.toggleExtraUnitsButton$ = $('.toggle-extra-units', this.articleForm$);
this.extraUnits$ = $('.extra-unit-fields', this.articleForm$);
this.submitButton$ = $('input[type="submit"]', this.articleForm$);
this.multiForm$ = multiForm$;
const selectContainer$ = this.articleForm$.parents('#modalContainer');
this.select2Config = {
Expand All @@ -47,6 +48,7 @@ class ArticleForm {
this.convertOrderedAndReceivedUnits(this.supplierUnitSelect$.val(), this.billingUnit$.val());
this.initializeFormSubmitListener();
this.initializeToggleExtraUnitsButton();
this.enableSubmitButton();
} catch (e) {
console.log('Could not initialize article form', e, 'articleUnitRatioTemplate$', articleUnitRatioTemplate$, 'articleForm$', articleForm$, 'units', units, 'priceMarkup', priceMarkup, 'multiForm$', multiForm$, 'unitFieldsIdPrefix', unitFieldsIdPrefix, 'unitFieldsNamePrefix', unitFieldsNamePrefix);
}
Expand Down Expand Up @@ -578,6 +580,10 @@ class ArticleForm {
ratioQuantityFieldNameByIndex(i) {
return `${this.unitFieldsNamePrefix}[article_unit_ratios_attributes][${i}][quantity]`;
}

enableSubmitButton() {
this.submitButton$.removeAttr('disabled');
}
}


Expand Down
4 changes: 2 additions & 2 deletions app/controllers/article_units_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ArticleUnitsController < ApplicationController
def index; end

def search
@query = params[:q].blank? ? nil : params[:q].downcase
@query = article_unit_params[:q].blank? ? nil : article_unit_params[:q].downcase

existing_article_units = ArticleUnit.all.to_a
@article_units = @available_units
Expand Down Expand Up @@ -46,7 +46,7 @@ def load_available_units
end

def article_unit_params
params.require(:article_unit).permit(:unit)
params.permit(:q)
end

def sort_by_unit_name(query, a_unit, b_unit)
Expand Down
2 changes: 1 addition & 1 deletion app/views/article_units/_create_link.html.haml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
= link_to t('.add'), article_units_path(unit: unit), class: 'btn btn-mini', remote: true, method: :post, data: {'for-unit': unit}
= link_to t('.add'), article_units_path(unit: unit), class: 'btn btn-mini', remote: true, method: :post, data: {'for-unit': unit, 'e2e-create-unit': unit}
2 changes: 1 addition & 1 deletion app/views/article_units/_destroy_link.html.haml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
=link_to t('.delete'), article_unit, method: :delete, class: 'btn btn-mini btn-danger', remote: true, data: { confirm: t('.confirm'), 'for-unit': article_unit.unit }
=link_to t('.delete'), article_unit, method: :delete, class: 'btn btn-mini btn-danger', remote: true, data: { confirm: t('.confirm'), 'for-unit': article_unit.unit, 'e2e-destroy-unit': article_unit.unit }
2 changes: 1 addition & 1 deletion app/views/articles/_article.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
%td= number_to_currency(article.deposit) if article.deposit != 0
%td
= link_to t('ui.edit'), edit_supplier_article_path(@supplier, article),
remote: true, class: 'btn btn-mini'
remote: true, class: 'btn btn-mini', data: {'e2e-edit-article': article.id}
= link_to t('ui.copy'), supplier_article_copy_path(@supplier, article),
remote: true, class: 'btn btn-mini'
= link_to t('ui.delete'), [@supplier, article],
Expand Down
2 changes: 1 addition & 1 deletion app/views/articles/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
= f.input :order_number
.modal-footer
= link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'}
= f.submit class: 'btn btn-primary'
= f.submit class: 'btn btn-primary', disabled: true
16 changes: 15 additions & 1 deletion spec/factories/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,24 @@
order_number { nil }
unit_quantity { nil }
unit { nil }
supplier_order_unit { 'XPK' }
group_order_unit { 'XPK' }
billing_unit { 'XPK' }
price_unit { 'XPK' }
article_unit_ratio_count { 1 }
end

after(:create) do |article, evaluator|
create_list(:article_version, evaluator.article_version_count, article: article, order_number: evaluator.order_number, unit_quantity: evaluator.unit_quantity, unit: evaluator.unit)
create_list(:article_version, evaluator.article_version_count,
article: article,
order_number: evaluator.order_number,
unit_quantity: evaluator.unit_quantity,
unit: evaluator.unit,
supplier_order_unit: evaluator.supplier_order_unit,
group_order_unit: evaluator.group_order_unit,
billing_unit: evaluator.billing_unit,
price_unit: evaluator.price_unit,
article_unit_ratio_count: evaluator.article_unit_ratio_count)

article.reload
end
Expand Down
7 changes: 4 additions & 3 deletions spec/factories/article_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
FactoryBot.define do
factory :article_version do
sequence(:name) { |n| Faker::Lorem.words(number: rand(2..4)).join(' ') + " ##{n}" }
supplier_order_unit { 'XPK' } # TODO
group_order_unit { 'XPK' } # TODO
billing_unit { 'XPK' } # TODO
supplier_order_unit { 'XPK' }
group_order_unit { 'XPK' }
billing_unit { 'XPK' }
price { rand(0.1..26.0).round(2) }
price_unit { 'XPK' }
tax { [6, 21].sample }
deposit { rand(10) < 8 ? 0 : [0.0, 0.80, 1.20, 12.00].sample }
article_category
Expand Down
64 changes: 64 additions & 0 deletions spec/integration/article_units_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require_relative '../spec_helper'

feature ArticleUnitsController do
let(:user) { create(:user, groups: [create(:workgroup, role_article_meta: true)]) }

before do
login user
create(:article_unit, unit: 'XPP')
end

describe ':index', :js do
before { visit article_units_path }

it 'displays units that have already been added along with their translations' do
expect(page).to have_content(/piece/i)
end

it 'does not display units that have already been added along with their translations' do
expect(page).to have_no_content(/kilogram/i)
end

it 'allows searching for recommended (translated) units that have not been added yet' do
check 'only_recommended'
fill_in 'article_unit_search', with: 'kilogram'
expect(page).to have_content(/kilogram/i)
end

it 'does not return search results for units that have neither been added nor translated unless the "only recommended" checkbox is unchecked' do
check 'only_recommended'
fill_in 'article_unit_search', with: 'kiloampere'
expect(page).to have_no_content(/kiloampere/i)

uncheck 'only_recommended'

expect(page).to have_content(/kiloampere/i)
end

it 'allows adding units' do
fill_in 'article_unit_search', with: 'kilogram'
expect(page).to have_content(/kilogram/i)

find('*[data-e2e-create-unit="KGM"]').click

# reset search...:
fill_in 'article_unit_search', with: ''
page.has_content?('piece')

# ... kilogram should *still* be there if it has been added successfully:
expect(page).to have_content(/kilogram/i)
end

it 'allows deleting units' do
accept_confirm do
find('*[data-e2e-destroy-unit="XPP"]').click
end

expect(page).to have_css('.alert-success')

# the unit is still displayed (even if not in the search scope) in case the user wants to re-add it:
expect(page).to have_content(/piece/i)
expect(page).to have_css('a[data-e2e-create-unit="XPP"]')
end
end
end
76 changes: 61 additions & 15 deletions spec/integration/articles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,74 @@
end

describe ':index', :js do
before { visit supplier_articles_path(supplier_id: supplier.id) }
let!(:existing_article) do
create(:article,
supplier: supplier,
supplier_order_unit: 'B22',
group_order_unit: 'B22',
billing_unit: 'B22',
price_unit: 'B22',
article_unit_ratio_count: 0)
end

before do
visit supplier_articles_path(supplier_id: supplier.id)
end

it 'can visit supplier articles path' do
expect(page).to have_content(supplier.name)
expect(page).to have_content(I18n.t('articles.index.edit_all'))
end

it 'can create a new article' do
click_on I18n.t('articles.index.new')
expect(page).to have_css('form#new_article_version')
article_version = build(:article_version, supplier_order_unit: article_unit.unit)
within('#new_article_version') do
fill_in 'article_version_name', with: article_version.name
select article_category.name, from: 'article_version_article_category_id'
fill_in 'article_version_price', with: article_version.price
unit_label = ArticleUnitsLib.units[article_version.supplier_order_unit][:name]
select unit_label, from: 'article_version_supplier_order_unit'
fill_in 'article_version_tax', with: article_version.tax
fill_in 'article_version_deposit', with: article_version.deposit
find('input[type="submit"]').click
describe 'creating articles' do
it 'can create a new article' do
click_on I18n.t('articles.index.new')
expect(page).to have_css('form#new_article_version')
article_version = build(:article_version, supplier_order_unit: article_unit.unit)
within('#new_article_version') do
fill_in 'article_version_name', with: article_version.name
select article_category.name, from: 'article_version_article_category_id'
fill_in 'article_version_price', with: article_version.price
unit_label = ArticleUnitsLib.units[article_version.supplier_order_unit][:name]
select unit_label, from: 'article_version_supplier_order_unit'
fill_in 'article_version_tax', with: article_version.tax
fill_in 'article_version_deposit', with: article_version.deposit
find('input[type="submit"]:enabled').click
end
expect(page).to have_content(article_version.name)
end

it 'provides units that have been added to article_units' do
create(:article_unit, unit: 'KGM')
create(:article_unit, unit: 'LTR')

click_on I18n.t('articles.index.new')
expect(page).to have_css('form#new_article_version')
expect(page).to have_select('article_version_supplier_order_unit', options: ['Custom', 'kilogram (kg)', 'litre (l)', 'Package', 'Piece'])
end
end

describe 'editing articles' do
it 'can edit an existing article' do
find("*[data-e2e-edit-article='#{existing_article.id}']").click
expect(page).to have_css("form#edit_article_version_#{existing_article.id}")
within("#edit_article_version_#{existing_article.id}") do
fill_in 'article_version_name', with: 'New name'
sleep 0.25 # <- unsure why this is required as the following line should wait for the button to be enabled:
find('input[type="submit"]:enabled').click
end

expect(page).to have_content('New name')
end

it 'provides units that have been added to article_units as well as those in the article being edited' do
create(:article_unit, unit: 'KGM')
create(:article_unit, unit: 'LTR')

find("*[data-e2e-edit-article='#{existing_article.id}']").click
expect(page).to have_css("form#edit_article_version_#{existing_article.id}")
expect(page).to have_select('article_version_supplier_order_unit', options: ['Custom', 'kiloampere (kA)', 'kilogram (kg)', 'litre (l)', 'Package', 'Piece'])
end
expect(page).to have_content(article_version.name)
end
end

Expand Down

0 comments on commit 02bec16

Please sign in to comment.