<commit_before>require 'spec_helper'

feature 'Customers' do
  include AdminHelper
  include AuthenticationWorkflow
  include WebHelper

  context "as an enterprise user" do
    let(:user) { create_enterprise_user(enterprise_limit: 10) }
    let(:managed_distributor1) { create(:distributor_enterprise, owner: user) }
    let(:managed_distributor2) { create(:distributor_enterprise, owner: user) }
    let(:unmanaged_distributor) { create(:distributor_enterprise) }

    describe "using the customers index", js: true do
      let!(:customer1) { create(:customer, enterprise: managed_distributor1, code: nil) }
      let!(:customer2) { create(:customer, enterprise: managed_distributor1, code: nil) }
      let!(:customer3) { create(:customer, enterprise: unmanaged_distributor) }
      let!(:customer4) { create(:customer, enterprise: managed_distributor2) }

      before do
        quick_login_as user
        visit admin_customers_path
      end

      it "passes the smoke test" do
        # Prompts for a hub for a list of my managed enterprises
        expect(page).to have_select2 "shop_id", with_options: [managed_distributor1.name,managed_distributor2.name], without_options: [unmanaged_distributor.name]

        select2_select managed_distributor2.name, from: "shop_id"

        # Loads the right customers
        expect(page).to have_no_selector "tr#c_#{customer1.id}"
        expect(page).to have_no_selector "tr#c_#{customer2.id}"
        expect(page).to have_no_selector "tr#c_#{customer3.id}"
        expect(page).to have_selector "tr#c_#{customer4.id}"

        # Changing Shops
        select2_select managed_distributor1.name, from: "shop_id"

        # Loads the right customers
        expect(page).to have_selector "tr#c_#{customer1.id}"
        expect(page).to have_selector "tr#c_#{customer2.id}"
        expect(page).to have_no_selector "tr#c_#{customer3.id}"
        expect(page).to have_no_selector "tr#c_#{customer4.id}"

        # Searching
        fill_in "quick_search", with: customer2.email
        expect(page).to have_no_selector "tr#c_#{customer1.id}"
        expect(page).to have_selector "tr#c_#{customer2.id}"
        fill_in "quick_search", with: ""

        # Sorting when the header of a sortable column is clicked
        customer_emails = [customer1.email, customer2.email].sort
        within "#customers thead" do
          click_on "Email"
        end
        expect(page).to have_selector("#customers .customer:nth-child(1) .email", text: customer_emails[0])
        expect(page).to have_selector("#customers .customer:nth-child(2) .email", text: customer_emails[1])

        # Then sorting in reverse when the header is clicked again
        within "#customers thead" do
          click_on "Email"
        end
        expect(page).to have_selector("#customers .customer:nth-child(1) .email", text: customer_emails[1])
        expect(page).to have_selector("#customers .customer:nth-child(2) .email", text: customer_emails[0])

        # Toggling columns
        expect(page).to have_selector "th.email"
        expect(page).to have_content customer1.email
        toggle_columns "Email"
        expect(page).to have_no_selector "th.email"
        expect(page).to have_no_content customer1.email

        # Deleting
        create(:order, customer: customer1)
        expect{
          within "tr#c_#{customer1.id}" do
            find("a.delete-customer").trigger('click')
          end
          expect(page).to have_selector "#info-dialog .text", text: I18n.t('admin.customers.destroy.has_associated_orders')
          click_button "OK"
        }.to_not change{Customer.count}

        expect{
          within "tr#c_#{customer2.id}" do
            find("a.delete-customer").click
          end
          expect(page).to have_no_selector "tr#c_#{customer2.id}"
        }.to change{Customer.count}.by(-1)
      end

      it "allows updating of attributes" do
        select2_select managed_distributor1.name, from: "shop_id"

        within "tr#c_#{customer1.id}" do
          find_field('name').value.should eq '<NAME>'

          fill_in "code", with: "new-customer-code"
          expect(page).to have_css "input[name=code].update-pending"

          fill_in "name", with: "customer abc"
          expect(page).to have_css "input[name=name].update-pending"

          find(:css, "tags-input .tags input").set "awesome\n"
          expect(page).to have_css ".tag_watcher.update-pending"
        end
        expect(page).to have_content I18n.t('admin.unsaved_changes')
        click_button "Save Changes"

        # Every says it updated
        expect(page).to have_css "input[name=code].update-success"
        expect(page).to have_css "input[name=name].update-success"
        expect(page).to have_css ".tag_watcher.update-success"

        # And it actually did
        expect(customer1.reload.code).to eq "new-customer-code"
        expect(customer1.reload.name).to eq "customer abc"
        expect(customer1.tag_list).to eq ["awesome"]

        # Clearing attributes
        within "tr#c_#{customer1.id}" do
          fill_in "code", with: ""
          expect(page).to have_css "input[name=code].update-pending"

          fill_in "name", with: ""
          expect(page).to have_css "input[name=name].update-pending"

          find("tags-input li.tag-item a.remove-button").trigger('click')
          expect(page).to have_css ".tag_watcher.update-pending"<commit_msg>Update customers_spec to work with Selenium<commit_after>require 'spec_helper'

