<commit_before>require 'spec_helper'

feature 'As an administrateur I can edit types de champ', js: true do
  let(:administrateur) { procedure.administrateurs.first }
  let(:procedure) { create(:procedure) }

  before do
    Flipflop::FeatureSet.current.test!.switch!(:champ_repetition, true)
    login_as administrateur, scope: :administrateur
    visit champs_procedure_path(procedure)
  end

  it "Add a new champ" do
    click_on 'Supprimer'

    within '.buttons' do
      click_on 'Ajouter un champ'
    end
    expect(page).to have_selector('#champ-0-libelle')
    fill_in 'champ-0-libelle', with: 'libellé de champ'
    blur
    expect(page).to have_content('Formulaire enregistré')

    page.refresh
    within '.buttons' do
      click_on 'Enregistrer'
    end

    expect(page).to have_content('Formulaire enregistré')
  end

  it "Add multiple champs" do
    within '.buttons' do
      click_on 'Ajouter un champ'
      click_on 'Ajouter un champ'
      click_on 'Ajouter un champ'
    end
    page.refresh

    fill_in 'champ-0-libelle', with: 'libellé de champ 0'
    fill_in 'champ-1-libelle', with: 'libellé de champ 1'
    blur
    expect(page).to have_content('Formulaire enregistré')

    expect(page).to have_selector('#champ-0-libelle')
    expect(page).to have_selector('#champ-1-libelle')
    expect(page).to have_selector('#champ-2-libelle')
    expect(page).to have_selector('#champ-3-libelle')

    within '.type-de-champ[data-index="2"]' do
      click_on 'Supprimer'
    end

    expect(page).not_to have_selector('#champ-3-libelle')
    fill_in 'champ-2-libelle', with: 'libellé de champ 2'
    blur
    expect(page).to have_content('Formulaire enregistré')

    expect(page).to have_content('Supprimer', count: 3)

    page.refresh

    expect(page).to have_content('Supprimer', count: 3)
  end

  it "Remove champs" do
    fill_in 'champ-0-libelle', with: 'libellé de champ'
    blur
    expect(page).to have_content('Formulaire enregistré')
    page.refresh

    click_on 'Supprimer'
    expect(page).to have_content('Formulaire enregistré')
    expect(page).to have_content('Supprimer', count: 1)
    page.refresh

    expect(page).to have_content('Supprimer', count: 1)
  end

  it "Only add valid champs" do
    expect(page).to have_selector('#champ-0-description')
    fill_in 'champ-0-libelle', with: ''
    fill_in 'champ-0-description', with: 'déscription du champ'
    blur
    expect(page).not_to have_content('Formulaire enregistré')

    fill_in 'champ-0-libelle', with: 'libellé de champ'
    blur
    expect(page).to have_content('Formulaire enregistré')
  end

  it "Add repetition champ" do
    expect(page).to have_selector('#champ-0-libelle')
    select('Bloc répétable', from: 'champ-0-type_champ')
    fill_in 'champ-0-libelle', with: 'libellé de champ'
    blur

    expect(page).to have_content('Formulaire enregistré')
    page.refresh

    within '.type-de-champ .repetition' do
      click_on 'Ajouter un champ'
    end

    fill_in 'repetition-0-champ-0-libelle', with: 'libellé de champ 1'
    blur

    expect(page).to have_content('Formulaire enregistré')
    expect(page).to have_content('Supprimer', count: 2)

    within '.buttons' do
      click_on 'Ajouter un champ'
    end

    select('Bloc répétable', from: 'champ-0-type_champ')
    fill_in 'champ-0-libelle', with: 'libellé de champ 2'
    blur

    expect(page).to have_content('Supprimer', count: 3)
  end

  it "Add carte champ" do
    select('Carte', from: 'champ-0-type_champ')
    fill_in 'champ-0-libelle', with: 'Libellé de champ carte', fill_options: { clear: :backspace }
    blur
    check 'Quartiers prioritaires'
    expect(page).to have_content('Formulaire enregistré')

    preview_window = window_opened_by { click_on 'Prévisualiser le formulaire' }
    within_window(preview_window) do
      expect(page).to have_content('Libellé de champ carte')
      expect(page).to have_content('Quartiers prioritaires')
      expect(page).not_to have_content('Cadastres')
    end
  end

  it "Add dropdown champ" do
    select('Menu déroulant', from: 'champ-0-type_champ')
    fill_in 'champ-0-libelle', with: 'libellé de champ menu déroulant'
    blur
    fill_in 'champ-0-drop_down_list_value', with: 'Un menu'
    expect(page).to have_content('Formulaire enregistré')

    page.refresh

    expect(page).to have_content('Un menu')
  end
end<commit_msg>spec: fix flaky test by using new `wait_until` helper<commit_after>require 'spec_helper'

