~edwardloveall/scribe

ref: f05a12a880cbb1399925d9dc12317f6f5ce8a285 scribe/spec/classes/page_converter_spec.cr -rw-r--r-- 2.7 KiB
f05a12a8Edward Loveall Add support for missing posts a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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