Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 2e16b16

Browse files
committed
refactor(editor): spec-types && add more tests
1 parent c6ac24c commit 2e16b16

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

lib/helper/converter/editor_to_html/frags/table.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ defmodule Helper.Converter.EditorToHTML.Frags.Table do
55
see https://editorjs.io/
66
"""
77
alias Helper.Converter.EditorToHTML.Class
8-
# alias Helper.Types, as: T
8+
alias Helper.Types, as: T
99

1010
@class get_in(Class.article(), ["table"])
1111

12+
@spec get_row([T.editor_table_cell()]) :: T.html()
1213
def get_row(group_items) do
1314
tr_content =
1415
Enum.reduce(group_items, "", fn item, acc ->
@@ -19,6 +20,7 @@ defmodule Helper.Converter.EditorToHTML.Frags.Table do
1920
~s(<tr>#{tr_content}</tr>)
2021
end
2122

23+
@spec frag(:td, T.editor_table_cell()) :: T.html()
2224
def frag(:td, item) do
2325
%{
2426
"align" => align,
@@ -43,6 +45,7 @@ defmodule Helper.Converter.EditorToHTML.Frags.Table do
4345
end
4446
end
4547

48+
@spec frag(:th, T.editor_table_cell()) :: T.html()
4649
def frag(:th, item) do
4750
%{"align" => align, "text" => text} = item
4851

lib/helper/types.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ defmodule Helper.Types do
5959
required(:text) => String.t(),
6060
prefixIndex: String.t()
6161
}
62+
63+
@typedoc """
64+
editor.js's Table align type
65+
"""
66+
@type editor_table_align :: :center | :left | :right
67+
68+
@typedoc """
69+
editor.js's Table td type
70+
"""
71+
@type editor_table_cell :: %{
72+
required(:text) => String.t(),
73+
required(:align) => editor_table_align,
74+
isStripe: Boolean.t(),
75+
isHeader: Boolean.t()
76+
}
77+
6278
@typedoc """
6379
html fragment
6480
"""

test/helper/converter/editor_to_html_test/table_test.exs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Table do
88
alias Helper.Utils
99

1010
@root_class Class.article()
11-
@class get_in(@root_class, ["list"])
11+
@class get_in(@root_class, ["table"])
1212

1313
describe "[table block unit]" do
1414
defp set_items(column_count, items) do
@@ -97,25 +97,28 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Table do
9797
{:ok, editor_string} = Jason.encode(editor_json)
9898
{:ok, converted} = Parser.to_html(editor_string)
9999

100-
IO.inspect(converted, label: "table >>")
100+
th_header_class = @class["th_header"]
101+
td_stripe_class = @class["td_stripe"]
101102

102-
# unorder_list_prefix_class = @class["unorder_list_prefix"]
103-
# assert Utils.str_occurence(converted, unorder_list_prefix_class) == 3
103+
assert Utils.str_occurence(converted, th_header_class) == 4
104+
assert Utils.str_occurence(converted, td_stripe_class) == 3
104105
end
105106

106-
# @tag :wip
107-
# test "invalid list mode parse should raise error message" do
108-
# editor_json = set_items("invalid-mode", [])
109-
# {:ok, editor_string} = Jason.encode(editor_json)
110-
# {:error, err_msg} = Parser.to_html(editor_string)
107+
@tag :wip2
108+
test "invalid table field parse should raise error message" do
109+
editor_json = set_items("aa", "bb")
110+
{:ok, editor_string} = Jason.encode(editor_json)
111+
{:error, err_msg} = Parser.to_html(editor_string)
111112

112-
# assert err_msg == [
113-
# %{
114-
# block: "list",
115-
# field: "mode",
116-
# message: "should be: checklist | order_list | unorder_list"
117-
# }
118-
# ]
119-
# end
113+
assert err_msg = [
114+
%{
115+
block: "table",
116+
field: "columnCount",
117+
message: "should be: number",
118+
value: "aa"
119+
},
120+
%{block: "table", field: "items", message: "should be: list", value: "bb"}
121+
]
122+
end
120123
end
121124
end

0 commit comments

Comments
 (0)