//go:build browser package slidestest import ( "net/http" "regexp" "strings" "testing" "github.com/chromedp/chromedp" ) var listedDeck = regexp.MustCompile(`/d/[a-z0-9]{10}/edit`) // listedDecks counts the decks on this person's list. func (te *tidyEditor) listedDecks(t *testing.T) int { t.Helper() seen := map[string]bool{} for _, p := range listedDeck.FindAllString(mustGet(t, te.app, "/", http.StatusOK, te.cookies).Body.String(), -1) { seen[p] = true } return len(seen) } // landedOnANewDeck waits for the editor of a deck other than from. func (te *tidyEditor) landedOnANewDeck(from string) { te.rig.Run( waitFor(`/^\/d\/[a-z0-9]{10}\/edit$/.test(location.pathname) && location.pathname !== '`+from+`'`), chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), waitFor(editorBound), ) } // TestFileNewDeckOpensTheNewDeck: File → New deck goes straight to the // new deck's editor, with what was just typed in the old deck saved, and // a second press while it is on its way makes no second deck. func TestFileNewDeckOpensTheNewDeck(t *testing.T) { te := openTidyEditor(t, 2, 1280, 720) first := te.path before := te.listedDecks(t) const typed = "kept-on-the-way-out" te.clickNotes() te.rig.Run( chromedp.KeyEvent(typed), chromedp.Click(chromedp.CSS(`[slides-menu="file"] > summary`)), chromedp.WaitVisible(chromedp.CSS(`[slides-menu="file"] form[action="/decks"] button`)), chromedp.Click(chromedp.CSS(`[slides-menu="file"] form[action="/decks"] button`)), ) te.landedOnANewDeck(first) var second string te.rig.Run(evalInto(&second, `location.pathname`)) if got := te.listedDecks(t); got != before+1 { t.Errorf("File → New deck left %d decks, want %d", got, before+1) } if page := mustGet(t, te.app, first, http.StatusOK, te.cookies).Body.String(); !strings.Contains(page, typed) { t.Errorf("the notes typed just before File → New deck were not saved") } te.rig.Run(chromedp.Evaluate[chromedp.Void](`(() => { const b = document.querySelector('[slides-menu="file"] form[action="/decks"] button') b.click(); b.click() })()`)) te.landedOnANewDeck(second) if got := te.listedDecks(t); got != before+2 { t.Errorf("a double press made %d decks in all, want %d", got, before+2) } }