Skip to content

Commit

Permalink
improved unique_variation_id issues & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cblavier committed Oct 10, 2022
1 parent 33277a7 commit a126403
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# CHANGELOG

## v0.4.5 (not released)
## v0.4.5 (2022-10-10)

- **bugfix**: `TemplateHelpers.unique_variation_id/2` raises in playground.
- **bugfix**: `TemplateHelpers.unique_variation_id/2` raises in playground if component has an `id` attr.
- **bugfix**: fixed some dialyxir warnings

## v0.4.4 (2022-10-10)

Expand Down
4 changes: 4 additions & 0 deletions lib/phx_live_storybook/helpers/template_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ defmodule PhxLiveStorybook.TemplateHelpers do
end)
end

def unique_variation_id(story, {:single, variation_id}) do
unique_variation_id(story, variation_id)
end

def unique_variation_id(story, {group_id, variation_id}) do
unique_variation_id(story, "#{group_id}-#{variation_id}")
end
Expand Down
8 changes: 4 additions & 4 deletions lib/phx_live_storybook/rendering/code_renderer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule PhxLiveStorybook.Rendering.CodeRenderer do
component_code_heex(
context.story,
fun_or_mod,
strip_attributes(context.story, v),
strip_attributes(context.story, context.group_id, v),
v.let,
v.slots,
context.template
Expand All @@ -61,7 +61,7 @@ defmodule PhxLiveStorybook.Rendering.CodeRenderer do
component_code_heex(
context.story,
fun_or_mod,
strip_attributes(context.story, v),
strip_attributes(context.story, context.group_id, v),
v.let,
v.slots,
context.template
Expand Down Expand Up @@ -233,9 +233,9 @@ defmodule PhxLiveStorybook.Rendering.CodeRenderer do

# If :id is a declared attribute, it is important enough to be shown as component markup.
# Otherwise, we keep it hidden.
defp strip_attributes(story, %RenderingVariation{id: v_id, attributes: attributes}) do
defp strip_attributes(story, group_id, %RenderingVariation{id: v_id, attributes: attributes}) do
if Enum.any?(story.merged_attributes(), &(&1.id == :id)) do
Map.put(attributes, :id, TemplateHelpers.unique_variation_id(story, v_id))
Map.put(attributes, :id, TemplateHelpers.unique_variation_id(story, {group_id, v_id}))
else
Map.delete(attributes, :id)
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule PhxLiveStorybook.MixProject do
use Mix.Project

@version "0.4.4"
@version "0.4.5"

def project do
[
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/storybook_content/tree/component.story.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ defmodule TreeStorybook.Component do

def attributes do
[
%Attr{
id: :id,
type: :string
},
%Attr{
id: :label,
type: :string,
Expand Down
4 changes: 2 additions & 2 deletions test/phx_live_storybook/rendering/code_renderer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ defmodule PhxLiveStorybook.Rendering.CodeRendererTest do
live_component: live_component
} do
code = render_variation_code(component, :hello)
assert code =~ ~s|<.component label="hello"/>|
assert code =~ ~s|<.component id="component-hello" label="hello"/>|

code = render_variation_code(component, :world)
assert code =~ ~s|<.component index={37} label="world"/>|
assert code =~ ~s|<.component id="component-world" index={37} label="world"/>|

code = render_variation_code(live_component, :hello)
assert code =~ ~s|<.live_component module={LiveComponent} label="hello"/>|
Expand Down

0 comments on commit a126403

Please sign in to comment.