Skip to content

Commit

Permalink
Add tests for node breadcrumbs ordering by depth
Browse files Browse the repository at this point in the history
  • Loading branch information
miks committed Mar 22, 2016
1 parent f8261e8 commit c994c38
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 8 deletions.
Expand Up @@ -61,16 +61,13 @@ def add_resource_breadcrumb(resource)
end

def ancestor_nodes(resource)
ancestors = []
if resource.new_record?
if resource.parent_id
ancestors = resource.parent.ancestors.reorder(:depth)
ancestors += [resource.parent]
end
if resource.persisted?
resource.ancestors.reorder(:depth)
elsif resource.new_record? && resource.parent
resource.parent.ancestors.reorder(:depth) + [resource.parent]
else
ancestors = resource.ancestors.reorder(:depth)
[]
end
ancestors
end

def self.resource_class
Expand Down
Expand Up @@ -8,6 +8,40 @@
end
end

describe "#ancestor_nodes" do
let(:node){ Node.new }
let(:ancestors){ Node.where(id: 1212) }

before do
allow(ancestors).to receive(:reorder).with(:depth).and_return(["depth_ordered_ancestors"])
end

context "when new node" do
context "when node has parent" do
it "returns parent ancestors ordered by depth alongside parent ancestor" do
parent_node = Node.new
node.parent = parent_node
allow(parent_node).to receive(:ancestors).and_return(ancestors)
expect(subject.ancestor_nodes(node)).to eq(["depth_ordered_ancestors", parent_node])
end
end

context "when node has no parent" do
it "returns empty array" do
expect(subject.ancestor_nodes(node)).to eq([])
end
end
end

context "when persisted node" do
it "returns resource ancestors ordered by depth" do
allow(node).to receive(:persisted?).and_return(true)
allow(node).to receive(:ancestors).and_return(ancestors)
expect(subject.ancestor_nodes(node)).to eq(["depth_ordered_ancestors"])
end
end
end

describe ".resource_class" do
it "looks up node class in releaf content resource configuration" do
config = { 'OtherSite::OtherNode' => { controller: 'Releaf::Content::NodesController' } }
Expand Down
68 changes: 68 additions & 0 deletions releaf-content/spec/features/nodes_spec.rb
Expand Up @@ -378,7 +378,75 @@ def create_child parent, child_text, position=nil
expect(page).to have_content "Node class: Node"
expect(page).to have_content "Releaf github repository"
end
end

feature "breadcrumbs ordering by depth", with_releaf_node_controller: true do
# create some nodes and then rearrange them to get some with oldest under newest

scenario "create and reorder node depth" do
visit releaf_content_nodes_path
find('li[data-id="' + @lv_root.id.to_s + '"] > .toolbox-cell button').click
click_link "Add child"
within '.ajaxbox-inner .dialog.content-type' do
click_link "Text page"
end
create_resource do
fill_in "resource_name", with: "TextContent_1"
fill_in_richtext 'Text', with: "asdasd"
end
expect(page).to have_breadcrumbs("Releaf/content/nodes", "lv", "TextContent_1")

visit releaf_content_nodes_path
find('li[data-id="' + @lv_root.id.to_s + '"] > .toolbox-cell button').click
click_link "Add child"
within '.ajaxbox-inner .dialog.content-type' do
click_link "Text page"
end
create_resource do
fill_in "resource_name", with: "TextContent_2"
fill_in_richtext 'Text', with: "asdasd"
end
expect(page).to have_breadcrumbs("Releaf/content/nodes", "lv", "TextContent_2")

visit releaf_content_nodes_path
find('li[data-id="' + @lv_root.id.to_s + '"] > .toolbox-cell button').click
click_link "Add child"
within '.ajaxbox-inner .dialog.content-type' do
click_link "Text page"
end
create_resource do
fill_in "resource_name", with: "TextContent_3"
fill_in_richtext 'Text', with: "asdasd"
end
expect(page).to have_breadcrumbs("Releaf/content/nodes", "lv", "TextContent_3")


text_page_1_node_id = Node.find_by(name: "TextContent_1").id
text_page_2_node_id = Node.find_by(name: "TextContent_2").id
text_page_3_node_id = Node.find_by(name: "TextContent_3").id

visit edit_releaf_content_node_path(text_page_1_node_id)
expect(page).to have_breadcrumbs("Releaf/content/nodes", "lv", "TextContent_1")
open_toolbox_dialog "Move"
within ".dialog" do
find('li[data-id="' + @lv_root.id.to_s + '"] > .collapser-cell button').click
find("label[for=new_parent_id_#{text_page_2_node_id}]").click
click_button "Move"
end
expect(page).to have_notification("Move succeeded")

visit edit_releaf_content_node_path(text_page_2_node_id)
expect(page).to have_breadcrumbs("Releaf/content/nodes", "lv", "TextContent_2")
open_toolbox_dialog "Move"
within ".dialog" do
find("label[for=new_parent_id_#{text_page_3_node_id}]").click
click_button "Move"
end
expect(page).to have_notification("Move succeeded")

visit edit_releaf_content_node_path(text_page_1_node_id)
expect(page).to have_breadcrumbs("Releaf/content/nodes", "lv", "TextContent_3", "TextContent_2", "TextContent_1")
end
end

feature "using multiple independent node trees", with_multiple_node_classes: true, with_other_tree: true do
Expand Down

0 comments on commit c994c38

Please sign in to comment.