oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package mapatest
4
5import (
6 "context"
7 "runtime"
8 "testing"
9
10 "github.com/chromedp/chromedp"
11 "github.com/chromedp/chromedp/kb"
12
13 "amadan.net/oficina/mapa/internal/mapa"
14)
15
16// Column layouts in a real Chromium (columns and icons spec §9): COL6,
17// no shift with layouts of two and four columns, at three widths, light
18// and dark, with and without a draft, and stacking by the page column's
19// width; COL10, the keyboard: Insert → Two columns, ⌥⌘→ and Undo
20// columns, with axe.
21
22const layoutBody = "# Plan\n\n:::: columns\n\n::: column\nLeft, with " +
23 "[[Pilot]].\n\n- one\n- two\n\n:::\n\n::: column\nRight.\n\n:::\n\n" +
24 "::::\n\n:::: columns\n\n::: column\nA\n\n:::\n\n::: column\nB\n\n" +
25 ":::\n\n::: column\nC\n\n:::\n\n::: column\n:::\n\n::::\n\nAfter."
26
27func TestCOL6LayoutsMountWithoutAShift(t *testing.T) {
28 rig, store := browserRig(t, 0)
29 ctx := context.Background()
30 page, err := store.CreateTeamPage(ctx, Bo, "Pilot", layoutBody)
31 if err != nil {
32 t.Fatal(err)
33 }
34 viewer, _ := store.Viewer(ctx, Ana, nil)
35 drive(t, rig, onNewDocument(shiftRecorder))
36 for _, draft := range []bool{false, true} {
37 if draft {
38 write := mapa.DraftWrite{Base: page.Revision,
39 Body: layoutBody + "\n\nMine."}
40 if _, err := store.Autosave(ctx, viewer,
41 mapa.DraftKey{PageID: page.ID}, write); err != nil {
42 t.Fatal(err)
43 }
44 }
45 for _, dark := range []bool{false, true} {
46 for _, width := range []int64{1440, 1000, 390} {
47 noShift(t, rig, mapa.PagePath(page), width, dark, draft)
48 }
49 }
50 }
51}
52
53// columnsSideBySide is whether the first layout's columns share a row.
54const columnsSideBySide = `(function () {
55 var columns = document.querySelectorAll("[mapa-columns] > [mapa-column]");
56 return columns[0].getBoundingClientRect().top ===
57 columns[1].getBoundingClientRect().top;
58})()`
59
60func TestCOL6ColumnsStackByThePageColumnsWidth(t *testing.T) {
61 rig, store := browserRig(t, 0)
62 page, err := store.CreateTeamPage(context.Background(), Bo, "Pilot",
63 layoutBody)
64 if err != nil {
65 t.Fatal(err)
66 }
67 for width, side := range map[int64]bool{1440: true, 390: false} {
68 drive(t, rig, chromedp.EmulateViewport(width, 900),
69 chromedp.Navigate(rig.Origin+mapa.PagePath(page)),
70 chromedp.WaitVisible("[mapa-columns]"))
71 if got := read(t, rig, chromedp.Evaluate[bool](
72 columnsSideBySide)); got != side {
73 t.Errorf("%dpx: side by side is %v", width, got)
74 }
75 }
76}
77
78func TestCOL10ColumnsByKeyboard(t *testing.T) {
79 rig, store := browserRig(t, 0)
80 titledPage(t, rig, store, "Columns", "Make me columns.")
81 drive(t, rig, chromedp.Click("article[mapa-body] p"),
82 chromedp.Evaluate[chromedp.Void](`document.querySelector(
83 '[data-mapa-command="insert-columns-2"]').click()`),
84 chromedp.WaitVisible("[mapa-columns] [mapa-column]"))
85 if found := axeSerious(t, rig); found != "" {
86 t.Fatalf("axe, a page with columns:\n%s", found)
87 }
88 drive(t, rig, chromedp.KeyEvent("Right."),
89 chromedp.KeyEvent(kb.ArrowLeft, modAlt()))
90 at := evaluate(t, rig, `(function () {
91 var column = getSelection().anchorNode.parentElement.closest("[mapa-column]");
92 return String([].indexOf.call(column.parentElement.children, column));
93})()`)
94 if at != "0" {
95 t.Fatalf("⌥⌘← left the caret in column %s", at)
96 }
97 drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector(
98 '[data-mapa-command="undo-columns"]').click()`))
99 eventually(t, "the layout undone", func() bool {
100 return evaluate(t, rig,
101 `String(document.querySelectorAll("[mapa-columns]").length)`) == "0"
102 })
103}
104
105// modAlt is ⌥⌘ on a Mac, Ctrl+Alt elsewhere.
106func modAlt() chromedp.KeyOption {
107 if runtime.GOOS == "darwin" {
108 return chromedp.KeyModifiers(chromedp.ModifierMeta |
109 chromedp.ModifierAlt)
110 }
111 return chromedp.KeyModifiers(chromedp.ModifierCtrl |
112 chromedp.ModifierAlt)
113}
114