-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmarkdown_test.go
More file actions
78 lines (67 loc) · 2.91 KB
/
markdown_test.go
File metadata and controls
78 lines (67 loc) · 2.91 KB
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
package goeditorjs_test
import (
"errors"
"testing"
"github.com/davidscottmills/goeditorjs"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type mockMarkdownBlockHandler struct {
mock.Mock
typeName string
}
func (m *mockMarkdownBlockHandler) GenerateMarkdown(editorJSBlock goeditorjs.EditorJSBlock) (string, error) {
args := m.Called(editorJSBlock)
return args.String(0), args.Error(1)
}
func (m *mockMarkdownBlockHandler) Type() string {
return m.typeName
}
func Test_Markdown(t *testing.T) {
eng := goeditorjs.NewMarkdownEngine()
require.NotNil(t, eng)
require.NotNil(t, eng.BlockHandlers)
}
func Test_MarkdownEngine_RegisterBlockHandler(t *testing.T) {
bh1 := &mockMarkdownBlockHandler{typeName: "header"}
bh2 := &mockMarkdownBlockHandler{typeName: "list"}
eng := &goeditorjs.MarkdownEngine{BlockHandlers: make(map[string]goeditorjs.MarkdownBlockHandler)}
eng.RegisterBlockHandlers(bh1, bh2)
require.Equal(t, eng.BlockHandlers["header"], bh1)
require.Equal(t, eng.BlockHandlers["list"], bh2)
}
func Test_GenerateMarkdown_Returns_Parse_Err(t *testing.T) {
eng := &goeditorjs.MarkdownEngine{BlockHandlers: make(map[string]goeditorjs.MarkdownBlockHandler)}
_, err := eng.GenerateMarkdown(``)
require.Error(t, err)
}
func Test_GenerateMarkdown_NoHandler_Should_Err(t *testing.T) {
editorJSData := `{"time": 1607709186831,"blocks": [{"type": "header","data": {"text": "Heading 1","level": 1}}],"version": "2.19.1"}`
eng := &goeditorjs.MarkdownEngine{BlockHandlers: make(map[string]goeditorjs.MarkdownBlockHandler)}
_, err := eng.GenerateMarkdown(editorJSData)
require.Error(t, err)
require.True(t, errors.Is(err, goeditorjs.ErrBlockHandlerNotFound))
}
func Test_GenerateMarkdown_Returns_Err_From_Handler(t *testing.T) {
bh := &mockMarkdownBlockHandler{}
mockErr := errors.New("Mock Error")
bh.On("GenerateMarkdown", mock.Anything).Return("", mockErr)
editorJSData := `{"time": 1607709186831,"blocks": [{"type": "header","data": {"text": "Heading 1","level": 1}}],"version": "2.19.1"}`
eng := &goeditorjs.MarkdownEngine{BlockHandlers: make(map[string]goeditorjs.MarkdownBlockHandler)}
eng.BlockHandlers["header"] = bh
_, err := eng.GenerateMarkdown(editorJSData)
require.Error(t, err)
require.Equal(t, mockErr, err)
bh.AssertCalled(t, "GenerateMarkdown", mock.Anything)
}
func Test_GenerateMarkdown_Result_Includes_Handler_Result(t *testing.T) {
bh := &mockMarkdownBlockHandler{}
handlerResult := "# Hello World"
bh.On("GenerateMarkdown", mock.Anything).Return(handlerResult, nil)
editorJSData := `{"time": 1607709186831,"blocks": [{"type": "header","data": {"text": "Heading 1","level": 1}}],"version": "2.19.1"}`
eng := &goeditorjs.MarkdownEngine{BlockHandlers: make(map[string]goeditorjs.MarkdownBlockHandler)}
eng.BlockHandlers["header"] = bh
result, _ := eng.GenerateMarkdown(editorJSData)
require.Contains(t, result, handlerResult)
bh.AssertCalled(t, "GenerateMarkdown", mock.Anything)
}