Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templ version #1

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions styledjsx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,68 @@ export default function () {
`
diff.TestContent(t, actual, expected)
}

func TestTempl(t *testing.T) {
input := `
package view

templ Story(story *hackernews.Story) {
<div class="story">
<div>
<a>
{ story.Title }
</a>
if story.URL != "" {
<a class="url" href={ templ.URL(story.URL) }>({ formatURL(story.URL) })</a>
}
</div>
<div class="meta">
{ strconv.Itoa(story.Points) } points by { story.Author } • { " " }
@Time(story.CreatedAt)
</div>
<style scoped>{` + "`" + `
.story {
background: red;
}
.url {
text-transform: none;
}
.meta {
padding: 10px;
}
` + "`" + `}</style>
</div>
}
`

rewriter := styledjsx.New()
actual, err := rewriter.Rewrite("input.jsx", string(input))
if err != nil {
t.Fatal(err)
}
expected := `
import Style from "styled-jsx";

package view

templ Story(story *hackernews.Story) {
<div class="jsx-4GjC3K story">
<div class="jsx-4GjC3K">
<a class="jsx-4GjC3K">
{ story.Title }
</a>
if story.URL != "" {
<a class="jsx-4GjC3K url" href={ templ.URL(story.URL) }>({ formatURL(story.URL) })</a>
}
</div>
<div class="jsx-4GjC3K meta">
{ strconv.Itoa(story.Points) } points by { story.Author } • { " " }
@Time(story.CreatedAt)
</div>
<Style scoped id="jsx-4GjC3K">{` + "`" + `.story.jsx-4GjC3K { background: red }
.url.jsx-4GjC3K { text-transform: none }
.meta.jsx-4GjC3K { padding: 10px }` + "`" + `}</Style>
</div>
}`
diff.TestContent(t, actual, expected)
}