Unit test for editor live preview

This commit is contained in:
Jan-Lukas Else 2021-11-10 18:17:25 +01:00
parent 581822dd88
commit f90a9a1f28
4 changed files with 71 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import (
"net/url"
"strings"
"github.com/gorilla/websocket"
"github.com/microcosm-cc/bluemonday"
"go.goblog.app/app/pkgs/contenttype"
"gopkg.in/yaml.v3"
@ -39,13 +40,16 @@ func (a *goBlog) serveEditorPreview(w http.ResponseWriter, r *http.Request) {
if err != nil {
break
}
if mt != websocket.TextMessage {
continue
}
// Create preview
preview, err := a.createMarkdownPreview(blog, message)
if err != nil {
preview = []byte(err.Error())
}
// Write preview to socket
err = c.WriteMessage(mt, preview)
err = c.WriteMessage(websocket.TextMessage, preview)
if err != nil {
break
}

62
editor_test.go Normal file
View File

@ -0,0 +1,62 @@
package main
import (
"context"
"net/http"
"path/filepath"
"testing"
"github.com/gorilla/websocket"
"github.com/posener/wstest"
"github.com/stretchr/testify/require"
)
func Test_editorPreview(t *testing.T) {
app := &goBlog{
cfg: &config{
Server: &configServer{},
Db: &configDb{
File: filepath.Join(t.TempDir(), "test.db"),
},
Blogs: map[string]*configBlog{
"en": {
Lang: "en",
Sections: map[string]*configSection{
"test": {
Title: "Test",
},
},
DefaultSection: "test",
},
},
DefaultBlog: "en",
Micropub: &configMicropub{},
},
}
_ = app.initDatabase(false)
app.initComponents(false)
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
app.serveEditorPreview(rw, r.WithContext(context.WithValue(r.Context(), blogKey, "en")))
})
d := wstest.NewDialer(h)
c, _, err := d.Dial("ws://whatever/editor/preview", nil)
require.NoError(t, err)
require.NotNil(t, c)
err = c.WriteMessage(websocket.TextMessage, []byte("---\ntitle: Title\nsection: test\n---\nContent."))
require.NoError(t, err)
mt, msg, err := c.ReadMessage()
require.NoError(t, err)
require.Equal(t, websocket.TextMessage, mt)
msgStr := string(msg)
require.Contains(t, msgStr, "<h1>Title")
require.Contains(t, msgStr, "<p>Content")
require.Contains(t, msgStr, "Test")
}

1
go.mod
View File

@ -33,6 +33,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.9
github.com/microcosm-cc/bluemonday v1.0.16
github.com/paulmach/go.geojson v1.4.0
github.com/posener/wstest v1.2.0
github.com/pquerna/otp v1.3.0
github.com/schollz/sqlite3dump v1.3.1
github.com/snabb/sitemap v1.0.0

3
go.sum
View File

@ -244,6 +244,7 @@ github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyC
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
@ -398,6 +399,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/posener/wstest v1.2.0 h1:PAY0cRybxOjh0yqSDCrlAGUwtx+GNKpuUfid/08pv48=
github.com/posener/wstest v1.2.0/go.mod h1:GkplCx9zskpudjrMp23LyZHrSonab0aZzh2x0ACGRbU=
github.com/pquerna/otp v1.3.0 h1:oJV/SkzR33anKXwQU3Of42rL4wbrffP4uvUf1SvS5Xs=
github.com/pquerna/otp v1.3.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=