Skip to content

Commit

Permalink
Reduce unnecessary calls to Beacon.Loader in tests (#402)
Browse files Browse the repository at this point in the history
* Reduce unnecessary calls to Beacon.Loader in tests

* call page_assigns/0 instead of walking the ast

that fn wasn't available before

* fix map order for otp 23

* clean up
  • Loading branch information
leandrocp authored Feb 2, 2024
1 parent 1f07ee1 commit 1ddd87c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 45 deletions.
34 changes: 12 additions & 22 deletions test/beacon/loader/page_module_loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ defmodule Beacon.Loader.PageModuleLoaderTest do
%{"property" => "og:url", "content" => "http://example.com/{{ page.path }}"}
]
]
|> published_page_fixture()
|> page_fixture()
|> Repo.preload(:event_handlers)

Beacon.Loader.load_page(page)

{:ok, _module, ast} = PageModuleLoader.load_page!(page)
{:ok, module, _ast} = PageModuleLoader.load_page!(page)

assert has_fields?(ast, [{"content", "MY TEST PAGE"}, {"property", "og:description"}])
assert has_fields?(ast, [{"content", "http://example.com/page/meta-tag"}, {"property", "og:url"}])
assert Enum.sort(module.page_assigns().meta_tags) == [
%{"content" => "MY TEST PAGE", "property" => "og:description"},
%{"content" => "http://example.com/page/meta-tag", "property" => "og:url"}
]
end

test "interpolates raw_schema snippets" do

Check failure on line 77 in test/beacon/loader/page_module_loader_test.exs

View workflow job for this annotation

GitHub Actions / test: OTP 23 | Elixir 1.13.0 | Phoenix 1.7.0 | LiveView 0.19.0

test page_assigns/1 interpolates raw_schema snippets (Beacon.Loader.PageModuleLoaderTest)

Check failure on line 77 in test/beacon/loader/page_module_loader_test.exs

View workflow job for this annotation

GitHub Actions / test: OTP 26 | Elixir 1.15 | Phoenix ~> 1.7 | LiveView ~> 0.20

test page_assigns/1 interpolates raw_schema snippets (Beacon.Loader.PageModuleLoaderTest)
Expand Down Expand Up @@ -108,32 +108,22 @@ defmodule Beacon.Loader.PageModuleLoaderTest do
}
]
]
|> published_page_fixture()
|> page_fixture()
|> Repo.preload(:event_handlers)

Beacon.Loader.load_page(page)
{:ok, module, _ast} = PageModuleLoader.load_page!(page)

{:ok, _module, ast} = PageModuleLoader.load_page!(page)
[raw_schema] = module.page_assigns().raw_schema

assert has_fields?(ast,
assert Enum.sort(raw_schema) == [
"@context": "https://schema.org",
"@type": "BlogPosting",
author: {:%{}, [], ["@type": "Person", name: "author_1"]},
author: %{name: "author_1", "@type": "Person"},
headline: "hello world"
)
]
end
end

defp has_fields?(ast, match) do
{_new_ast, present} =
Macro.prewalk(ast, false, fn
{:%{}, _, fields} = node, acc -> {node, acc or match == Enum.sort(fields)}
node, acc -> {node, acc}
end)

present
end

describe "render" do
test "do not load template on boot stage" do
page = page_fixture(site: "my_site", path: "1") |> Repo.preload([:event_handlers, :variants])
Expand Down
9 changes: 3 additions & 6 deletions test/beacon/loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Beacon.LoaderTest do
stylesheet_fixture()

layout =
layout_fixture(
published_layout_fixture(
template: """
<header>layout_v1</header>
<%= @inner_content %>
Expand All @@ -44,7 +44,7 @@ defmodule Beacon.LoaderTest do
)

page =
page_fixture(
published_page_fixture(
layout_id: layout.id,
path: "/loader_test",
template: """
Expand All @@ -55,10 +55,7 @@ defmodule Beacon.LoaderTest do
"""
)

Content.publish_layout(layout)
Content.publish_page(page)

Beacon.reload_site(:my_site)
Beacon.Loader.load_page(page)

[layout: layout, page: page, component: component]
end
Expand Down
4 changes: 0 additions & 4 deletions test/beacon/tailwind_compiler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ defmodule Beacon.TailwindCompilerTest do
"""
)

Beacon.Loader.load_stylesheets(@site)
Beacon.Loader.load_components(@site)
Beacon.Loader.load_layouts(@site)
Beacon.Loader.load_pages(@site)
Beacon.Loader.load_runtime_css(@site)

:ok
Expand Down
16 changes: 8 additions & 8 deletions test/beacon_web/components/components_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ defmodule BeaconWeb.ComponentsTest do
defp create_page_with_component(template) do
layout = published_layout_fixture()

published_page_fixture(
layout_id: layout.id,
path: "home",
template: template
)

Beacon.Loader.load_components(:my_site)
Beacon.Loader.load_pages(:my_site)
page =
published_page_fixture(
layout_id: layout.id,
path: "home",
template: template
)

Beacon.Loader.load_page(page)
end
end
9 changes: 4 additions & 5 deletions test/beacon_web/live/page_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule BeaconWeb.Live.PageLiveTest do
)

page_home =
page_fixture(
published_page_fixture(
layout_id: layout.id,
path: "home",
template: """
Expand Down Expand Up @@ -74,9 +74,7 @@ defmodule BeaconWeb.Live.PageLiveTest do
"""
})

Content.publish_page(page_home)

_page_without_meta_tags =
page_without_meta_tags =
published_page_fixture(
layout_id: layout.id,
path: "without_meta_tags",
Expand All @@ -87,7 +85,8 @@ defmodule BeaconWeb.Live.PageLiveTest do
meta_tags: nil
)

Beacon.reload_site(:my_site)
Beacon.Loader.load_page(page_home)
Beacon.Loader.load_page(page_without_meta_tags)

[layout: layout]
end
Expand Down

0 comments on commit 1ddd87c

Please sign in to comment.