~edwardloveall/scribe

ref: 1dcded9153c482553538fdf9a4f80e7d9361b19c scribe/spec/classes/page_converter_spec.cr -rw-r--r-- 2.8 KiB
1dcded91Edward Loveall Update changelog to mention no DATABASE_URL 2 months 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
134
135
136
137
138
139
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_data_json(title, paragraph_json)
    data = PostResponse::Data.from_json(data_json)

    page = PageConverter.new.convert(data)

    page.title.should eq title
  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 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
    data_json = default_data_json(title, 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_paragraph_json
  "[]"
end

def default_data_json(
  title : String = "This is a story",
  paragraph_json : String = default_paragraph_json
)
  <<-JSON
    {
      "post": {
        "title": "#{title}",
        "createdAt": 1628974309758,
        "creator": {
          "id": "abc123",
          "name": "Author"
        },
        "content": {
          "bodyModel": {
            "paragraphs": #{paragraph_json}
          }
        }
      }
    }
  JSON
end