Skip to content
quarry Artifact [2637694ccf]
Login

Artifact [2637694ccf]

Artifact 2637694ccf3b35a648c18c45c7625ef4945fb42fac9d5496a60a83601fb638b1:


package api

import (
	"context"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/ops"
)

// The block editor's read: every block's source joins back to the
// draft byte for byte, and each block is rendered on its own through
// the preview's pipeline, polls included.
func TestMarkupBlocksSplitAndRenderEachBlock(t *testing.T) {
	r := ops.NewRegistry()
	if err := RegisterAll(r, Deps{Version: "v", Repos: testRepos(wikiIndex(t))}); err != nil {
		t.Fatal(err)
	}
	body := "# Title\n\nSome *words*.\n\n```poll\nquestion: Ship it?\n- yes\n- no\n```\n"
	out, err := r.Invoke(context.Background(), "markup.blocks", markupPreviewIn{Repo: "test", Body: body})
	if err != nil {
		t.Fatal(err)
	}
	blocks := out.(markupBlocksOut).Blocks
	var joined strings.Builder
	for _, b := range blocks {
		joined.WriteString(b.Source)
	}
	if joined.String() != body {
		t.Fatalf("FAIL: the blocks do not join back to the draft: %q", joined.String())
	}
	if len(blocks) != 3 {
		t.Fatalf("FAIL: %d blocks; want heading, paragraph, poll", len(blocks))
	}
	if !strings.Contains(blocks[0].HTML, "<h") || !strings.Contains(blocks[1].HTML, "<em>") {
		t.Errorf("FAIL: blocks are not rendered: %q %q", blocks[0].HTML, blocks[1].HTML)
	}
	if strings.Contains(blocks[2].HTML, "<pre") {
		t.Errorf("FAIL: the poll block rendered as a code fence, not as its widget: %q", blocks[2].HTML)
	}
}