<commit_before>require 'rails_helper'

describe 'Customizing a card' do
  let!(:template) { CardTemplate.create(greeting: "nevermind", image_file: "sorry_template_0.1.svg") }
  before(:each) { visit "/card_templates/#{template.id}/cards/new"   }
  before(:each) do
    fill_in_card_form(
      custom_message: 'Not having the sorry',
      signature: '<NAME>',
      recipient_name: '<NAME>',
      street_address: '123 Main Street',
      city: 'Oakland',
      state: 'CA',
      zip_code: '94607'
    )
  end

  describe 'Viewing the customization page' do
    it 'has the greeting for the template selected' do
      expect(page.find("#template-#{template[:id]}-image")).to_not be_nil
    end
  end

  describe 'Filling out the card form' do
    it 'Can create a card with all of the relevant fields' do
      card_count = Card.all.count
      click_button('Submit')
      expect(Card.all.count).to eq(card_count + 1)
    end
  end

  describe 'Paying for a card' do
    before(:each) { click_button('Submit') }

    describe 'Viewing the payment page' do
      describe 'After a valid card has been created' do
        it 'Shows the payment label' do
          expect(page).to have_content "Review Order"
        end

        it 'Still shows the greeting for the template selected' do
          expect(page.find("#template-#{template[:id]}-image")).to_not be_nil
        end

        it 'Still shows the custom details of the customized card' do
          expect(page).to have_content Card.last.signature
        end

        it 'Lists total price' do
          expect(page).to have_content 'Total: $4.99'
        end

        it 'Includes the Stripe checkout button', js: true do
          expect(page).to have_css '.stripe-button-el'
          expect(page.find_button('Pay with Card')).to_not be_nil
        end
      end

      describe 'If the card does not match the current session' do
        let(:sessionless_card){ Card.create({
          card_template_id: template.id,
          custom_message: 'Nonsense session_id',
          signature: 'Cause I made it manually',
          recipient_name: '<NAME>',
          street_address: '123 Main Street',
          city: 'Oakland',
          state: 'CA',
          zip_code: '94607',
          session_id: 'humbaba'
        }) }

        it 'Does not show the card details and payment section' do
          visit card_template_card_path(card_template_id: sessionless_card.card_template_id, id: sessionless_card.id)
          expect(page).to_not have_content "Review Order"
        end

        it 'Redirects to the new card page for the relevant template' do
          visit card_template_card_path(card_template_id: sessionless_card.card_template_id, id: sessionless_card.id)
          expect(page).to have_content "Customize Your Message"
          expect(page.find("#template-#{template[:id]}-image")).to_not be_nil
        end
      end

      describe 'If the user wants to go back and edit the card' do
        it 'Displays a back button' do
          expect(page.find_button('Back')).to_not be_nil
        end
      end
    end

# add test for showing the flash error if there is a problem with payment

  end
<commit_msg>Refactor some test structure and test for back button"<commit_after>require 'rails_helper'

describe 'Customizing a card' do
  let!(:template) { CardTemplate.create(greeting: "nevermind", image_file: "sorry_template_0.1.svg") }
  let!(:card_data) {{
    custom_message: 'Not having the sorry',
    signature: '<NAME>',
    recipient_name: '<NAME>',
    street_address: '123 Main Street',
    city: 'Oakland',
    state: 'CA',
    zip_code: '94607'
  }}
  before(:each) do
    visit "/card_templates/#{template.id}/cards/new"
    fill_in_card_form(card_data)
  end

  describe 'Viewing the customization page' do
    it 'has the greeting for the template selected' do
      expect(page.find("#template-#{template[:id]}-image")).to_not be_nil
    end
  end

  describe 'Filling out the card form' do
    it 'Can create a card with all of the relevant fields' do
      card_count = Card.all.count
      click_button('Submit')
      expect(Card.all.count).to eq(card_count + 1)
    end
  end

  describe 'Paying for a card' do
    before(:each) { click_button('Submit') }

    describe 'Viewing the payment page' do
      describe 'After a valid card has been created' do
        it 'Shows the payment label' do
          expect(page).to have_content "Review Order"
        end

        it 'Still shows the greeting for the template selected' do
          expect(page.find("#template-#{template[:id]}-image")).to_not be_nil
        end

        it 'Still shows the custom details of the customized card' do
          expect(page).to have_content Card.last.signature
        end

        it 'Lists total price' do
          expect(page).to have_content 'Total: $4.99'
        end

        it 'Includes the Stripe checkout button', js: true do
          expect(page).to have_css '.stripe-button-el'
          expect(page.find_button('Pay with Card')).to_not be_nil
        end
      end

      describe 'If the card does not match the current session' do
        let(:sessionless_card){ Card.create({
          card_template_id: template.id,
          custom_message: 'Nonsense session_id',
          signature: 'Cause I made it manually',
          recipient_name: '<NAME>',
          street_address: '123 Main Street',
          city: 'Oakland',
          state: 'CA',
          zip_code: '94607',
          session_id: 'humbaba'
        }) }

        it 'Does not show the card details and payment section' do
          visit card_template_card_path(card_template_id: sessionless_card.card_template_id, id: sessionless_card.id)
          expect(page).to_not have_content "Review Order"
        end

        it 'Redirects to the new card page for the relevant template' do
          visit card_template_card_path(card_template_id: sessionless_card.card_template_id, id: sessionless_card.id)
          expect(page).to have_content "Customize Your Message"
          expect(page.find("#template-#{template[:id]}-image")).to_not be_nil
        end
      end

      describe 'If the user wants to go back and edit the card' do
        it 'they can click the back button to edit' do
          click_button('Back')
          expect(page).to have_content "Customize Your Message"
          expect(page).to have_content card_data["custom_message"]
          expect(page.find("#template-#{template[:id]}-image")).to_not be_nil
        end
      end
    end

    # add test for showing the flash error if there is a problem with payment

  end

{'fraction_non_alphanumeric': 0.09505818346682787, 'fraction_numerical': 0.006649539066042013, 'mean_word_length': 2.642267473858008, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1660775', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1923, 'n_words': 617}