require "spec_helper" def mods_display_item(mods_record) ModsDisplay::RelatedItem.new(mods_record, ModsDisplay::Configuration::Base.new, mock("controller")) end describe ModsDisplay::RelatedItem do before(:all) do @item = Stanford::Mods::Record.new.from_str("A Related Item", false).related_item @linked_item = Stanford::Mods::Record.new.from_str("A Related Itemhttp://library.stanford.edu/", false).related_item @collection = Stanford::Mods::Record.new.from_str("This is a Collection", false).related_item @display_label = Stanford::Mods::Record.new.from_str("A Related Item", false).related_item @location = Stanford::Mods::Record.new.from_str("The Location", false).related_item @reference = Stanford::Mods::Record.new.from_str("The title124DATE", false).related_item @blank_item = Stanford::Mods::Record.new.from_str("", false).related_item @multi_items = Stanford::Mods::Record.new.from_str("Libraryhttp://library.stanford.eduSDRhttp://purl.stanford.edu", false).related_item end describe "label" do it "should default to Related Item" do mods_display_item(@item).fields.first.label.should == "Related item" end it "should get the location label" do mods_display_item(@location).fields.first.label.should == "Location" end it "should get the reference label" do mods_display_item(@reference).fields.first.label.should == "Referenced by" end it "should get the displayLabel if available" do mods_display_item(@display_label).fields.first.label.should == "Special Item" end end describe "fields" do it "should get a location if it is available" do fields = mods_display_item(@item).fields fields.length.should == 1 fields.first.values.should == ["A Related Item"] end it "should return a link if there is a location/url present" do fields = mods_display_item(@linked_item).fields fields.length.should == 1 fields.first.values.should == ["A Related Item"] end it "should not return any fields if the described related item is a collection" do mods_display_item(@collection).fields.should == [] end it "should not return empty links when there is no title or link" do mods_display_item(@blank_item).fields.should == [] end it "should concat the isReferencedBy related item title with other metadata" do fields = mods_display_item(@reference).fields fields.length.should == 1 fields.first.values.should == ["The title DATE 124"] end it "should collapse labels down into the same record" do fields = mods_display_item(@multi_items).fields fields.length.should == 1 fields.first.label.should == "Related item" fields.first.values.length.should == 2 fields.first.values.first.should =~ /Library<\/a>/ or fields.first.values.last.should =~ /SDR<\/a>/ end end end