Skip to content

Commit

Permalink
notice edit test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cblgh committed Mar 11, 2021
1 parent d27098e commit ebe051c
Showing 1 changed file with 51 additions and 48 deletions.
99 changes: 51 additions & 48 deletions web/handlers/admin/notices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,75 @@ package admin
import (
"fmt"
"net/http"
// "net/url"
"net/url"
"strings"
"testing"

// "github.com/ssb-ngi-pointer/go-ssb-room/admindb"
"github.com/ssb-ngi-pointer/go-ssb-room/admindb"
"github.com/ssb-ngi-pointer/go-ssb-room/web"
"github.com/ssb-ngi-pointer/go-ssb-room/web/router"
"github.com/stretchr/testify/assert"
)

/* TODO: 500s for some reason? */
func TestNoticeAddLanguageIncludesAllFields(t *testing.T) {
ts := newSession(t)
a := assert.New(t)
// instantiate the urlTo helper (constructs urls for us!)
urlTo := web.NewURLTo(ts.Router)

// to test translations we first need to add a notice to the notice mockdb
notice := admindb.Notice{
ID: 1,
Title: "News",
Content: "Breaking News: This Room Has News",
Language: "en-GB",
}
ts.NoticeDB.GetByIDReturns(notice, nil)
// the we need to pin the mocked notice
ts.PinnedDB.GetReturns(&notice, nil)

/* TODO: are you only supposed to add translations to pinned notices? */
u := urlTo(router.AdminNoticeDraftTranslation, "name", admindb.NoticeNews.String())
html, resp := ts.Client.GetHTML(u.String())
a.Equal(http.StatusOK, resp.Code)
fmt.Println(html.Html())
}

func TestNoticeEditFormIncludesAllFields(t *testing.T) {
ts := newSession(t)
a := assert.New(t)
// instantiate the urlTo helper (constructs urls for us!)
urlTo := web.NewURLTo(ts.Router)

checkFormInputs := func(t *testing.T, url string) {
html, resp := ts.Client.GetHTML(url)
fmt.Println(html.Html())
a := assert.New(t)
/* TODO: continue test by checking for 1) forms, and 2) their required input fields */
// helper function to test all form inputs that should exist on a given edit page
checkFormInputs := func(u *url.URL) {
html, resp := ts.Client.GetHTML(u.String())
a.Equal(http.StatusOK, resp.Code, "Wrong HTTP status code")
// Phrased these tests this way (multiple tests checking #) to present less confusion if, somehow, the inputs somehow end up being more than 1 :)
titleInputs := html.Find(`input[name="title"]`).Length()
a.True(titleInputs > 0, "Title input is missing")
a.True(titleInputs == 1, "Expected only one title input (there were several)")
testElementExistence := func(tag, name string) {
inputs := html.Find(fmt.Sprintf(`%s[name="%s"]`, tag, name)).Length()
// phrased these tests this way (multiple tests checking #) to present less confusion if, somehow, the inputs end up being more than 1 :)
a.True(inputs > 0, fmt.Sprintf("%s input is missing", strings.Title(name)))
a.True(inputs == 1, fmt.Sprintf("Expected only one %s input (there were several)", name))
}
testElementExistence("input", "title")
testElementExistence("input", "language") // this test will fail when converted to dropdown from single input
testElementExistence("textarea", "content")
testElementExistence("input", "id")

// make sure the id input is hidden
idInput := html.Find(`input[name="id"]`)
idType, idHasType := idInput.Attr("type")
idInputs := idInput.Length()
a.True(idInputs > 0, "ID input is missing")
a.True(idInputs == 1, "Expected only one id input (there were several)")
a.True(idHasType && idType == "hidden", "Expected id input to be of type hidden")
}
// Construct notice url to edit
url := urlTo(router.AdminNoticeEdit, "id", 0).String()
checkFormInputs(t, url)
// Create mock notice data to operate on
// notice := admindb.Notice{
// ID: 1,
// Title: "News",
// Content: "Breaking News: This Room Has News",
// Language: "en",
// }
}

/* TODO: use this test as an example for pair programming 2021-03-09 */
// Assumption: I can use all the exported routes for my tests
// Reality: I can only use the routes associated with the 'correct' testing setup function
// (setup for all, newSession for admin)

// TestEditButtonVisibleForAdmin specifically tests if the regular notice view at /notice/show?id=<x>
// displays an edit button for admin users
func TestNoticeEditButtonVisibleForAdmin(t *testing.T) {
ts := newSession(t)
// a := assert.New(t)

urlTo := web.NewURLTo(ts.Router)

// Create mock notice data to operate on
notice := admindb.Notice{
ID: 1,
Title: "News",
Content: "Breaking News: This Room Has News",
Language: "en-GB",
}
ts.NoticeDB.GetByIDReturns(notice, nil)
// Construct notice url to edit
baseurl := urlTo(router.CompleteNoticeShow, "id", 3)

t.Log(baseurl.String())

// url := fmt.Sprintf("%s?id=%d", baseurl.String(), 1)
//
// html, resp := ts.Client.GetHTML(url)
// fmt.Println(html.Html())
// a.Equal(http.StatusOK, resp.Code, "Wrong HTTP status code")
checkFormInputs(urlTo(router.AdminNoticeEdit, "id", 1))
}

0 comments on commit ebe051c

Please sign in to comment.