feature 'As an administrateur I can edit types de champ', js: true do
  let(:administrateur) { procedure.administrateurs.first }
  let(:procedure) { create(:procedure) }

  before do
    Flipflop::FeatureSet.current.test!.switch!(:champ_repetition, true)
    login_as administrateur, scope: :administrateur
    visit champs_procedure_path(procedure)
  end

  it "Add a new champ" do
    click_on 'Supprimer'

    within '.buttons' do
      click_on 'Ajouter un champ'
    end
    expect(page).to have_selector('#champ-0-libelle')
    fill_in 'champ-0-libelle', with: 'libellé de champ'
    blur
    expect(page).to have_content('Formulaire enregistré')

    page.refresh
    within '.buttons' do
      click_on 'Enregistrer'
    end

    expect(page).to have_content('Formulaire enregistré')
  end

  it "Add multiple champs" do
    within '.buttons' do
      click_on 'Ajouter un champ'
      click_on 'Ajouter un champ'
      click_on 'Ajouter un champ'
    end
    page.refresh

    fill_in 'champ-0-libelle', with: 'libellé de champ 0'
    fill_in 'champ-1-libelle', with: 'libellé de champ 1'
    blur
    expect(page).to have_content('Formulaire enregistré')

    expect(page).to have_selector('#champ-0-libelle')
    expect(page).to have_selector('#champ-1-libelle')
    expect(page).to have_selector('#champ-2-libelle')
    expect(page).to have_selector('#champ-3-libelle')

    within '.type-de-champ[data-index="2"]' do
      click_on 'Supprimer'
    end

    expect(page).not_to have_selector('#champ-3-libelle')
    fill_in 'champ-2-libelle', with: 'libellé de champ 2'
    blur
    expect(page).to have_content('Formulaire enregistré')

    expect(page).to have_content('Supprimer', count: 3)

    page.refresh

    expect(page).to have_content('Supprimer', count: 3)
  end

  it "Remove champs" do
    fill_in 'champ-0-libelle', with: 'libellé de champ'
    blur
    expect(page).to have_content('Formulaire enregistré')
    page.refresh

    click_on 'Supprimer'
    expect(page).to have_content('Formulaire enregistré')
    expect(page).to have_content('Supprimer', count: 1)
    page.refresh

    expect(page).to have_content('Supprimer', count: 1)
  end

  it "Only add valid champs" do
    expect(page).to have_selector('#champ-0-description')
    fill_in 'champ-0-libelle', with: ''
    fill_in 'champ-0-description', with: 'déscription du champ'
    blur
    expect(page).not_to have_content('Formulaire enregistré')

    fill_in 'champ-0-libelle', with: 'libellé de champ'
    blur
    expect(page).to have_content('Formulaire enregistré')
  end

  it "Add repetition champ" do
    expect(page).to have_selector('#champ-0-libelle')
    select('Bloc répétable', from: 'champ-0-type_champ')
    fill_in 'champ-0-libelle', with: 'libellé de champ'
    blur

    expect(page).to have_content('Formulaire enregistré')
    page.refresh

    within '.type-de-champ .repetition' do
      click_on 'Ajouter un champ'
    end

    fill_in 'repetition-0-champ-0-libelle', with: 'libellé de champ 1'
    blur

    expect(page).to have_content('Formulaire enregistré')
    expect(page).to have_content('Supprimer', count: 2)

    within '.buttons' do
      click_on 'Ajouter un champ'
    end

    select('Bloc répétable', from: 'champ-0-type_champ')
    fill_in 'champ-0-libelle', with: 'libellé de champ 2'
    blur

    expect(page).to have_content('Supprimer', count: 3)
  end

  it "Add carte champ" do
    select('Carte', from: 'champ-0-type_champ')
    fill_in 'champ-0-libelle', with: 'Libellé de champ carte', fill_options: { clear: :backspace }
    check 'Quartiers prioritaires'

    wait_until { procedure.types_de_champ.first.quartiers_prioritaires == true }
    expect(page).to have_content('Formulaire enregistré')

    preview_window = window_opened_by { click_on 'Prévisualiser le formulaire' }
    within_window(preview_window) do
      expect(page).to have_content('Libellé de champ carte')
      expect(page).to have_content('Quartiers prioritaires')
      expect(page).not_to have_content('Cadastres')
    end
  end

  it "Add dropdown champ" do
    select('Menu déroulant', from: 'champ-0-type_champ')
    fill_in 'champ-0-libelle', with: 'Libellé de champ menu déroulant', fill_options: { clear: :backspace }
    fill_in 'champ-0-drop_down_list_value', with: 'Un menu', fill_options: { clear: :backspace }

    wait_until { procedure.types_de_champ.first.drop_down_list&.value == 'Un menu' }
    expect(page).to have_content('Formulaire enregistré')

    page.refresh

    expect(page).to have_content('Un menu')
  end
end
{'fraction_non_alphanumeric': 0.12001329787234043, 'fraction_numerical': 0.008643617021276596, 'mean_word_length': 3.688311688311688, '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': '7388012', 'n_tokens_mistral': 3242, 'n_tokens_neox': 3131, 'n_words': 730}