# frozen_string_literal: true require 'spec_helper' require 'highline' require_relative '../../../../lib/balboa/punch_date' describe Balboa::CLI::Command::PunchCommand do class InteractorFaker def initialize(last, skips = []) @last = last @options = { 'skips' => skips } end attr_reader :last, :options def punch(date) end end it 'raises when skips is not an array' do interactor = InteractorFaker.new('bad date') command = described_class.new(interactor, nil) expect { command.execute }.to raise_error(ArgumentError) end it 'returns all punched dates' do today = Date.new(2015, 8, 15) last_punch_date = today - 7 allow(Date).to receive(:today).and_return(today) interactor = InteractorFaker.new(last_punch_date.to_s) command = described_class.new(interactor, nil) punched_dates = [ "\n10/08/2015", '11/08/2015', '12/08/2015', '13/08/2015', '14/08/2015' ] expect(command.execute).to eq(punched_dates.join("\n")) end it 'returns all punched dates except for the skipped ones' do today = Date.new(2015, 8, 15) last_punch_date = today - 7 skips = [Date.new(2015, 8, 12), Date.new(2015, 8, 14)] allow(Date).to receive(:today).and_return(today) interactor = InteractorFaker.new(last_punch_date.to_s, skips) command = described_class.new(interactor, nil) punched_dates = [ "\n10/08/2015", '11/08/2015', '13/08/2015' ] expect(command.execute).to eq(punched_dates.join("\n")) end it 'returns all punched dates except for the skipped ones' do today = Date.new(2015, 8, 15) last_punch_date = today - 7 skips = [Date.new(2015, 8, 12), Date.new(2015, 8, 14)] allow(Date).to receive(:today).and_return(today) interactor = InteractorFaker.new(last_punch_date.to_s, skips) command = described_class.new(interactor, nil) punched_dates = [ "\n10/08/2015", '11/08/2015', '13/08/2015' ] expect(command.execute).to eq(punched_dates.join("\n")) end it 'returns nothing when last punch date is recent' do today = Date.new(2015, 8, 15) last_punch_date = today skips = [Date.new(2015, 8, 12), Date.new(2015, 8, 14)] allow(Date).to receive(:today).and_return(today) interactor = InteractorFaker.new(last_punch_date.to_s, skips) command = described_class.new(interactor, nil) expect(command.execute).to eq('') end context 'on holidays' do it 'skips date when user input no' do today = Date.new(2015, 9, 10) last_punch_date = today - 7 input = StringIO.new('no\n') output = StringIO.new cli = HighLine.new(input, output) allow(Date).to receive(:today).and_return(today) interactor = InteractorFaker.new(last_punch_date.to_s) command = described_class.new(interactor, cli) punched_dates = [ "\n04/09/2015", '08/09/2015', '09/09/2015' ] expect(command.execute).to eq(punched_dates.join("\n")) end it 'returns date when user input yes' do today = Date.new(2015, 9, 10) last_punch_date = today - 7 input = StringIO.new('yes\n') output = StringIO.new cli = HighLine.new(input, output) allow(Date).to receive(:today).and_return(today) interactor = InteractorFaker.new(last_punch_date.to_s) command = described_class.new(interactor, cli) punched_dates = [ "\n04/09/2015", '07/09/2015', '08/09/2015', '09/09/2015' ] expect(command.execute).to eq(punched_dates.join("\n")) end end end