describe "d3 - selection" do after(:each) do D3.select("#test-area").html("") end it "d3.selection" do s = D3.selection expect(s).to be_instance_of(D3::Selection) expect(s.size).to eq(1) end it "d3.select" do s = D3.select("div") expect(s).to be_instance_of(D3::Selection) expect(s.size).to eq(1) expect(s.empty?).to eq(false) s = D3.select("h6") expect(s).to be_instance_of(D3::Selection) expect(s.size).to eq(0) expect(s.empty?).to eq(true) end it "d3.select_all" do s = D3.select_all("div") expect(s).to be_instance_of(D3::Selection) expect(s.size).to eq(1) expect(s.empty?).to eq(false) s = D3.select_all("h6") expect(s).to be_instance_of(D3::Selection) expect(s.size).to eq(0) expect(s.empty?).to eq(true) end describe "nested selections" do before(:each) do D3.select("div").html("
123
456
") end it "selection.select" do expect(D3.select("p").select("b").size).to eq(1) expect(D3.select_all("p").select("b").size).to eq(2) end it "selection.select_all" do expect(D3.select("p").select_all("b").size).to eq(3) expect(D3.select_all("p").select_all("b").size).to eq(6) end end describe "selection.filter - selector string" do before(:each) do D3.select("div").html(" 1 2 3 ") end let(:a) { D3.select_all("span.a") } let(:b) { D3.select_all("span.b") } let(:c) { D3.select_all("span.c") } let(:d) { D3.select_all("span.d") } it do expect(b.filter(".c").size).to eq(1) end end describe "selection.filter - filter" do before(:each) do D3.select("div") .select_all("span") .data(%W[a b c d]) .enter .append("span") .text{|d| d} end it "function" do D3.select_all("span").filter{|d| d =~ /[bc]/}.attr("class", "x") expect(D3.select("div").html).to eq( %Q[abcd] ) end it "function with index" do D3.select_all("span").filter{|d,i| i.even?}.attr("class", "y") expect(D3.select("div").html).to eq( %Q[abcd] ) end end describe "selection.each" do before(:each) do D3.select("div") .select_all("span") .data(%W[a b c d]) .enter .append("span") .text{|d| d} end it do results = [] D3.select_all("span").each do |n| results << n end expect(results).to eq(["a", "b", "c", "d"]) end it do results = [] D3.select_all("span").each do |n,i| results << [n,i] end expect(results).to eq([["a", 0], ["b", 1], ["c", 2], ["d", 3]]) end end it "selection.append" do div = D3.select_all("div") ul = div.append("ul") ul.append("li") ul.append("li") expect(div.html).to eq("123
456
") end # These should use opal-browser, but that seems to be broken with phantomjs # For now just expose raw js objects it "selection.node" do expect(D3.select_all("b").node).to be_instance_of(Native::Object) end it "selection.nodes" do expect(D3.select_all("b").nodes).to be_instance_of(Array) D3.select_all("b").nodes.each do |n| expect(n).to be_instance_of(Native::Object) end end end end