| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | import ( |
| 6 | "net/http" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/chromedp/chromedp" |
| 12 | ) |
| 13 | |
| 14 | var listedDeck = regexp.MustCompile(`/d/[a-z0-9]{10}/edit`) |
| 15 | |
| 16 | // listedDecks counts the decks on this person's list. |
| 17 | func (te *tidyEditor) listedDecks(t *testing.T) int { |
| 18 | t.Helper() |
| 19 | seen := map[string]bool{} |
| 20 | for _, p := range listedDeck.FindAllString(mustGet(t, te.app, "/", http.StatusOK, te.cookies).Body.String(), -1) { |
| 21 | seen[p] = true |
| 22 | } |
| 23 | return len(seen) |
| 24 | } |
| 25 | |
| 26 | // landedOnANewDeck waits for the editor of a deck other than from. |
| 27 | func (te *tidyEditor) landedOnANewDeck(from string) { |
| 28 | te.rig.Run( |
| 29 | waitFor(`/^\/d\/[a-z0-9]{10}\/edit$/.test(location.pathname) && location.pathname !== '`+from+`'`), |
| 30 | chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), |
| 31 | waitFor(editorBound), |
| 32 | ) |
| 33 | } |
| 34 | |
| 35 | // TestFileNewDeckOpensTheNewDeck: File → New deck goes straight to the |
| 36 | // new deck's editor, with what was just typed in the old deck saved, and |
| 37 | // a second press while it is on its way makes no second deck. |
| 38 | func TestFileNewDeckOpensTheNewDeck(t *testing.T) { |
| 39 | te := openTidyEditor(t, 2, 1280, 720) |
| 40 | first := te.path |
| 41 | before := te.listedDecks(t) |
| 42 | |
| 43 | const typed = "kept-on-the-way-out" |
| 44 | te.clickNotes() |
| 45 | te.rig.Run( |
| 46 | chromedp.KeyEvent(typed), |
| 47 | chromedp.Click(chromedp.CSS(`[slides-menu="file"] > summary`)), |
| 48 | chromedp.WaitVisible(chromedp.CSS(`[slides-menu="file"] form[action="/decks"] button`)), |
| 49 | chromedp.Click(chromedp.CSS(`[slides-menu="file"] form[action="/decks"] button`)), |
| 50 | ) |
| 51 | te.landedOnANewDeck(first) |
| 52 | |
| 53 | var second string |
| 54 | te.rig.Run(evalInto(&second, `location.pathname`)) |
| 55 | if got := te.listedDecks(t); got != before+1 { |
| 56 | t.Errorf("File → New deck left %d decks, want %d", got, before+1) |
| 57 | } |
| 58 | if page := mustGet(t, te.app, first, http.StatusOK, te.cookies).Body.String(); !strings.Contains(page, typed) { |
| 59 | t.Errorf("the notes typed just before File → New deck were not saved") |
| 60 | } |
| 61 | |
| 62 | te.rig.Run(chromedp.Evaluate[chromedp.Void](`(() => { |
| 63 | const b = document.querySelector('[slides-menu="file"] form[action="/decks"] button') |
| 64 | b.click(); b.click() |
| 65 | })()`)) |
| 66 | te.landedOnANewDeck(second) |
| 67 | if got := te.listedDecks(t); got != before+2 { |
| 68 | t.Errorf("a double press made %d decks in all, want %d", got, before+2) |
| 69 | } |
| 70 | } |
| 71 | |