Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dsu browse command specs #53

Merged
merged 3 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [2.2.1] 2023-12-23

Changes

- Add specs for `dsu browse` command.
- Code refactors, nothing discernable to the end user.

## [2.2.0] 2023-12-23

Stable release.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
dsu (2.2.0)
dsu (2.2.1)
activemodel (>= 7.0.8, < 8.0)
activesupport (>= 7.0.8, < 8.0)
colorize (>= 0.8.1, < 1.0)
Expand Down
1 change: 1 addition & 0 deletions lib/dsu/subcommands/browse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Browse < BaseSubcommand
map %w[m] => :month
map %w[y] => :year

class_option :pager, default: true, type: :boolean, hide: true, aliases: '-p'
class_option :include_all, default: nil, type: :boolean, aliases: '-a',
desc: I18n.t('options.include_all')

Expand Down
2 changes: 1 addition & 1 deletion lib/dsu/support/entry_group_browsable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def browse_header_for(time:, options:)
end

def output_with_pager(output:, options:)
if options[:pager] == :no_pager
if options[:pager] == false
puts output
return
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dsu/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Dsu
VERSION_REGEX = /\A\d+\.\d+\.\d+(\.(alpha|rc)\.\d+)?\z/
VERSION = '2.2.0'
VERSION = '2.2.1'
end
6 changes: 5 additions & 1 deletion lib/dsu/views/entry_group/shared/no_entries_to_display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def initialize(times:, options: {})

# TODO: I18n.
def render
puts apply_theme(message, theme_color: color_theme.info)
puts render_as_string
end

def render_as_string
apply_theme(message, theme_color: color_theme.info)
end

private
Expand Down
134 changes: 116 additions & 18 deletions spec/dsu/features/dsu_browse_feature_spec.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,146 @@
# frozen_string_literal: true

RSpec.describe 'Dsu browse features', type: :feature do
subject(:cli) { Dsu::CLI.start(args) }
subject(:cli) do
strip_escapes(Dsu::Services::StdoutRedirectorService.call { Dsu::CLI.start(args) })
end

shared_examples 'help is displayed' do
it 'displays help' do
expect(cli).to include('rspec browse help [COMMAND]')
end
end

before do
freeze_time_at(time_string: time_string)
end

let(:options) { {} }
let(:time_string) { '2023-06-12' }
let(:configuration) do
build(:configuration)
end

context "when 'dsu help browse' is used" do
let(:args) { %w[help browse] }

it 'displays help' do
expect { cli }.to output(/Commands:.*rspec browse/m).to_stdout
end
it_behaves_like 'help is displayed'
end

context "when 'dsu browse COMMAND' is used" do
context 'with no COMMAND argument' do
let(:args) { %w[browse] }
let(:expected_output) do
'ERROR: "rspec browse" was called with no arguments'

it_behaves_like 'help is displayed'
end

context "with 'week'" do
before do
entry_groups
end

it 'an error is displayed to stderr'
let(:args) { %w[browse week --pager false] }
let(:time_string) { '2023-01-01' }
let(:entry_groups) do
times_for_week_of(Time.parse(time_string)).each.map do |time|
entries = [
build(:entry, description: "#{to_yyyymmdd_string(time)} Entry 1"),
build(:entry, description: "#{to_yyyymmdd_string(time)} Entry 2")
]
create(:entry_group, time: time, entries: entries)
end
end

it 'displays the entry groups for the week' do
entry_groups.each do |entry_group|
entry_group_header = Dsu::Support::TimeFormatable.formatted_time(time: entry_group.time)
expect(cli).to include(entry_group_header).and include(entry_group.entries[0].description).and include(entry_group.entries[1].description)
end
end
end

context 'with a mnemonic' do
context "with 'week'" do
let(:args) { %w[browse week] }
context "with 'month'" do
before do
entry_groups
end

it 'the entry groups for the week are displayed'
let(:args) { %w[browse month --pager false] }
let(:time_string) { '2023-01-01' }
let(:entry_groups) do
times_for_month_of(Time.parse(time_string)).each.map do |time|
entries = [
build(:entry, description: "#{to_yyyymmdd_string(time)} Entry 1"),
build(:entry, description: "#{to_yyyymmdd_string(time)} Entry 2")
]
create(:entry_group, time: time, entries: entries)
end
end

