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

Update specs #32

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion app/controllers/spina/admin/blog/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PostsController < AdminController
before_action :set_breadcrumb
before_action :set_tabs, only: %i[new create edit update]
before_action :set_locale

admin_section :blog

decorates_assigned :post
Expand Down
38 changes: 25 additions & 13 deletions spec/controllers/spina/admin/blog/posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
require 'rails_helper'

RSpec.describe Spina::Admin::Blog::PostsController, type: :controller do
let(:posts) { create_list(:spina_blog_post, 3) }
let(:blog_post) { create(:spina_blog_post) }
let(:user) { create(:spina_user) }
let(:category) { create(:spina_blog_category) }
let(:posts) { create_list(:spina_blog_post, 3) }
let(:blog_post) { create(:spina_blog_post) }
let(:draft_posts) { create_list(:spina_blog_post, 3, draft: true) }
let(:live_posts) { create_list(:spina_blog_post, 3, draft: false) }
let(:post_attributes) { attributes_for(:spina_blog_post) }
let(:live_posts) { create_list(:spina_blog_post, 3, draft: false) }
let(:post_attributes) do
attributes_for(:spina_blog_post).merge(
user_id: user.id,
category_id: category.id
)
end
let(:invalid_post_attributes) do
attributes_for(:invalid_spina_blog_post)
end

routes { Spina::Engine.routes }

Expand Down Expand Up @@ -85,17 +95,19 @@
end

describe 'POST #create' do
subject { post :create, params: { post: post_attributes } }

it { is_expected.to have_http_status :redirect }
it {
subject
expect(flash[:notice]).to eq 'Post saved'
}
it { expect { subject }.to change(Spina::Blog::Post, :count).by(1) }
context 'with valid attributes' do
subject { post :create, params: { post: post_attributes } }

it { is_expected.to have_http_status :redirect }
it {
subject
expect(flash[:notice]).to eq 'Post saved'
}
it { expect { subject }.to change(Spina::Blog::Post, :count).by(1) }
end

context 'with invalid attributes' do
subject { post :create, params: { post: { content: 'foo' } } }
subject { post :create, params: { post: invalid_post_attributes } }

it { is_expected.to_not have_http_status :redirect }
it { expect { subject }.to_not change(Spina::Blog::Post, :count) }
Expand Down
4 changes: 3 additions & 1 deletion spec/controllers/spina/blog/posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
context 'with a year' do
let(:this_year_posts) do
create_list :spina_blog_post, 3,
draft: false, published_at: Time.zone.today.beginning_of_year
draft: false,
published_at: Time.zone.today.beginning_of_year

end
let(:last_year_posts) do
create_list :spina_blog_post, 3,
Expand Down
6 changes: 5 additions & 1 deletion spec/factories/spina/blog/posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
sequence(:title) { |n| "Blog Post #{n}" }
content { 'Some content for my post' }
association :category, factory: :spina_blog_category
association :user, factory: :spina_user

seo_title { 'Some title for SEO' }
description { 'Some description for SEO' }

factory :invalid_spina_blog_post do
title { nil }
title { nil }
content { nil }
user_id { nil }
category_id { nil }
end
end
end
11 changes: 11 additions & 0 deletions spec/factories/spina/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

FactoryBot.define do
factory :spina_user, class: Spina::User do
name { 'Bram' }
sequence(:email) { |n| "bram#{n}@denkgroot.com" }
password { 'password' }
password_confirmation { password }
admin { true }
end
end
22 changes: 12 additions & 10 deletions spec/models/spina/blog/category_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

require 'rails_helper'

module Spina::Blog
RSpec.describe Category, type: :model do
let(:category) { build(:spina_blog_category) }
module Spina
module Blog
RSpec.describe Category, type: :model do
let(:category) { build(:spina_blog_category) }

subject { category }
subject { category }

it { is_expected.to be_valid }
it { expect { category.save }.to change(Spina::Blog::Category, :count).by(1) }
it { is_expected.to be_valid }
it { expect { category.save }.to change(Spina::Blog::Category, :count).by(1) }

context 'with invalid attributes' do
let(:category) { build(:invalid_spina_blog_category) }
context 'with invalid attributes' do
let(:category) { build(:invalid_spina_blog_category) }

