package slidestest import ( "net/http" "net/url" "strings" "testing" ) // The sample deck's own words, as the seed writes them (sample.go). // Slide 2 and slide 3 are named here because the paging test needs two // slides that are told apart by their headings alone. const ( sampleSlide2Heading = "Text and marks" sampleSlide3Heading = "Regions, not coordinates" ) // sampleDeck signs a person in and returns the path of the deck their // first sign-in seeded. Eight slides, notes on every one — the fixture // every viewer test here wants, without writing one of its own. func sampleDeck(t *testing.T, a *testApp, cookies []*http.Cookie) string { t.Helper() list := mustGet(t, a, "/", http.StatusOK, cookies).Body.String() path := deckPath.FindString(list) if path == "" { t.Fatalf("GET / lists no deck to view:\n%s", list) } return path } // slideSection returns the one
on a viewer page, // so an assertion about "the slide being shown" cannot be satisfied by // the filmstrip, which names every other slide on the same page. func slideSection(t *testing.T, page string) string { t.Helper() if n := strings.Count(page, "
") if end < 0 { t.Fatalf("the rendered slide is not closed:\n%s", page) } return page[start : start+end] } // TestViewerPagesThroughSlides: ?slide=N is the whole viewer. Slide 2 // shows the second slide and only the second slide, the next link goes // to 3, the previous link goes to 1, and the counter says where you // are in words rather than only in a URL. func TestViewerPagesThroughSlides(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path := sampleDeck(t, a, cookies) page := mustGet(t, a, path+"?slide=2", http.StatusOK, cookies).Body.String() section := slideSection(t, page) if !strings.Contains(section, sampleSlide2Heading) { t.Errorf("?slide=2 does not render the second slide:\n%s", section) } if strings.Contains(section, sampleSlide3Heading) { t.Errorf("?slide=2 rendered the third slide too:\n%s", section) } if !strings.Contains(page, `href="`+path+`?slide=3"`) { t.Errorf("no next link to slide 3:\n%s", page) } if !strings.Contains(page, `href="`+path+`?slide=1"`) { t.Errorf("no previous link to slide 1:\n%s", page) } if !strings.Contains(page, "2 of 8") { t.Errorf("the counter does not say which slide this is:\n%s", page) } // The filmstrip names every slide by its own heading and links to // it — that is how a person jumps rather than clicking next seven // times. if !strings.Contains(page, sampleSlide3Heading) { t.Errorf("the filmstrip does not name the third slide:\n%s", page) } if !strings.Contains(page, `href="`+path+`?slide=8"`) { t.Errorf("the filmstrip does not link the last slide:\n%s", page) } // The ends are ends: no link off either edge of the deck. first := mustGet(t, a, path, http.StatusOK, cookies).Body.String() if strings.Contains(first, `?slide=0"`) { t.Errorf("the first slide offers a previous link:\n%s", first) } if !strings.Contains(first, "1 of 8") { t.Errorf("no ?slide= does not default to the first slide:\n%s", first) } last := mustGet(t, a, path+"?slide=8", http.StatusOK, cookies).Body.String() if strings.Contains(last, `?slide=9"`) { t.Errorf("the last slide offers a next link:\n%s", last) } // Off the end is not a clamp and not a 500: it is a page that does // not exist. mustGet(t, a, path+"?slide=9", http.StatusNotFound, cookies) mustGet(t, a, path+"?slide=0", http.StatusNotFound, cookies) mustGet(t, a, path+"?slide=two", http.StatusNotFound, cookies) } // TestViewerOnPhoneViewportReflows: ?vp=phone is the settings screen's // "preview on a phone" — the same page, rendered for the other // viewport, and the viewport travels with every link on it. func TestViewerOnPhoneViewportReflows(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path := sampleDeck(t, a, cookies) page := mustGet(t, a, path+"?slide=2&vp=phone", http.StatusOK, cookies).Body.String() if !strings.Contains(page, `data-vp="phone"`) { t.Errorf("?vp=phone does not render the phone viewport:\n%s", page) } // Paging out of the preview would be a trap: every link keeps it. if !strings.Contains(page, `href="`+path+`?slide=3&vp=phone"`) { t.Errorf("the next link drops the phone viewport:\n%s", page) } desktop := mustGet(t, a, path+"?slide=2", http.StatusOK, cookies).Body.String() if strings.Contains(desktop, `data-vp="phone"`) { t.Errorf("the desktop viewer claims the phone viewport:\n%s", desktop) } } // TestViewerShowsNotesToItsPeople: the notes are under the slide for // anyone who reached the deck signed in, and the notes page is all of // them at once — the scriptless presenter's crib sheet (spec §8). func TestViewerShowsNotesToItsPeople(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path := sampleDeck(t, a, cookies) page := mustGet(t, a, path+"?slide=2", http.StatusOK, cookies).Body.String() if !strings.Contains(page, "