-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from loftwah/dl/baseline-tests
Dl/baseline tests
- Loading branch information
Showing
20 changed files
with
483 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# spec/controllers/achievements_controller_spec.rb | ||
require 'rails_helper' | ||
|
||
RSpec.describe AchievementsController, type: :controller do | ||
let(:user) { create(:user) } | ||
let(:achievement) { create(:achievement, user: user) } | ||
|
||
describe "GET #index" do | ||
it "returns a success response" do | ||
get :index | ||
expect(response).to be_successful | ||
end | ||
end | ||
|
||
describe "GET #show" do | ||
context "when achievement doesn't have a URL" do | ||
it "returns a success response" do | ||
get :show, params: { id: achievement.to_param } | ||
expect(response).to be_successful | ||
end | ||
end | ||
|
||
context "when achievement has a URL" do | ||
let(:achievement_with_url) { create(:achievement, user: user, url: 'http://example.com') } | ||
|
||
it "redirects to the achievement URL" do | ||
get :show, params: { id: achievement_with_url.to_param } | ||
expect(response).to redirect_to(achievement_with_url.url) | ||
end | ||
end | ||
|
||
it "creates an AchievementView" do | ||
expect { | ||
get :show, params: { id: achievement.to_param } | ||
}.to change(AchievementView, :count).by(1) | ||
end | ||
end | ||
|
||
describe "GET #new" do | ||
it "returns a success response" do | ||
sign_in user | ||
get :new | ||
expect(response).to be_successful | ||
end | ||
end | ||
|
||
describe "POST #create" do | ||
context "with valid params" do | ||
it "creates a new Achievement" do | ||
sign_in user | ||
expect { | ||
post :create, params: { achievement: attributes_for(:achievement) } | ||
}.to change(Achievement, :count).by(1) | ||
end | ||
end | ||
end | ||
|
||
describe "PUT #update" do | ||
context "with valid params" do | ||
let(:new_attributes) { { title: "New Title" } } | ||
|
||
it "updates the requested achievement" do | ||
sign_in user | ||
put :update, params: { id: achievement.to_param, achievement: new_attributes } | ||
achievement.reload | ||
expect(achievement.title).to eq("New Title") | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE #destroy" do | ||
it "destroys the requested achievement" do | ||
sign_in user | ||
achievement # ensure achievement is created before the expect block | ||
expect { | ||
delete :destroy, params: { id: achievement.to_param } | ||
}.to change(Achievement, :count).by(-1) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# spec/controllers/analytics_controller_spec.rb | ||
require 'rails_helper' | ||
|
||
RSpec.describe AnalyticsController, type: :controller do | ||
let(:user) { create(:user) } | ||
|
||
before do | ||
sign_in user | ||
create(:daily_metric, user: user, date: Date.today) | ||
end | ||
|
||
describe "GET #index" do | ||
it "returns a success response" do | ||
get :index | ||
expect(response).to be_successful | ||
end | ||
|
||
it "assigns the correct instance variables" do | ||
get :index | ||
expect(assigns(:total_page_views)).to be_a(Integer) | ||
expect(assigns(:total_link_clicks)).to be_a(Integer) | ||
expect(assigns(:total_achievement_views)).to be_a(Integer) | ||
expect(assigns(:unique_visitors)).to be_a(Integer) | ||
expect(assigns(:latest_daily_metric)).to be_a(DailyMetric) | ||
expect(assigns(:link_analytics)).to be_an(Array) | ||
expect(assigns(:achievement_analytics)).to be_an(Array) | ||
expect(assigns(:daily_views)).to be_a(Hash) | ||
expect(assigns(:browser_data)).to be_a(Hash) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# spec/controllers/links_controller_spec.rb | ||
require 'rails_helper' | ||
|
||
RSpec.describe LinksController, type: :controller do | ||
let(:user) { create(:user) } | ||
let(:link) { create(:link, user: user) } | ||
|
||
describe "GET #index" do | ||
it "returns a success response" do | ||
get :index | ||
expect(response).to be_successful | ||
end | ||
end | ||
|
||
describe "GET #show" do | ||
it "returns a success response" do | ||
get :show, params: { id: link.to_param } | ||
expect(response).to be_successful | ||
end | ||
end | ||
|
||
describe "GET #new" do | ||
it "returns a success response" do | ||
sign_in user | ||
get :new | ||
expect(response).to be_successful | ||
end | ||
end | ||
|
||
describe "POST #create" do | ||
context "with valid params" do | ||
it "creates a new Link" do | ||
sign_in user | ||
expect { | ||
post :create, params: { link: attributes_for(:link) } | ||
}.to change(Link, :count).by(1) | ||
end | ||
end | ||
end | ||
|
||
describe "PUT #update" do | ||
context "with valid params" do | ||
let(:new_attributes) { { title: "New Title" } } | ||
|
||
it "updates the requested link" do | ||
sign_in user | ||
put :update, params: { id: link.to_param, link: new_attributes } | ||
link.reload | ||
expect(link.title).to eq("New Title") | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE #destroy" do | ||
it "destroys the requested link" do | ||
sign_in user | ||
link # ensure link is created before the expect block | ||
expect { | ||
delete :destroy, params: { id: link.to_param } | ||
}.to change(Link, :count).by(-1) | ||
end | ||
end | ||
|
||
describe "GET #user_links" do | ||
it "returns a success response" do | ||
get :user_links, params: { username: user.username } | ||
expect(response).to be_successful | ||
end | ||
|
||
it "assigns the correct instance variables" do | ||
get :user_links, params: { username: user.username } | ||
expect(assigns(:user)).to eq(user) | ||
expect(assigns(:links)).to be_an(ActiveRecord::Relation) | ||
expect(assigns(:pinned_links)).to be_an(ActiveRecord::Relation) | ||
expect(assigns(:achievements)).to be_an(ActiveRecord::Relation) | ||
end | ||
end | ||
|
||
describe "GET #track_click" do | ||
it "creates a LinkClick" do | ||
expect { | ||
get :track_click, params: { id: link.to_param } | ||
}.to change(LinkClick, :count).by(1) | ||
end | ||
|
||
it "redirects to the link url" do | ||
get :track_click, params: { id: link.to_param } | ||
expect(response).to redirect_to(link.url) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# spec/controllers/pages_controller_spec.rb | ||
require 'rails_helper' | ||
|
||
RSpec.describe PagesController, type: :controller do | ||
describe "GET #home" do | ||
it "returns a success response" do | ||
get :home | ||
expect(response).to be_successful | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# spec/controllers/users/registrations_controller_spec.rb | ||
require 'rails_helper' | ||
|
||
RSpec.describe Users::RegistrationsController, type: :controller do | ||
before do | ||
@request.env["devise.mapping"] = Devise.mappings[:user] | ||
end | ||
|
||
describe "POST #create" do | ||
let(:valid_attributes) { | ||
{ email: "[email protected]", password: "password", password_confirmation: "password", | ||
username: "testuser", full_name: "Test User", tags: "tag1,tag2" } | ||
} | ||
|
||
it "creates a new User" do | ||
expect { | ||
post :create, params: { user: valid_attributes } | ||
}.to change(User, :count).by(1) | ||
end | ||
|
||
it "correctly processes tags" do | ||
post :create, params: { user: valid_attributes } | ||
user = User.last | ||
tags = user.tags.is_a?(String) ? JSON.parse(user.tags) : user.tags | ||
expect(tags).to eq(["tag1", "tag2"]) | ||
end | ||
end | ||
|
||
describe "PUT #update" do | ||
let(:user) { create(:user, tags: ["old_tag1", "old_tag2"].to_json) } | ||
|
||
before do | ||
sign_in user | ||
end | ||
|
||
context "with valid params" do | ||
let(:new_attributes) { | ||
{ full_name: "New Name", tags: "new_tag1,new_tag2" } | ||
} | ||
|
||
it "updates the requested user" do | ||
put :update, params: { user: new_attributes } | ||
user.reload | ||
expect(user.full_name).to eq("New Name") | ||
tags = user.tags.is_a?(String) ? JSON.parse(user.tags) : user.tags | ||
expect(tags).to eq(["new_tag1", "new_tag2"]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# spec/factories/achievements.rb | ||
FactoryBot.define do | ||
factory :achievement do | ||
title { "My Achievement" } | ||
date { Date.today } | ||
description { "This is a test achievement" } | ||
icon { "fa-trophy" } | ||
url { nil } | ||
association :user | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# spec/factories/daily_metrics.rb | ||
FactoryBot.define do | ||
factory :daily_metric do | ||
user | ||
date { Date.today } | ||
page_views { 10 } | ||
link_clicks { 5 } | ||
achievement_views { 3 } | ||
unique_visitors { 2 } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# spec/factories/links.rb | ||
FactoryBot.define do | ||
factory :link do | ||
title { "MyString" } | ||
url { "http://example.com" } | ||
visible { true } | ||
pinned { false } | ||
association :user | ||
end | ||
end |
Oops, something went wrong.