summaryrefslogblamecommitdiff
path: root/js_test.go
blob: bf7bc46c64dc5adf428b326ab82901456f1c5104 (plain) (tree)
1
2
3


                      
















                                                                                                    





                                  
 











                                                   











                                                                                                                                               
package flyscrape_test

import (
	"testing"

	"flyscrape"

	"github.com/stretchr/testify/require"
)

var html = `
<html>
    <body>
        <main>
            <h1>Plugins</h1>
            <p>The plugin API allows you to inject code into various parts of the build process.</p>
        </main>
    </body>
</html>`

var script = `
import { parse } from "flyscrape";

export const options = {
    url: "https://localhost/",
}

export default function({ html, url }) {
    const $ = parse(html);

    return {
        headline: $("h1").text(),
        body: $("p").text()
    }
}
`

func TestV8(t *testing.T) {
	opts, run, err := flyscrape.Compile(script)
	require.NoError(t, err)
	require.NotNil(t, opts)
	require.NotNil(t, run)

	extract, err := run(flyscrape.ScrapeParams{
		HTML: html,
	})

	require.NoError(t, err)
	require.Equal(t, "Plugins", extract.(map[string]any)["headline"])
	require.Equal(t, "The plugin API allows you to inject code into various parts of the build process.", extract.(map[string]any)["body"])
}