scribe/spec/classes/page_converter_spec.cr
Edward Loveall f05a12a880
Add support for missing posts
Posts, like 8661f4724aa9, can go missing if the account or post was
removed. In this case, the API returns data like this:

```json
{
  "data": {
    "post": null
  }
}
```

When this happens, we can detect it because the parsed response now has
a nil value: `response.data.post == nil` and construct an `EmptyPage`
instead of a `Page`. The `Articles::Show` action can then render
conditionally based on if the response from `PageConverter` is a `Page`
or an `EmptyPage`.
2022-06-17 16:00:01 -04:00

133 lines
2.7 KiB
Crystal

require "../spec_helper"
include Nodes
describe PageConverter do
it "sets the page title" do
title = "Hello, world!"
paragraph_json = <<-JSON
[
{
"text": "#{title}",
"type": "H3",
"markups": [],
"iframe": null,
"layout": null,
"metadata": null
}
]
JSON
data_json = default_post_json(title, paragraph_json)
data = PostResponse::Post.from_json(data_json)
page = PageConverter.new.convert(data)
page.title.should eq title
end
it "sets the author" do
post_json = <<-JSON
{
"title": "This is a story",
"createdAt": 0,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": []
}
}
}
JSON
post = PostResponse::Post.from_json(post_json)
page = PageConverter.new.convert(post)
page.author.name.should eq "Author"
page.author.id.should eq "abc123"
end
it "sets the publish date/time" do
post_json = <<-JSON
{
"title": "This is a story",
"createdAt": 1000,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": []
}
}
}
JSON
post = PostResponse::Post.from_json(post_json)
page = PageConverter.new.convert(post)
page.created_at.should eq Time.utc(1970, 1, 1, 0, 0, 1)
end
it "calls converts the remaining paragraph content" do
title = "Title"
paragraph_json = <<-JSON
[
{
"text": "#{title}",
"type": "H3",
"markups": [],
"iframe": null,
"layout": null,
"metadata": null
},
{
"text": "Content",
"type": "P",
"markups": [],
"iframe": null,
"layout": null,
"metadata": null
}
]
JSON
post_json = default_post_json(title, paragraph_json)
data = PostResponse::Post.from_json(post_json)
page = PageConverter.new.convert(data)
page.nodes.should eq [
Paragraph.new([
Text.new("Content"),
] of Child),
]
end
end
def default_paragraph_json
"[]"
end
def default_post_json(
title : String = "This is a story",
paragraph_json : String = default_paragraph_json
)
<<-JSON
{
"title": "#{title}",
"createdAt": 1628974309758,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": #{paragraph_json}
}
}
}
JSON
end