require 'spec_helper'

describe Staccato::Measurement::Checkout do
  let(:uri) {Staccato.ga_collection_uri}
  let(:tracker) {Staccato.tracker('UA-XXXX-Y')}
  let(:response) {double().tap {|o| o.stub(body: '', status: 201)}}

  before(:each) do
    allow(SecureRandom).to receive(:uuid).and_return('555')
    allow(Net::HTTP).to receive(:post_form).and_return(response)
  end

  context 'a pageview with a transaction' do
    let(:pageview) {
      tracker.build_pageview({
        path: '/checkout', hostname: 'mystore.com',
        title: 'Complete Your Checkout', product_action: 'checkout'
      })
    }

    let(:measurement_options) {{
      step: 1,
      step_options: 'Visa'
    }}

    before(:each) do
      pageview.add_measurement(:checkout, measurement_options)

      pageview.track!
    end

    it 'tracks the measurement' do
      expect(Net::HTTP).to have_received(:post_form).with(uri, {
        'v' => 1,
        'tid' => 'UA-XXXX-Y',
        'cid' => '555',
        't' => 'pageview',
        'dh' => 'mystore.com',
        'dp' => '/checkout',
        'dt' => 'Complete Your Checkout',
        'pa' => 'checkout',
        'cos' => 1,
        'col' => 'Visa'
      })
    end
  end
end
