Skip to content

Commit

Permalink
fix: order nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Jul 3, 2024
1 parent 6a9108f commit 162c67e
Showing 1 changed file with 18 additions and 43 deletions.
61 changes: 18 additions & 43 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -679,53 +679,28 @@ defmodule Radiator.OutlineTest do
end

describe "get_node_postion/1" do
setup :complex_node_fixture

test "with a given uuid we get all child nodes with positions",%{
node_1: node_1,
node_2: node_2,
node_3: node_3,
node_4: node_4,
node_5: node_5,
node_6: node_6,
nested_node_1: nested_node_1,
nested_node_2: nested_node_2,
parent_node: parent_node
} do
# get_node_postion_for_level(parent_node.uuid)
parent_node = Outline.get_parent_node(node_4)
child_nodes = Outline.get_all_child_nodes(parent_node)

# child_nodes
# sorted_child_nodes = []

# child_nodes.prev_id.nil?

# child_nodes -- first
# sorted_child_nodes = [first]

# child_nodes
# child_map %{
# uuid: {node, position},


# }
# sorted_child_nodes = []

temp_child_nodes = child_nodes
|> Enum.map(fn node -> {node.uuid, {node, nil}} end)
|> Map.new
setup :complex_node_fixture

temp_child_nodes
|> Enum.map(fn {uuid, {node, _}} -> {node, get_position(node.prev_id, temp_child_nodes) end)
end
test "with a given uuid we get all child nodes with positions", %{node_4: node_4} do
ordered_list =
node_4
|> Outline.get_parent_node()
|> Outline.get_all_child_nodes()
|> Enum.map(fn node -> {node.prev_id, node} end)
|> Map.new()
|> order_nodes(nil, [])

assert ordered_list |> Enum.map(& &1.content) ==
["node_1", "node_2", "node_3", "node_4", "node_5", "node_6"]
end
end

def get_position(uuid, %{uuid => {_node, nil}}), do: 1

def get_position(uuid, %{uuid => {_node, position}}), do: position + 1

defp order_nodes(index, prev_id, collection) do
case index[prev_id] do
%{uuid: uuid} = node -> order_nodes(index, uuid, [node | collection])
_ -> Enum.reverse(collection)
end
end

defp assert_level_for_node(tree, node, level) do
node = Enum.filter(tree, fn n -> n.uuid == node.uuid end) |> List.first()
Expand Down

0 comments on commit 162c67e

Please sign in to comment.