<commit_before>require 'spec_helper'

feature %q{
    As an administrator
    I want to manage order cycles
}, js: true do
  include AuthenticationWorkflow
  include WebHelper

  scenario "listing order cycles" do
    # Given some order cycles (created in an arbitrary order)
    oc4 = create(:simple_order_cycle, name: '4',
                 orders_open_at: 2.day.from_now, orders_close_at: 1.month.from_now)
    oc2 = create(:simple_order_cycle, name: '2', orders_close_at: 1.month.from_now)
    oc6 = create(:simple_order_cycle, name: '6',
                 orders_open_at: 1.month.ago, orders_close_at: 3.weeks.ago)
    oc3 = create(:simple_order_cycle, name: '3',
                 orders_open_at: 1.day.from_now, orders_close_at: 1.month.from_now)
    oc5 = create(:simple_order_cycle, name: '5',
                 orders_open_at: 1.month.ago, orders_close_at: 2.weeks.ago)
    oc1 = create(:order_cycle, name: '1')
    oc0 = create(:simple_order_cycle, name: '0',
                 orders_open_at: nil, orders_close_at: nil)

    # When I go to the admin order cycles page
    login_to_admin_section
    click_link 'Order Cycles'

    # Then the order cycles should be ordered correctly
    page.all('#listing_order_cycles tr td:first-child').map(&:text).should ==
      ['0', '1', '2', '3', '4', '5', '6']

    # And the rows should have the correct classes
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc0.id}.undated"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc1.id}.open"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc2.id}.open"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc3.id}.upcoming"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc4.id}.upcoming"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc5.id}.closed"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc6.id}.closed"

    # And I should see all the details for an order cycle
    # (the table includes a hidden field between each row, making this nth-child(3) instead of 2)
    within('table#listing_order_cycles tbody tr:nth-child(3)') do
      # Then I should see the basic fields
      page.should have_selector 'a', text: oc1.name

      page.should have_selector "input[value='#{oc1.orders_open_at}']"
      page.should have_selector "input[value='#{oc1.orders_close_at}']"
      page.should have_content oc1.coordinator.name

      # And I should see the suppliers and distributors
      oc1.suppliers.each    { |s| page.should have_content s.name }
      oc1.distributors.each { |d| page.should have_content d.name }

      # And I should see the number of variants
      page.should have_selector 'td.products', text: '2 variants'
    end
  end

  describe 'listing order cycles with other locales' do
    let!(:oc_de) { create(:simple_order_cycle, name: 'oc', orders_open_at: '2012-01-01 00:00:00') }

    around(:each) do |spec|
      I18n.locale = :de
      spec.run
      I18n.locale = :en
    end

    context 'using datepickers' do
      it "correctly open the datepicker and changes the date field" do
        login_to_admin_section
        visit admin_order_cycles_path

        within("tr.order-cycle-#{oc_de.id}") do
          expect(all('input').first.value).to start_with '2012-01-01 00:00:00'
          find('img.ui-datepicker-trigger', match: :first).click
        end

        within("#ui-datepicker-div") do
          expect(page).to have_selector 'a.ui-state-active', text: '1'

          click_link '30'
          find('button.ui-datepicker-close', match: :first).click
        end

        within("tr.order-cycle-#{oc_de.id}") do
          expect(all('input').first.value).to eq '2012-01-30 00:00'
        end
      end
    end<commit_msg>Tidy up order cycle spec<commit_after>require 'spec_helper'

feature %q{
    As an administrator
    I want to manage order cycles
}, js: true do
  include AuthenticationWorkflow
  include WebHelper

  scenario "listing order cycles" do
    # Given some order cycles (created in an arbitrary order)
    oc4 = create(:simple_order_cycle, name: '4',
                 orders_open_at: 2.day.from_now, orders_close_at: 1.month.from_now)
    oc2 = create(:simple_order_cycle, name: '2', orders_close_at: 1.month.from_now)
    oc6 = create(:simple_order_cycle, name: '6',
                 orders_open_at: 1.month.ago, orders_close_at: 3.weeks.ago)
    oc3 = create(:simple_order_cycle, name: '3',
                 orders_open_at: 1.day.from_now, orders_close_at: 1.month.from_now)
    oc5 = create(:simple_order_cycle, name: '5',
                 orders_open_at: 1.month.ago, orders_close_at: 2.weeks.ago)
    oc1 = create(:order_cycle, name: '1')
    oc0 = create(:simple_order_cycle, name: '0',
                 orders_open_at: nil, orders_close_at: nil)

    # When I go to the admin order cycles page
    login_to_admin_section
    click_link 'Order Cycles'

    # Then the order cycles should be ordered correctly
    page.all('#listing_order_cycles tr td:first-child').map(&:text).should ==
      ['0', '1', '2', '3', '4', '5', '6']

    # And the rows should have the correct classes
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc0.id}.undated"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc1.id}.open"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc2.id}.open"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc3.id}.upcoming"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc4.id}.upcoming"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc5.id}.closed"
    page.should have_selector "#listing_order_cycles tr.order-cycle-#{oc6.id}.closed"

    # And I should see all the details for an order cycle
    # (the table includes a hidden field between each row, making this nth-child(3) instead of 2)
    within('table#listing_order_cycles tbody tr:nth-child(3)') do
      # Then I should see the basic fields
      page.should have_selector 'a', text: oc1.name

      page.should have_selector "input[value='#{oc1.orders_open_at}']"
      page.should have_selector "input[value='#{oc1.orders_close_at}']"
      page.should have_content oc1.coordinator.name

      # And I should see the suppliers and distributors
      oc1.suppliers.each    { |s| page.should have_content s.name }
      oc1.distributors.each { |d| page.should have_content d.name }

      # And I should see the number of variants
      page.should have_selector 'td.products', text: '2 variants'
    end
  end

  describe 'listing order cycles with other locales' do
    let!(:oc_de) { create(:simple_order_cycle, name: 'oc', orders_open_at: '2012-01-01 00:00:00') }

    around(:each) do |spec|
      I18n.locale = :de
      spec.run
      I18n.locale = :en
    end

    context 'using datepickers' do
      it "correctly opens the datepicker and changes the date field" do
        login_to_admin_section
        visit admin_order_cycles_path

        within("tr.order-cycle-#{oc_de.id}") do
          expect(find('input.datetimepicker', match: :first).value).to start_with '2012-01-01 00:00:00'
          find('img.ui-datepicker-trigger', match: :first).click
        end

        within("#ui-datepicker-div") do
          expect(page).to have_selector 'a.ui-state-active', text: '1'

          click_link '30'
          find('button.ui-datepicker-close', match: :first).click
        end

        within("tr.order-cycle-#{oc_de.id}") do
          expect(find('input.datetimepicker', match: :first).value).to eq '2012-01-30 00:00'
        end
      end
    end
{'fraction_non_alphanumeric': 0.11732570239334027, 'fraction_numerical': 0.02445369406867846, 'mean_word_length': 3.1629669734704926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2544724', 'n_tokens_mistral': 2616, 'n_tokens_neox': 2473, 'n_words': 627}