feature 'Customers' do
  include AdminHelper
  include AuthenticationWorkflow
  include WebHelper

  context "as an enterprise user" do
    let(:user) { create_enterprise_user(enterprise_limit: 10) }
    let(:managed_distributor1) { create(:distributor_enterprise, owner: user) }
    let(:managed_distributor2) { create(:distributor_enterprise, owner: user) }
    let(:unmanaged_distributor) { create(:distributor_enterprise) }

    describe "using the customers index", js: true do
      let!(:customer1) { create(:customer, enterprise: managed_distributor1, code: nil) }
      let!(:customer2) { create(:customer, enterprise: managed_distributor1, code: nil) }
      let!(:customer3) { create(:customer, enterprise: unmanaged_distributor) }
      let!(:customer4) { create(:customer, enterprise: managed_distributor2) }

      before do
        quick_login_as user
        visit admin_customers_path
      end

      it "passes the smoke test" do
        # Prompts for a hub for a list of my managed enterprises
        expect(page).to have_select2 "shop_id", with_options: [managed_distributor1.name,managed_distributor2.name], without_options: [unmanaged_distributor.name]

        select2_select managed_distributor2.name, from: "shop_id"

        # Loads the right customers
        expect(page).to have_no_selector "tr#c_#{customer1.id}"
        expect(page).to have_no_selector "tr#c_#{customer2.id}"
        expect(page).to have_no_selector "tr#c_#{customer3.id}"
        expect(page).to have_selector "tr#c_#{customer4.id}"

        # Changing Shops
        select2_select managed_distributor1.name, from: "shop_id"

        # Loads the right customers
        expect(page).to have_selector "tr#c_#{customer1.id}"
        expect(page).to have_selector "tr#c_#{customer2.id}"
        expect(page).to have_no_selector "tr#c_#{customer3.id}"
        expect(page).to have_no_selector "tr#c_#{customer4.id}"

        # Searching
        fill_in "quick_search", with: customer2.email
        expect(page).to have_no_selector "tr#c_#{customer1.id}"
        expect(page).to have_selector "tr#c_#{customer2.id}"
        fill_in "quick_search", with: ""

        # Sorting when the header of a sortable column is clicked
        customer_emails = [customer1.email, customer2.email].sort
        within "#customers thead" do
          click_on "Email"
        end
        expect(page).to have_selector("#customers .customer:nth-child(1) .email", text: customer_emails[0])
        expect(page).to have_selector("#customers .customer:nth-child(2) .email", text: customer_emails[1])

        # Then sorting in reverse when the header is clicked again
        within "#customers thead" do
          click_on "Email"
        end
        expect(page).to have_selector("#customers .customer:nth-child(1) .email", text: customer_emails[1])
        expect(page).to have_selector("#customers .customer:nth-child(2) .email", text: customer_emails[0])

        # Toggling columns
        expect(page).to have_selector "th.email"
        expect(page).to have_content customer1.email
        toggle_columns "Email"
        expect(page).to have_no_selector "th.email"
        expect(page).to have_no_content customer1.email

        # Deleting
        create(:order, customer: customer1)
        expect{
          within "tr#c_#{customer1.id}" do
            accept_alert do
              find("a.delete-customer").click
            end
          end
          expect(page).to have_selector "#info-dialog .text", text: I18n.t('admin.customers.destroy.has_associated_orders')
          click_button "OK"
        }.to_not change{Customer.count}

        expect{
          within "tr#c_#{customer2.id}" do
            accept_alert do
              find("a.delete-customer").click
            end
          end
          expect(page).to have_no_selector "tr#c_#{customer2.id}"
        }.to change{Customer.count}.by(-1)
      end

      it "allows updating of attributes" do
        select2_select managed_distributor1.name, from: "shop_id"

        within "tr#c_#{customer1.id}" do
          find_field('name').value.should eq '<NAME>'

          fill_in "code", with: "new-customer-code"
          expect(page).to have_css "input[name=code].update-pending"

          fill_in "name", with: "customer abc"
          expect(page).to have_css "input[name=name].update-pending"

          find(:css, "tags-input .tags input").set "awesome\n"
          expect(page).to have_css ".tag_watcher.update-pending"
        end
        expect(page).to have_content I18n.t('admin.unsaved_changes')
        click_button "Save Changes"

        # Every says it updated
        expect(page).to have_css "input[name=code].update-success"
        expect(page).to have_css "input[name=name].update-success"
        expect(page).to have_css ".tag_watcher.update-success"

        # And it actually did
        expect(customer1.reload.code).to eq "new-customer-code"
        expect(customer1.reload.name).to eq "customer abc"
        expect(customer1.tag_list).to eq ["awesome"]

        # Clearing attributes
        within "tr#c_#{customer1.id}" do
          fill_in "code", with: ""
          expect(page).to have_css "input[name=code].update-pending"

          fill_in "name", with: ""
          expect(page).to have_css "input[name=name].update-pending"

          find("tags-input li.tag-item a.remove-button").click
          expect(page).to have_css ".tag_watcher.update-pending"
{'fraction_non_alphanumeric': 0.11701350857977363, 'fraction_numerical': 0.0104052573932092, 'mean_word_length': 2.9670528602461985, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7042989', 'n_tokens_mistral': 3374, 'n_tokens_neox': 3304, 'n_words': 710}