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

Jacobperia component unit tests #85

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
3 changes: 1 addition & 2 deletions app/components/matey/active_users_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

class Matey::ActiveUsersComponent < Matey::ApplicationComponent
def initialize(events:, time_window: 1.week, color_scheme: "neutral")
raise ArgumentError unless events.is_a?(ActiveRecord::Relation)
raise ArgumentError unless time_window.is_a?(Integer)
validate_arguments(events, time_window)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way to try things up. I've not really thought about this type of validation before but seems to make sense and definitely better than including it in each file.

Thoughts on leveraging super as an alternative implementation? Not sure it is better ...

In this implementation, I think it would be nice to have named arguments so that the method signature would be:

validate_arguments(events: events, time_window: time_window)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was able to implement both the suggestions. Much cleaner now 🚀


@current_period = events.where(time: time_window.ago..Time.current).pluck(:user_id).uniq.count
previous_period = events.where(time: (2 * time_window).ago..time_window.ago).pluck(:user_id).uniq.count
Expand Down
5 changes: 5 additions & 0 deletions app/components/matey/application_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ class Matey::ApplicationComponent < ViewComponent::Base
def before_render
validate!
end

def validate_arguments(records, time_window)
raise ArgumentError unless records.is_a?(ActiveRecord::Relation)
raise ArgumentError unless time_window.is_a?(Integer)
end
end
2 changes: 2 additions & 0 deletions app/components/matey/browser_os_breakdown_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Matey::BrowserOsBreakdownComponent < Matey::ApplicationComponent
def initialize(visits:, time_window:, color_scheme: "neutral")
validate_arguments(visits, time_window)

visits_in_time_window = visits.where(started_at: time_window.ago..)
@visits_in_time_window = visits_in_time_window.count
@browsers = visits_in_time_window.group(:browser).count
Expand Down
2 changes: 2 additions & 0 deletions app/components/matey/daily_active_users_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Matey::DailyActiveUsersComponent < Matey::ApplicationComponent
def initialize(visits:, time_window:, color_scheme: "neutral")
validate_arguments(visits, time_window)

@visits = visits
@time_window = time_window
visits_in_time_window = visits.where(started_at: time_window.ago..)
Expand Down
3 changes: 1 addition & 2 deletions app/components/matey/new_activity_component.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Matey::NewActivityComponent < Matey::ApplicationComponent
def initialize(events:, time_window: 1.week, color_scheme: "neutral")
raise ArgumentError unless events.is_a?(ActiveRecord::Relation)
raise ArgumentError unless time_window.is_a?(Integer)
validate_arguments(events, time_window)

@current_period = events.where(time: time_window.ago..Time.current).count
previous_period = events.where(time: (2 * time_window).ago..time_window.ago).pluck(:user_id).count
Expand Down
2 changes: 2 additions & 0 deletions app/components/matey/new_users_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Matey::NewUsersComponent < Matey::ApplicationComponent
def initialize(users:, time_window: 1.week, color_scheme: "neutral")
validate_arguments(users, time_window)

@current_period = users.where(created_at: time_window.ago..Time.current).count
previous_period = users.where(created_at: (2 * time_window).ago..time_window.ago).count

Expand Down
3 changes: 1 addition & 2 deletions app/components/matey/top_events_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

class Matey::TopEventsComponent < Matey::ApplicationComponent
def initialize(events:, time_window: 1.week, limit: 5, color_scheme: "neutral")
raise ArgumentError unless events.is_a?(ActiveRecord::Relation)
raise ArgumentError unless time_window.is_a?(Integer)
validate_arguments(events, time_window)

@events = events.where(time: time_window.ago..Time.current).limit(limit).order("count(name) DESC").group(:name).count
@time_window = time_window
Expand Down
2 changes: 2 additions & 0 deletions app/components/matey/top_visited_pages_table_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Matey::TopVisitedPagesTableComponent < Matey::ApplicationComponent
def initialize(events:, time_window: 1.week, limit: 10, color_scheme: "neutral")
validate_arguments(events, time_window)

# Group events by controller (:name) and action. Aggregate number of unique user actions
@user_count_by_event = events.where(started_at: time_window.ago..).pluck(:landing_page).tally

