scribe/spec/classes/page_converter_spec.cr
Edward Loveall 561483cf9f
Link to the author's page
Right now this links to the user's medium page. It may link to an
internal page in the future.

Instead of the Page taking the author as a string, it now takes a
PostResponse::Creator object. The Articles::ShowPage then converts the
Creator (a name and user_id) to an author link.

Finally, I did some refactoring of UserAnchor (which I thought I was
going to use for this) to change it's userId attribute to user_id as is
Crystal convention.
2021-09-15 16:03:36 -04:00

169 lines
3.6 KiB
Crystal

require "../spec_helper"
include Nodes
describe PageConverter do
it "sets the title and subtitle if present" do
paragraph_json = <<-JSON
[
{
"text": "Title",
"type": "H3",
"markups": [],
"iframe": null,
"layout": null,
"metadata": null
},
{
"text": "Subtitle",
"type": "H4",
"markups": [],
"iframe": null,
"layout": null,
"metadata": null
}
]
JSON
data_json = default_data_json(paragraph_json)
data = PostResponse::Data.from_json(data_json)
page = PageConverter.new.convert(data)
page.title.should eq "Title"
page.subtitle.should eq "Subtitle"
end
it "sets the title to the first paragraph if no title" do
paragraph_json = <<-JSON
[
{
"text": "Not a title",
"type": "P",
"markups": [],
"iframe": null,
"layout": null,
"metadata": null
}
]
JSON
data_json = default_data_json(paragraph_json)
data = PostResponse::Data.from_json(data_json)
page = PageConverter.new.convert(data)
page.title.should eq "Not a title"
page.subtitle.should eq nil
end
it "sets the author" do
data_json = <<-JSON
{
"post": {
"title": "This is a story",
"createdAt": 0,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": []
}
}
}
}
JSON
data = PostResponse::Data.from_json(data_json)
page = PageConverter.new.convert(data)
page.author.name.should eq "Author"
page.author.id.should eq "abc123"
end
it "sets the publish date/time" do
data_json = <<-JSON
{
"post": {
"title": "This is a story",
"createdAt": 1000,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": []
}
}
}
}
JSON
data = PostResponse::Data.from_json(data_json)
page = PageConverter.new.convert(data)
page.created_at.should eq Time.utc(1970, 1, 1, 0, 0, 1)
end
it "calls ParagraphConverter to convert the remaining paragraph content" do
paragraph_json = <<-JSON
[
{
"text": "Title",
"type": "H3",
"markups": [],
"iframe": null,
"layout": null,
"metadata": null
},
{
"text": "Subtitle",
"type": "H4",
"markups": [],
"iframe": null,
"layout": null,
"metadata": null
},
{
"text": "Content",
"type": "P",
"markups": [],
"iframe": null,
"layout": null,
"metadata": null
}
]
JSON
data_json = default_data_json(paragraph_json)
data = PostResponse::Data.from_json(data_json)
page = PageConverter.new.convert(data)
page.nodes.should eq [
Paragraph.new([
Text.new("Content"),
] of Child),
]
end
end
def default_data_json(paragraph_json : String)
<<-JSON
{
"post": {
"title": "This is a story",
"createdAt": 1628974309758,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": #{paragraph_json}
}
}
}
}
JSON
end