diff --git a/app/controllers/spina/admin/blog/posts_controller.rb b/app/controllers/spina/admin/blog/posts_controller.rb index bc212f0..c1022d9 100644 --- a/app/controllers/spina/admin/blog/posts_controller.rb +++ b/app/controllers/spina/admin/blog/posts_controller.rb @@ -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 diff --git a/spec/controllers/spina/admin/blog/posts_controller_spec.rb b/spec/controllers/spina/admin/blog/posts_controller_spec.rb index 92b9a71..c5cc71b 100644 --- a/spec/controllers/spina/admin/blog/posts_controller_spec.rb +++ b/spec/controllers/spina/admin/blog/posts_controller_spec.rb @@ -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 } @@ -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) } diff --git a/spec/controllers/spina/blog/posts_controller_spec.rb b/spec/controllers/spina/blog/posts_controller_spec.rb index d51187e..947764e 100644 --- a/spec/controllers/spina/blog/posts_controller_spec.rb +++ b/spec/controllers/spina/blog/posts_controller_spec.rb @@ -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, diff --git a/spec/factories/spina/blog/posts.rb b/spec/factories/spina/blog/posts.rb index e942a4a..a94fc6b 100644 --- a/spec/factories/spina/blog/posts.rb +++ b/spec/factories/spina/blog/posts.rb @@ -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 diff --git a/spec/factories/spina/users.rb b/spec/factories/spina/users.rb new file mode 100644 index 0000000..9379aca --- /dev/null +++ b/spec/factories/spina/users.rb @@ -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 diff --git a/spec/models/spina/blog/category_spec.rb b/spec/models/spina/blog/category_spec.rb index be1c33d..b4f4ed2 100644 --- a/spec/models/spina/blog/category_spec.rb +++ b/spec/models/spina/blog/category_spec.rb @@ -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 diff --git a/spec/models/spina/blog/post_spec.rb b/spec/models/spina/blog/post_spec.rb index 7d8f6ed..51d8b2b 100644 --- a/spec/models/spina/blog/post_spec.rb +++ b/spec/models/spina/blog/post_spec.rb @@ -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 diff --git a/spec/support/controller_helpers.rb b/spec/support/controller_helpers.rb index 6a7a19b..bbb3db6 100644 --- a/spec/support/controller_helpers.rb +++ b/spec/support/controller_helpers.rb @@ -2,7 +2,7 @@ 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: 'admin@example.com', password: 'password', admin: true request.session[:spina_user_id] = @user.id end @@ -10,13 +10,13 @@ def sign_in 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: 'admin@example.com', 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 diff --git a/spec/system/spina/admin/blog/posts_spec.rb b/spec/system/spina/admin/blog/posts_spec.rb index c32b625..217648f 100644 --- a/spec/system/spina/admin/blog/posts_spec.rb +++ b/spec/system/spina/admin/blog/posts_spec.rb @@ -10,11 +10,13 @@ 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 @@ -22,6 +24,11 @@ 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'