|
33 | 33 | require "test_helper" |
34 | 34 |
|
35 | 35 | class PageTest < ActiveSupport::TestCase |
36 | | - # test "the truth" do |
37 | | - # assert true |
38 | | - # end |
| 36 | + include ActiveJob::TestHelper |
| 37 | + |
| 38 | + let(:convention) { create(:convention) } |
| 39 | + |
| 40 | + describe "#compute_og_description" do |
| 41 | + it "strips HTML tags and truncates to 160 characters" do |
| 42 | + page = create(:page, parent: convention, content: "<p>#{"a" * 200}</p>") |
| 43 | + result = page.compute_og_description |
| 44 | + assert_equal 160, result.length |
| 45 | + assert_no_match(%r{</?p>}, result) |
| 46 | + end |
| 47 | + |
| 48 | + it "returns the plain text content for short pages" do |
| 49 | + page = create(:page, parent: convention, content: "Hello world") |
| 50 | + assert_equal "Hello world", page.compute_og_description |
| 51 | + end |
| 52 | + |
| 53 | + it "returns an empty string if rendering raises" do |
| 54 | + page = create(:page, parent: convention, content: "Hello world") |
| 55 | + CmsContentFinder.stub(:new, ->(*) { raise "boom" }) { assert_equal "", page.compute_og_description } |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + describe "after save" do |
| 60 | + it "enqueues CachePageOgDescriptionJob on create" do |
| 61 | + assert_enqueued_with(job: CachePageOgDescriptionJob) { create(:page, parent: convention) } |
| 62 | + end |
| 63 | + |
| 64 | + it "enqueues CachePageOgDescriptionJob on update" do |
| 65 | + page = create(:page, parent: convention) |
| 66 | + assert_enqueued_with(job: CachePageOgDescriptionJob) { page.update!(name: "Updated") } |
| 67 | + end |
| 68 | + end |
39 | 69 | end |
0 commit comments