Expand Down
2 changes: 2 additions & 0 deletions app/components/matey/user_engagement_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class Matey::UserEngagementComponent < Matey::ApplicationComponent
def initialize(events:, user_id:, time_window: 1.week, limit: 10, color_scheme: "neutral")
validate_arguments(events, time_window)

@events_for_user = events.where_props(user_id: user_id).where(time: time_window.ago..Time.current).group(:name).count
@count_by_event = @events_for_user.sort_by { |event, count| count }.last(limit).reverse
@time_window = time_window
Expand Down
2 changes: 2 additions & 0 deletions app/components/matey/visits_by_day_of_week_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

class Matey::VisitsByDayOfWeekComponent < Matey::ApplicationComponent
def initialize(visits:, time_window: 1.month, exclude_days: [], color_scheme: "neutral")
validate_arguments(visits, time_window)

@visits = visits
@time_window = time_window
@exclude_days = exclude_days
Expand Down
28 changes: 28 additions & 0 deletions spec/matey/bounce_rate_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Matey::BounceRateComponent, type: :component do
context "renders component" do
let(:color_scheme) { "bg-light text-dark border-dark" }
it "with a card element" do
subject = render_inline(Matey::BounceRateComponent.new(events: Ahoy::Event.all, visits: Ahoy::Visit.all))

expect(subject.css("div[class='card #{color_scheme}']").to_html).not_to be_empty
end
end

context "raises an exception when" do
it "events are missing" do
expect { Matey::BounceRateComponent.new }.to raise_error(ArgumentError)
end

it "events are invalid" do
expect { Matey::BounceRateComponent.new(events: nil) }.to raise_error(ArgumentError)
end

it "time_window is invalid" do
expect { Matey::BounceRateComponent.new(events: [], time_window: nil) }.to raise_error(ArgumentError)
end
end
end
28 changes: 28 additions & 0 deletions spec/matey/browser_os_breakdown_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Matey::BrowserOsBreakdownComponent, type: :component do
context "renders component" do
let(:color_scheme) { "bg-light text-dark border-dark" }
it "with a card element" do
subject = render_inline(Matey::BrowserOsBreakdownComponent.new(visits: Ahoy::Visit.all, time_window: 1.week))

expect(subject.css("div[class='card #{color_scheme}']").to_html).not_to be_empty
end
end

context "raises an exception when" do
it "visits are missing" do
expect { Matey::BrowserOsBreakdownComponent.new }.to raise_error(ArgumentError)
end

it "visits are invalid" do
expect { Matey::BrowserOsBreakdownComponent.new(visits: nil) }.to raise_error(ArgumentError)
end

it "time_window is invalid" do
expect { Matey::BrowserOsBreakdownComponent.new(visits: [], time_window: nil) }.to raise_error(ArgumentError)
end
end
end
28 changes: 28 additions & 0 deletions spec/matey/daily_active_users_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Matey::DailyActiveUsersComponent, type: :component do
context "renders component" do
let(:color_scheme) { "bg-light text-dark border-dark" }
it "with a card element" do
subject = render_inline(Matey::DailyActiveUsersComponent.new(visits: Ahoy::Visit.all, time_window: 1.month))

expect(subject.css("div[class='card #{color_scheme}']").to_html).not_to be_empty
end
end

context "raises an exception when" do
it "visits are missing" do
expect { Matey::DailyActiveUsersComponent.new }.to raise_error(ArgumentError)
end

it "visits are invalid" do
expect { Matey::DailyActiveUsersComponent.new(visits: nil) }.to raise_error(ArgumentError)
end

it "time_window is invalid" do
expect { Matey::DailyActiveUsersComponent.new(visits: [], time_window: nil) }.to raise_error(ArgumentError)
end
end
end
28 changes: 28 additions & 0 deletions spec/matey/new_activity_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Matey::NewActivityComponent, type: :component do
context "when ahoy events are present" do
let(:color_scheme) { "bg-light text-dark border-dark" }
it "renders the card element" do
subject = render_inline(Matey::NewActivityComponent.new(events: Ahoy::Event.all))

expect(subject.css("div[class='card #{color_scheme}']").to_html).not_to be_empty
end
end