it { is_expected.to_not be_valid }
it { expect { category.save }.to_not change(Spina::Blog::Category, :count) }
it { is_expected.to_not be_valid }
it { expect { category.save }.to_not change(Spina::Blog::Category, :count) }
end
end
end
end
46 changes: 24 additions & 22 deletions spec/models/spina/blog/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@

require 'rails_helper'

module Spina::Blog
RSpec.describe Post, type: :model do
let(:post) { build(:spina_blog_post) }
module Spina
module Blog
RSpec.describe Post, type: :model do
let(:post) { build(:spina_blog_post) }

subject { post }
subject { post }

it { is_expected.to be_valid }
it { expect { post.save }.to change(Spina::Blog::Post, :count).by(1) }
it { is_expected.to be_valid }
it { expect { post.save }.to change(Spina::Blog::Post, :count).by(1) }

context 'with invalid attributes' do
let(:post) { build(:invalid_spina_blog_post) }
context 'with invalid attributes' do
let(:post) { build(:invalid_spina_blog_post) }

it { is_expected.to_not be_valid }
it { expect { post.save }.to_not change(Spina::Blog::Post, :count) }
end
it { is_expected.to_not be_valid }
it { expect { post.save }.to_not change(Spina::Blog::Post, :count) }
end

describe '.featured' do
let!(:post) { create(:spina_blog_post, featured: true) }
let!(:unfeatured) { create(:spina_blog_post) }
describe '.featured' do
let!(:post) { create(:spina_blog_post, featured: true) }
let!(:unfeatured) { create(:spina_blog_post) }

it 'returns 1 result' do
expect(Spina::Blog::Post.featured).to match_array [post]
it 'returns 1 result' do
expect(Spina::Blog::Post.featured).to match_array [post]
end
end
end

describe '.unfeatured' do
let!(:post) { create(:spina_blog_post, featured: true) }
let!(:unfeatured) { create(:spina_blog_post) }
describe '.unfeatured' do
let!(:post) { create(:spina_blog_post, featured: true) }
let!(:unfeatured) { create(:spina_blog_post) }

it 'returns 1 result' do
expect(Spina::Blog::Post.unfeatured).to match_array [unfeatured]
it 'returns 1 result' do
expect(Spina::Blog::Post.unfeatured).to match_array [unfeatured]
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/support/controller_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

module ControllerHelpers
def sign_in
@account = Spina::Account.create name: 'My Website', preferences: {theme: 'default'}
@account = Spina::Account.create name: 'My Website', preferences: { theme: 'default' }
@user = Spina::User.create name: 'admin', email: '[email protected]', password: 'password', admin: true
request.session[:spina_user_id] = @user.id
end
end

module FeatureHelpers
def sign_in
@account = Spina::Account.create name: 'My Website', preferences: {theme: 'default'}
@account = Spina::Account.create name: 'My Website', preferences: { theme: 'default' }
@user = Spina::User.create name: 'admin', email: '[email protected]', password: 'password', admin: true
visit '/admin/login'
fill_in :email, with: @user.email
fill_in :password, with: 'password'
click_button 'Login'
expect(page).to have_content("Pages")
expect(page).to have_content('Pages')
end
end

Expand Down
9 changes: 8 additions & 1 deletion spec/system/spina/admin/blog/posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@

it 'shows all the posts' do
visit '/admin/blog/posts'
expect(page).to have_content "Blog Post"
expect(page).to have_content 'Blog Post'
end
end

describe 'creating a post' do
let!(:category) { create(:spina_blog_category) }

it 'creates a post', js: true do
visit '/admin/blog/posts'
find(:css, 'a[href="/admin/blog/posts/new"]').click
fill_in 'Post title', with: 'Title of Blog post'
find(
:css, 'trix-editor[input*="content_input"]'
).set('Foobar')
within 'div[data-controller="tabs"]' do
click_on 'Settings'
end
select category.name, from: 'post[category_id]'
select @user.name, from: 'post[user_id]'
click_on 'Save post'
within 'nav[data-controller="navigation"]' do
click_on 'Posts'
Expand Down