## Search

Page index: https://gasmod.dev/llms.txt. Add index.md to any page URL for its Markdown.

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_b845b566cb63bd033e9c576d&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.

# ui/uitest

> Package uitest holds a mock of ui.Provider, MockUI, for tests of code that renders pages.

The Markdown of https://gasmod.dev/reference/ui/uitest/ from the Gas documentation, whose index for LLMs is https://gasmod.dev/llms.txt

```go
import "github.com/gasmod/gas/ui/uitest"
```

Package uitest holds a mock of ui.Provider, [MockUI](#MockUI), for tests of code
that renders pages.

## Usage

The mock records each call, and returns what the function in the field
named after the method returns:

```go
m := &uitest.MockUI{
	RenderFn: func(_ context.Context, w http.ResponseWriter, name string, _ any, _ ...ui.RenderOption) error {
		_, err := io.WriteString(w, "<h1>"+name+"</h1>")
		return err
	},
}
err := m.Render(ctx, w, "home", nil, ui.WithStatus(http.StatusAccepted))
// w holds <h1>home</h1>, err is nil, and m.CallCount("Render") is 1
```

The mock keeps none of the rules of the ui package: it checks no name
or option and compiles nothing. To test against those rules, build a
ui.Service over a gas/template/memory store.

## Defaults

With a field nil, its method writes nothing to the response writer and
returns nil:

- Render and RenderFragment render nothing.
- RegisterFuncs accepts every map, nil and invalid functions included.

## Calls

Each method appends a [Call](#Call) with its name and its arguments after the
context and the response writer, before it calls its field. The
arguments recorded:

- Render and RenderFragment: the name, the data and the options. The
data is recorded as given. The options are resolved by
ui.ApplyRenderOptions into a ui.RenderOptions.
- RegisterFuncs: a copy of the function map, nil for nil.

To read the record:

- Calls returns a copy of the calls in the order they were made, with
copies of the function maps, so that changing what it returns
changes no recorded call.
- CallCount counts those of one method.
- Reset forgets them all.

The methods may be called from several goroutines at once, as the calls
are kept under a lock, which Calls, CallCount and Reset take too.
Setting a field while a method runs is a data race.


## API reference

<a id="Call"></a>

### type Call

```go
type Call struct {
	// Method is the name of the method called.
	Method string
	// Args are the method's arguments after the context and the response
	// writer, in order.
	Args []any
}
```

Call is one call of the mock's method.

<a id="MockUI"></a>

### type MockUI

```go
type MockUI struct {
	// RenderFn is what Render returns. When it is nil, Render writes
	// nothing and returns nil.
	RenderFn func(ctx context.Context, w http.ResponseWriter, name string, data any, opts ...ui.RenderOption) error
	// RenderFragmentFn is what RenderFragment returns. When it is nil,
	// RenderFragment writes nothing and returns nil.
	RenderFragmentFn func(
		ctx context.Context,
		w http.ResponseWriter,
		name string,
		data any,
		opts ...ui.RenderOption,
	) error
	// RegisterFuncsFn is what RegisterFuncs returns. When it is nil,
	// RegisterFuncs returns nil.
	RegisterFuncsFn func(funcs template.FuncMap) error
	// contains filtered or unexported fields
}
```

MockUI is a ui.Provider that records its calls, as described in the
package documentation.

<a id="MockUI.CallCount"></a>

#### func (*MockUI) CallCount

```go
func (m *MockUI) CallCount(method string) int
```

CallCount returns how many calls were made to method.

<a id="MockUI.Calls"></a>

#### func (*MockUI) Calls

```go
func (m *MockUI) Calls() []Call
```

Calls returns a copy of the calls, in the order they were made:
changing a function map it returns changes no recorded call.

<a id="MockUI.RegisterFuncs"></a>

#### func (*MockUI) RegisterFuncs

```go
func (m *MockUI) RegisterFuncs(funcs template.FuncMap) error
```

RegisterFuncs records the call and returns RegisterFuncsFn's result.

<a id="MockUI.Render"></a>

#### func (*MockUI) Render

```go
func (m *MockUI) Render(
	ctx context.Context,
	w http.ResponseWriter,
	name string,
	data any,
	opts ...ui.RenderOption,
) error
```

Render records the call and returns RenderFn's result.

<a id="MockUI.RenderFragment"></a>

#### func (*MockUI) RenderFragment

```go
func (m *MockUI) RenderFragment(
	ctx context.Context,
	w http.ResponseWriter,
	name string,
	data any,
	opts ...ui.RenderOption,
) error
```

RenderFragment records the call and returns RenderFragmentFn's result.

<a id="MockUI.Reset"></a>

#### func (*MockUI) Reset

```go
func (m *MockUI) Reset()
```

Reset forgets every call.
