action_page_spec.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. require "rails_helper"
  2. describe ActionPage do
  3. let(:attr) { FactoryGirl.attributes_for :action_page }
  4. it "creates a new instance given a valid attribute" do
  5. expect {
  6. ActionPage.create!(attr)
  7. }.to change { ActionPage.count }.by(1)
  8. end
  9. it "knows when to redirect from an archived action" do
  10. action_page = FactoryGirl.build_stubbed :archived_action_page
  11. expect(action_page.redirect_from_archived_to_active_action?).to be_truthy
  12. end
  13. # The test was a no-go because of the ajaxy html requiring nils... and Then
  14. # changing them from nils to ""??? Needs effeciency review before crashyness review
  15. # it "should not allow the creation of a model with so few attrs that it would crash the views" do
  16. # expect {
  17. # ActionPage.create!({ })
  18. # }.to raise_exception(ActiveRecord::RecordInvalid)
  19. #
  20. # expect(ActionPage.count).to eq 0
  21. # end
  22. describe "slug" do
  23. let(:page) { FactoryGirl.create(:action_page) }
  24. let(:new_slug) { "a-better-slug" }
  25. it "has a friendly slug" do
  26. expect(page.slug).to eq(page.title.downcase.gsub(" ", "-"))
  27. end
  28. it "updates the slug when title changes" do
  29. expect { page.update(title: "something else") }
  30. .to change { page.slug }
  31. end
  32. it "does not update slug when unrelated attr changes" do
  33. expect { page.update(summary: "something else") }
  34. .not_to change { page.slug }
  35. end
  36. context "when a dev has added a custom slug" do
  37. before { page.update(slug: new_slug) }
  38. it "remembers the new slug" do
  39. expect(page.slug).to eq(new_slug)
  40. end
  41. it "updates the slug when the title changes" do
  42. expect { page.update(title: "something else") }
  43. .to change { page.slug }
  44. end
  45. it "does not update slug when unrelated attr changes" do
  46. expect { page.update(summary: "something else") }
  47. .not_to change { page.slug }
  48. end
  49. end
  50. context "with historical slugs" do
  51. # This is here as documentation of FriendlyId's behavior,
  52. # because it surprised me a little, and it might surprise you.
  53. # FriendlyId's `history` module remembers all of a model's slugs
  54. # and prevents new models from duplicating them.
  55. # This is cool for supporting old links, but the way FriendlyId handles
  56. # duplication is to append a UUID onto the end of the title,
  57. # eg: "my-awesome-action-page-de6d58bf-6033-4876-9f79-ee3f9d9ff043"
  58. # which, in my opinion, is not very friendly.
  59. let(:old_slug) { page.slugs.first }
  60. before { page.update(slug: new_slug) }
  61. it "does not allow slug conflicts" do
  62. expect(FactoryGirl.create(:action_page, slug: old_slug).slug)
  63. .not_to eq(old_slug)
  64. end
  65. end
  66. end
  67. end