spec/models/homesteading/post_spec.rb in homesteading_publisher-0.1.2 vs spec/models/homesteading/post_spec.rb in homesteading_publisher-0.1.3

- old
+ new

@@ -1,10 +1,221 @@ require "rails_helper" describe Post do - describe '.validates' do - let(:post) { create(:post) } - it 'is valid' do - expect(post.valid?).to eq true + describe "validations" do + subject { create(:post) } + + it "has a valid fabricator" do + expect(subject).to be_valid + end + + it "validates presence of year" do + subject.year = "" + expect(subject).not_to be_valid + end + + it "validates presence of month" do + subject.month = "" + expect(subject).not_to be_valid + end + + it "validates presence of day" do + subject.day = "" + expect(subject).not_to be_valid + end + + it "validates presence of hour" do + subject.hour = "" + expect(subject).not_to be_valid + end + + it "validates presence of minute" do + subject.minute = "" + expect(subject).not_to be_valid + end + + it "validates presence of second" do + subject.second = "" + expect(subject).not_to be_valid + end + + it "validates presence of slug" do + subject.slug = "" + expect(subject).not_to be_valid + end + end + + describe "default_scope sort order" do + pending + # newest posts first + # default_scope { order("published_at desc") } + end + + describe "#path" do + subject { create(:post) } + before { create(:setting, name: "Post Type", content: "fake-post") } + + it "creates a path for post permalink URL" do + subject.year = 1979 + subject.month = 9 + subject.day = 18 + expect(subject.path).to eq "/fake-posts/1979/09/18/test-slug" + end + + context "when #published_at is nil" do + let(:path) { create(:post, published_at: nil).path } + + it "it sets it to now" do + Time.stub(:now).and_return(Time.parse("Jan 31, 1986")) + expect(path).to eq "/fake-posts/1986/01/31/test-slug" + end + end + + context "when #published_at is not nil" do + let(:post) { create(:post, published_at: 5.minutes.ago) } + subject { post.path } + + it "is '/fake-posts/:year/:month/:day/:slug'" do + year = post.published_at.year + month = post.published_at.month.to_s.rjust(2, '0') + day = post.published_at.day.to_s.rjust(2, '0') + slug = post.slug + expect(subject).to eq "/fake-posts/#{year}/#{month}/#{day}/#{slug}" + end + end + + end + + describe "#params" do + subject { create(:post).params } + + it "returns a hash of date and slug values" do + subject[:year] = 1979 + subject[:month] = 9 + subject[:day] = 18 + + params_hash = { slug: "test-slug", year: 1979, month: 9, day: 18 } + expect(subject).to eq params_hash + end + end + + describe "#public?" do + subject { create(:post) } + + context "when #private is true" do + subject { build(:post, private: true).public? } + + it "is private" do + expect(subject).to eq false + end + end + + context "when #private is nil" do + subject { build(:post, private: nil).public? } + + it "is public" do + expect(subject).to eq true + end + end + + context "when #private is false" do + subject { build(:post, private: false).public? } + + it "is public" do + expect(subject).to eq true + end + end + + end + + describe "#name" do + subject { create(:post) } + + context "when #title is populated" do + subject { build(:post, title: "Test Title").name } + + it "is a title" do + expect(subject).to eq "Test Title" + end + end + + context "when #title and #subtitle are populated" do + subject { build(:post, title: "Test Title", subtitle: "Test SUBTitle").name } + + it "is a title + subtitle" do + expect(subject).to eq "Test Title : Test SUBTitle" + end + end + + context "when neither #title nor #subtitle is populated" do + subject { build(:post, title: nil, subtitle: nil).name } + + it "is blank" do + expect(subject).to eq "" + end + end + end + + describe "#set_published_at_attrs" do + subject { build(:post, published_at: Time.parse("18 Sep 1979 14:30:00 UTC +00:00")) } + + it "year is 1979" do + expect(subject.year).to eq "1979" + end + + it "month is September" do + expect(subject.month).to eq "09" + end + + it "day is the 18th" do + expect(subject.day).to eq "18" + end + + it "hour is 2PM" do + expect(subject.hour).to eq "14" + end + + it "minute is halp passed" do + expect(subject.minute).to eq "30" + end + + it "second is zero" do + expect(subject.second).to eq "00" + end + end + + describe "#set_slug" do + subject { create(:post) } + + context "when #slug is populated" do + subject { build(:post, slug: "turtles-all-the-way-down", title: "Test Post Title").slug } + + it "is slugged" do + expect(subject).to eq "turtles-all-the-way-down" + end + end + + context "when only #title is populated" do + subject { build(:post, slug: nil, title: "Test Post Title").slug } + + it "is slugged" do + expect(subject).to eq "test-post-title" + end + end + + context "when only #content is populated" do + subject { build(:post, slug: nil, content: "Lorem Impsum").slug } + + it "is slugged" do + expect(subject).to eq "lorem-impsum" + end + end + + context "when both #title and #content are populated" do + subject { build(:post, slug: nil, title: "Test Post Title", content: "Lorem Impsum").slug } + + it "is slugged" do + expect(subject).to eq "test-post-title" + end end end end