Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce unnecessary calls to Beacon.Loader in tests #402

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading