require "spec_helper"

feature "testing with teaspoon in the browser", js: true do

  let(:spec_helper) { nil }

  before do
    allow(Teaspoon.configuration).to receive(:suite_configs).and_return "integration" => {block: proc{ |suite|
      suite.matcher = "spec/dummy/app/assets/javascripts/integration/*_spec.{js,js.coffee,coffee}"
      suite.helper = spec_helper
    }}
  end

  scenario "gives me the expected results" do
    visit "/teaspoon/integration"

    within("#teaspoon-progress") do
      expect(find("em")).to have_text("100%")
    end

    within("#teaspoon-stats") do
      expect(find("li:nth-child(1)")).to have_text("passes: 4")
      expect(find("li:nth-child(2)")).to have_text("failures: 2")
      expect(find("li:nth-child(3)")).to have_text("skipped: 1")
    end

    within("#teaspoon-report-failures") do
      expect(find("li.spec:first-child")).to have_text("Integration tests allows failing specs.")
    end

    expect(find("#spec_helper_el")).to have_text("this was generated by the spec_helper")
  end

  describe "toggle buttons" do

    scenario "progress toggle button toggles progress display" do
      visit "/teaspoon/integration?reporter=HTML"

      find("#teaspoon-display-progress").click
      expect(page).not_to have_text("100%")

      find("#teaspoon-display-progress").click
      expect(page).to have_text("100%")
    end

  end

  describe "using turbolinks doesn't break things" do

    let(:spec_helper) { "turbolinks_helper" }

    scenario "executes in each page navigation" do
      visit "/teaspoon/integration"

      within("#teaspoon-progress") do
        expect(find("em")).to have_text("100%")
      end

      within("#teaspoon-report-failures") do
        find("li.spec:first-child a").click
      end

      within("#teaspoon-progress") do
        expect(find("em")).to have_text("100%")
      end
    end

  end

end
