Surface.LiveViewTest (surface v0.7.0) View Source

Conveniences for testing Surface components.

Link to this section Summary

Functions

Compiles Surface code into a new LiveView module.

Render Surface code.

Wraps a test code so it runs using a custom configuration for a given component.

Link to this section Functions

Link to this macro

compile_surface(code, assigns \\ quote do %{} end)

View Source (macro)

Compiles Surface code into a new LiveView module.

This macro should be used sparingly as it always generates and compiles a new module on-the-fly, which can potentially slow down your test suite.

Its main use is when testing compile-time errors/warnings.

Example

code =
  quote do
    ~F"""
    <KeywordProp prop="some string"/>
    """
  end

message =
  ~S(code:1: invalid value for property "prop". Expected a :keyword, got: "some string".)

assert_raise(CompileError, message, fn ->
  compile_surface(code)
end)
Link to this macro

render_surface(list)

View Source (macro)

Render Surface code.

Use this macro when testing regular rendering of stateless components or live components that don't require a parent live view during the tests.

For tests depending on the existence of a parent live view, e.g. testing events on live components and its side-effects, you need to use either Phoenix.LiveViewTest.live/2 or Phoenix.LiveViewTest.live_isolated/3.

Example

html =
  render_surface do
    ~F"""
    <Link label="user" to="/users/1" />
    """
  end

assert html =~ """
      <a href="/users/1">user</a>
      """
Link to this macro

using_config(component, config, list)

View Source (macro)

Wraps a test code so it runs using a custom configuration for a given component.

Tests using this macro should run synchronously. A warning is shown if the test case is configured as async: true.

Example

using_config TextInput, default_class: "default_class" do
  html =
    render_surface do
      ~F"""
      <TextInput/>
      """
    end

  assert html =~ ~r/class="default_class"/
end