Skip to content

Commit 0bd58ac

Browse files
committed
Improve Examples and LiveExample docs
rename test files and modules related to LiveExample
1 parent 4c1f4c6 commit 0bd58ac

File tree

10 files changed

+48
-31
lines changed

10 files changed

+48
-31
lines changed

lib/surface/catalogue/examples.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ defmodule Surface.Catalogue.Examples do
3737
has no effect when direction is "vertical".
3838
3939
* `assert` - Optional. When using `catalogue_test/1`, generates simple `=~` assertions for
40-
the given text or list of texts. This option is available only for Examples, not Playgrounds.
40+
the given text or list of texts.
4141
42-
When defined at the module level, i.e. passing to `use Surface.Catalogue.Example, ...`, the
42+
When defined at the module level, i.e. passing to `use Surface.Catalogue.Examples, ...`, the
4343
options apply to all examples. Aside from `subject` and `catalogue`, options can be overridden
4444
at the function level using the `@example` module attribute.
4545

lib/surface/catalogue/live_example.ex

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Surface.Catalogue.LiveExample do
2-
@moduledoc """
2+
@moduledoc since: "0.12.0"
3+
@moduledoc ~S'''
34
A generic LiveView to create a single live example for the Surface Catalogue.
45
56
Use `LiveExample` if your example requires manipulating state (data) through
@@ -8,22 +9,22 @@ defmodule Surface.Catalogue.LiveExample do
89
910
## Options
1011
11-
Besides the buit-in options provided by the LiveView itself, an Example also
12+
Besides the buit-in options provided by the LiveView itself, a LiveExample also
1213
provides the following options:
1314
1415
* `subject` - Required. The target component of the Example.
1516
16-
* `height` - Required. The height of the Example.
17-
1817
* `catalogue` - Optional. A module that implements the `Surface.Catalogue`
1918
providing additional information to the catalogue tool. Usually required
2019
if you want to share your components as a library.
2120
22-
* `body` - Optional. Sets/overrides the attributes of the the Example's body tag.
23-
Useful to set a different background or padding.
21+
* `height` - Required. The height of the Example.
2422
2523
* `title` - Optional. The title of the example.
2624
25+
* `body` - Optional. Sets/overrides the attributes of the the Example's body tag.
26+
Useful to set a different background or padding.
27+
2728
* `direction` - Optional. Defines how the example + code boxes should be displayed.
2829
Available values are "horizontal" or "vertical". Default is "horizontal" (side-by-side).
2930
@@ -34,7 +35,23 @@ defmodule Surface.Catalogue.LiveExample do
3435
* `assert` - Optional. When using `catalogue_test/1`, generates simple `=~` assertions for
3536
the given text or list of texts.
3637
37-
"""
38+
## Example
39+
40+
defmodule MyApp.Catalogue.MyComponentExample do
41+
use Surface.Catalogue.LiveExample,
42+
subject: MyApp.MyComponent,
43+
title: "An example for MyComponent",
44+
assert: ["Value is 0"]
45+
46+
data value, :integer, default: 0
47+
48+
def render(assigns) do
49+
~F"""
50+
<MyApp.MyComponent {=value}/>
51+
"""
52+
end
53+
end
54+
'''
3855

3956
defmacro __using__(opts) do
4057
subject = Surface.Catalogue.fetch_subject!(opts, __MODULE__, __CALLER__)

test/support/catalogue/fake_example.ex renamed to test/support/catalogue/fake_live_example.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Surface.Catalogue.FakeExample do
1+
defmodule Surface.Catalogue.FakeLiveExample do
22
use Surface.Catalogue.LiveExample,
33
subject: Surface.FakeComponent,
44
title: "A fake example",

test/support/catalogue/fake_example_moduledoc.ex renamed to test/support/catalogue/fake_live_example_moduledoc.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Surface.Catalogue.FakeExampleModuleDocFalse do
1+
defmodule Surface.Catalogue.FakeLiveExampleModuleDocFalse do
22
@moduledoc false
33

44
use Surface.Catalogue.LiveExample,

test/support/catalogue/fake_example_with_user_config.ex renamed to test/support/catalogue/fake_live_example_with_user_config.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Surface.Catalogue.FakeExampleWithUserConfig do
1+
defmodule Surface.Catalogue.FakeLiveExampleWithUserConfig do
22
use Surface.Catalogue.LiveExample,
33
subject: Surface.FakeComponent,
44
head_css: "User's fake css",

test/support/live_view_test/fake_example.ex renamed to test/support/live_view_test/fake_live_example.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Surface.LiveViewTestTest.FakeExample do
1+
defmodule Surface.LiveViewTestTest.FakeLiveExample do
22
use Surface.Catalogue.LiveExample,
33
subject: Surface.LiveViewTestTest.FakeComponent,
44
title: "A fake example",

test/support/live_view_test/fake_example_for_other_fake_component.ex renamed to test/support/live_view_test/fake_live_example_for_other_fake_component.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Surface.LiveViewTestTest.FakeExampleForOtherFakeComponent do
1+
defmodule Surface.LiveViewTestTest.FakeLiveExampleForOtherFakeComponent do
22
use Surface.Catalogue.LiveExample,
33
subject: Surface.LiveViewTestTest.OtherFakeComponent,
44
title: "A fake example"

test/surface/catalogue/catalogue_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
defmodule Surface.Catalogue.CatalogueTest do
22
use ExUnit.Case
33

4-
alias Surface.Catalogue.FakeExample
4+
alias Surface.Catalogue.FakeLiveExample
55
alias Surface.Catalogue.FakePlayground
6-
alias Surface.Catalogue.FakeExampleWithUserConfig
6+
alias Surface.Catalogue.FakeLiveExampleWithUserConfig
77

88
setup do
99
Application.delete_env(:surface_catalogue, :assets_config)
@@ -12,7 +12,7 @@ defmodule Surface.Catalogue.CatalogueTest do
1212

1313
describe "get_config/1" do
1414
test "get default configuration if none is provided" do
15-
config = Surface.Catalogue.get_config(FakeExample)
15+
config = Surface.Catalogue.get_config(FakeLiveExample)
1616

1717
assert config[:head_css] =~ "/assets/app.css"
1818
assert config[:head_js] =~ "/assets/app.js"
@@ -24,7 +24,7 @@ defmodule Surface.Catalogue.CatalogueTest do
2424
head_js: "Configs's fake head js"
2525
)
2626

27-
config = Surface.Catalogue.get_config(FakeExample)
27+
config = Surface.Catalogue.get_config(FakeLiveExample)
2828

2929
assert config[:head_css] =~ "Configs's fake head css"
3030
assert config[:head_js] =~ "Configs's fake head js"
@@ -38,7 +38,7 @@ defmodule Surface.Catalogue.CatalogueTest do
3838
end
3939

4040
test "user config overrides default and catalogue configs" do
41-
config = Surface.Catalogue.get_config(FakeExampleWithUserConfig)
41+
config = Surface.Catalogue.get_config(FakeLiveExampleWithUserConfig)
4242

4343
assert config[:head_css] =~ "User's fake css"
4444
assert config[:head_js] =~ "User's fake js"

test/surface/catalogue/live_example_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
defmodule Surface.Catalogue.LiveExampleTest do
22
use ExUnit.Case
33

4-
alias Surface.Catalogue.FakeExample
5-
alias Surface.Catalogue.FakeExampleModuleDocFalse
4+
alias Surface.Catalogue.FakeLiveExample
5+
alias Surface.Catalogue.FakeLiveExampleModuleDocFalse
66

77
test "saves subject as metadata" do
8-
meta = Surface.Catalogue.get_metadata(FakeExample)
8+
meta = Surface.Catalogue.get_metadata(FakeLiveExample)
99

1010
assert meta.subject == Surface.FakeComponent
1111
end
1212

1313
test "saves render/1's content as metadata" do
14-
[meta] = Surface.Catalogue.get_metadata(FakeExample).examples_configs
14+
[meta] = Surface.Catalogue.get_metadata(FakeLiveExample).examples_configs
1515

1616
assert Keyword.fetch!(meta, :code) == "The code\n"
1717
end
1818

1919
test "saves user config" do
20-
config = Surface.Catalogue.get_config(FakeExample)
20+
config = Surface.Catalogue.get_config(FakeLiveExample)
2121

2222
assert config[:title] == "A fake example"
2323
end
2424

2525
test "saves render/1's content as metadata when moduledoc is false" do
26-
[meta] = Surface.Catalogue.get_metadata(FakeExampleModuleDocFalse).examples_configs
26+
[meta] = Surface.Catalogue.get_metadata(FakeLiveExampleModuleDocFalse).examples_configs
2727

2828
assert Keyword.fetch!(meta, :code) == "The code\n"
2929
end

test/surface/live_view_test_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ defmodule Surface.LiveViewTestTest do
4141
tests = get_test_functions(Surface.LiveViewTestTest.PassingCatalogueSubjectTests)
4242

4343
assert tests == [
44-
# Using Example
45-
{:"test Surface.LiveViewTestTest.FakeExample.render", 1},
4644
# Using Examples
4745
{:"test Surface.LiveViewTestTest.FakeExamples.example_with_assert_text", 1},
4846
{:"test Surface.LiveViewTestTest.FakeExamples.example_with_assert_texts", 1},
4947
{:"test Surface.LiveViewTestTest.FakeExamples.example_with_opts", 1},
5048
{:"test Surface.LiveViewTestTest.FakeExamples.example_without_opts", 1},
49+
# Using LiveExample
50+
{:"test Surface.LiveViewTestTest.FakeLiveExample.render", 1},
5151
# Using Playground
5252
{:"test Surface.LiveViewTestTest.FakePlayground", 1}
5353
]
@@ -56,15 +56,15 @@ defmodule Surface.LiveViewTestTest do
5656
test "passing :all" do
5757
tests = get_test_functions(Surface.LiveViewTestTest.PassingCatalogueAllTests)
5858

59-
assert {:"test Surface.LiveViewTestTest.FakeExample.render", 1} in tests
60-
assert {:"test Surface.LiveViewTestTest.FakeExampleForOtherFakeComponent.render", 1} in tests
59+
assert {:"test Surface.LiveViewTestTest.FakeLiveExample.render", 1} in tests
60+
assert {:"test Surface.LiveViewTestTest.FakeLiveExampleForOtherFakeComponent.render", 1} in tests
6161
end
6262

6363
test "passing :all with the :except option" do
6464
tests = get_test_functions(Surface.LiveViewTestTest.PassingCatalogueAllAndExceptTests)
6565

66-
assert {:"test Surface.LiveViewTestTest.FakeExample.render", 1} in tests
67-
refute {:"test Surface.LiveViewTestTest.FakeExampleForOtherFakeComponent.render", 1} in tests
66+
assert {:"test Surface.LiveViewTestTest.FakeLiveExample.render", 1} in tests
67+
refute {:"test Surface.LiveViewTestTest.FakeLiveExampleForOtherFakeComponent.render", 1} in tests
6868
end
6969

7070
test "warns when passing an undefined module (subject)" do

0 commit comments

Comments
 (0)