context "raises an exception when" do
it "events are missing" do
expect { Matey::NewActivityComponent.new }.to raise_error(ArgumentError)
end

it "events are invalid" do
expect { Matey::NewActivityComponent.new(events: nil) }.to raise_error(ArgumentError)
end

it "time_window is invalid" do
expect { Matey::NewActivityComponent.new(events: [], time_window: nil) }.to raise_error(ArgumentError)
end
end
end
28 changes: 28 additions & 0 deletions spec/matey/new_users_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Matey::NewUsersComponent, type: :component do
context "renders component" do
let(:color_scheme) { "bg-light text-dark border-dark" }
it "with a card element" do
subject = render_inline(Matey::NewUsersComponent.new(users: User.all))

expect(subject.css("div[class='card #{color_scheme}']").to_html).not_to be_empty
end
end

context "raises an exception when" do
it "users are missing" do
expect { Matey::NewUsersComponent.new }.to raise_error(ArgumentError)
end

it "users are invalid" do
expect { Matey::NewUsersComponent.new(users: nil) }.to raise_error(ArgumentError)
end

it "time_window is invalid" do
expect { Matey::NewUsersComponent.new(users: [], time_window: nil) }.to raise_error(ArgumentError)
end
end
end
28 changes: 28 additions & 0 deletions spec/matey/top_visited_pages_table_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Matey::TopVisitedPagesTableComponent, type: :component do
context "renders component" do
let(:color_scheme) { "bg-light text-dark border-dark" }
it "with a card element" do
subject = render_inline(Matey::TopVisitedPagesTableComponent.new(events: Ahoy::Visit.all))

expect(subject.css("div[class='card #{color_scheme}']").to_html).not_to be_empty
end
end

context "raises an exception when" do
it "events are missing" do
expect { Matey::TopVisitedPagesTableComponent.new }.to raise_error(ArgumentError)
end

it "events are invalid" do
expect { Matey::TopVisitedPagesTableComponent.new(events: nil) }.to raise_error(ArgumentError)
end

it "time_window is invalid" do
expect { Matey::TopVisitedPagesTableComponent.new(events: [], time_window: nil) }.to raise_error(ArgumentError)
end
end
end
29 changes: 29 additions & 0 deletions spec/matey/user_engagement_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Matey::UserEngagementComponent, type: :component do
context "when ahoy events are present" do
let(:user) { create(:user) }
let(:color_scheme) { "bg-light text-dark border-dark" }
it "renders the card element" do
subject = render_inline(Matey::UserEngagementComponent.new(events: Ahoy::Event.all, user_id: user.id))

expect(subject.css("div[class='card #{color_scheme}']").to_html).not_to be_empty
end
end

context "raises an exception when" do
it "events are missing" do
expect { Matey::UserEngagementComponent.new }.to raise_error(ArgumentError)
end

it "events are invalid" do
expect { Matey::UserEngagementComponent.new(events: nil) }.to raise_error(ArgumentError)
end

it "time_window is invalid" do
expect { Matey::UserEngagementComponent.new(events: [], time_window: nil) }.to raise_error(ArgumentError)
end
end
end
28 changes: 28 additions & 0 deletions spec/matey/visits_by_day_of_week_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Matey::VisitsByDayOfWeekComponent, type: :component do
context "when ahoy visits are present" do
let(:color_scheme) { "bg-light text-dark border-dark" }
it "renders the card element" do
subject = render_inline(Matey::VisitsByDayOfWeekComponent.new(visits: Ahoy::Visit.all, exclude_days: ["Monday", "Tuesday"]))

expect(subject.css("div[class='card #{color_scheme}']").to_html).not_to be_empty
end
end

context "raises an exception when" do
it "visits are missing" do
expect { Matey::VisitsByDayOfWeekComponent.new }.to raise_error(ArgumentError)
end

it "visits are invalid" do
expect { Matey::VisitsByDayOfWeekComponent.new(visits: nil) }.to raise_error(ArgumentError)
end

it "time_window is invalid" do
expect { Matey::VisitsByDayOfWeekComponent.new(visits: [], time_window: nil) }.to raise_error(ArgumentError)
end
end
end
Loading