context "with 'month'" do
let(:args) { %w[browse month] }
it 'displays the entry groups for the month' do
entry_groups.each do |entry_group|
entry_group_header = Dsu::Support::TimeFormatable.formatted_time(time: entry_group.time)
expect(cli).to include(entry_group_header).and include(entry_group.entries[0].description).and include(entry_group.entries[1].description)
end
end
end

context "with 'year'" do
before do
entry_groups
end

it 'the entry groups for the month are displayed'
let(:args) { %w[browse year --pager false] }
let(:time_string) { '2023-01-01' }
let(:entry_groups) do
times_for_year_of(Time.parse(time_string)).each.map do |time|
entries = [
build(:entry, description: "#{to_yyyymmdd_string(time)} Entry 1"),
build(:entry, description: "#{to_yyyymmdd_string(time)} Entry 2")
]
create(:entry_group, time: time, entries: entries)
end
end

context "with 'year'" do
let(:args) { %w[browse year] }
it 'displays the entry groups for the year' do
entry_groups.each do |entry_group|
entry_group_header = Dsu::Support::TimeFormatable.formatted_time(time: entry_group.time)
expect(cli).to include(entry_group_header).and include(entry_group.entries[0].description).and include(entry_group.entries[1].description)
end
end
end

context "with 'week' and no entry groups and option :include_all is true" do
let(:args) { %w[browse week --include_all true --pager false] }
let(:time_string) { '2023-01-01' }
let(:times) { times_for_week_of(Time.parse(time_string)) }
let(:week_of) { Time.parse(time_string).in_time_zone }

it 'displays the dates for the week with no entry groups' do
nothing_to_display_header = Dsu::Views::EntryGroup::Shared::NoEntriesToDisplayForWeekOf.new(time: week_of).render_as_string
expect(cli).to include(strip_escapes(nothing_to_display_header))
end
end

context "with 'month' and no entry groups and option :include_all is true" do
let(:args) { %w[browse month --include_all true --pager false] }
let(:time_string) { '2023-01-01' }
let(:times) { times_for_month_of(Time.parse(time_string)) }
let(:month_of) { Time.parse(time_string).in_time_zone }

it 'displays the dates for the month with no entry groups' do
nothing_to_display_header = Dsu::Views::EntryGroup::Shared::NoEntriesToDisplayForMonthOf.new(time: month_of).render_as_string
expect(cli).to include(strip_escapes(nothing_to_display_header))
end
end

context "with 'year' and no entry groups and option :include_all is true" do
let(:args) { %w[browse year --include_all true --pager false] }
let(:time_string) { '2023-01-01' }
let(:times) { times_for_year_of(Time.parse(time_string)) }
let(:year_of) { Time.parse(time_string).in_time_zone }

it 'the entry groups for the year are displayed'
it 'displays the dates for the year with no entry groups' do
nothing_to_display_header = Dsu::Views::EntryGroup::Shared::NoEntriesToDisplayForYearOf.new(time: year_of).render_as_string
expect(cli).to include(strip_escapes(nothing_to_display_header))
end
end
end
Expand Down
35 changes: 34 additions & 1 deletion spec/dsu/support/entry_group_browsable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
# frozen_string_literal: true

RSpec.describe Dsu::Support::EntryGroupBrowsable do
it 'does something awesome'
subject(:entry_group_browsable) do
Class.new do
include Dsu::Support::EntryGroupBrowsable

def configuration
@configuration ||= FactoryBot.build(:configuration) # rubocop:disable FactoryBot/SyntaxMethods
end
end.new.browse_entry_groups(time: time, options: options)
end

let(:time) { Time.now }
let(:options) { { browse: :xyz, include_all: true, pager: false } }

describe '#browse_entry_groups' do
context 'when the :time argument is invalid' do
let(:time) { :bad }
let(:expected_error) { /time must be a Time object/ }

it_behaves_like 'an error is raised'
end

context 'when the :options argument is invalid' do
let(:options) { :bad }
let(:expected_error) { /options must be a Hash/ }

it_behaves_like 'an error is raised'
end

context 'when the :browse command is not valid' do
let(:expected_error) { /Unhandled option; expected :week, :month, or :year but received xyz/ }

it_behaves_like 'an error is raised'
end
end
end
2 changes: 1 addition & 1 deletion spec/support/time_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# objects
module TimeHelpers
def freeze_time_at(time_string:)
allow(Time).to receive(:now).and_return(Time.parse(time_string).in_time_zone)
allow(Time).to receive(:now).and_return(Time.parse(time_string))
end

def today_yyyymmdd_string
Expand Down