|
2 | 2 |
|
3 | 3 | require 'rails_helper' |
4 | 4 |
|
| 5 | +class CollectionMock |
| 6 | + |
| 7 | + def initialize (id:, title:) |
| 8 | + @id = id |
| 9 | + @title = title |
| 10 | + end |
| 11 | + |
| 12 | + def id |
| 13 | + return @id |
| 14 | + end |
| 15 | + |
| 16 | + def title |
| 17 | + return @title |
| 18 | + end |
| 19 | +end |
| 20 | + |
| 21 | +class FormMock |
| 22 | + |
| 23 | + def reflect_on_association |
| 24 | + end |
| 25 | + |
| 26 | + def attributes |
| 27 | + end |
| 28 | +end |
| 29 | + |
5 | 30 | describe Hyrax::Forms::CollectionForm do |
6 | 31 |
|
7 | | - describe "#terms" do |
8 | | - subject { described_class.terms } |
| 32 | + let(:model) { Collection.new } |
| 33 | + let(:user) { create(:user) } |
| 34 | + let(:ability) { Ability.new(user) } |
| 35 | + let(:config) { Blacklight::Solr::Configuration.new } |
| 36 | + let(:repository) { Blacklight::Solr::Repository.new(:config) } |
| 37 | + let(:subject) { described_class.new(model, ability, repository) } |
9 | 38 |
|
10 | | - it { is_expected.to eq %i[ |
| 39 | + let( :expected_required_fields ) { %i[ |
| 40 | + title |
| 41 | + creator |
| 42 | + description |
| 43 | + subject_discipline |
| 44 | + ] } |
| 45 | + |
| 46 | + let( :expected_terms ) { %i[ |
11 | 47 | authoremail |
12 | 48 | based_near |
13 | 49 | collection_type_gid |
|
34 | 70 | thumbnail_id |
35 | 71 | title |
36 | 72 | visibility |
37 | | - ] } |
| 73 | + ] } |
| 74 | + |
| 75 | + let( :expected_default_work_primary_terms ) { %i[ |
| 76 | + title |
| 77 | + creator |
| 78 | + description |
| 79 | + keyword |
| 80 | + subject_discipline |
| 81 | + language |
| 82 | + referenced_by |
| 83 | + ] } |
| 84 | + |
| 85 | + describe "delegates methods to model:" do |
| 86 | + [:id, :depositor, :permissions, :human_readable_type, :member_ids, :nestable?].each do |
| 87 | + |method| |
| 88 | + it "#{method}" do |
| 89 | + expect(subject).to delegate_method(method).to(:model) |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + describe "delegates method to Hyrax::CollectionsController" do |
| 95 | + it "#blacklight_config" do |
| 96 | + skip "Add test here" |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + describe "#terms" do |
| 101 | + subject { described_class.terms } |
| 102 | + |
| 103 | + it "equals array" do |
| 104 | + is_expected.to eq expected_terms |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + describe "#required_fields" do |
| 109 | + subject { described_class.required_fields } |
| 110 | + |
| 111 | + it "equals array" do |
| 112 | + is_expected.to eq expected_required_fields |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + |
| 117 | + describe "#initialize" do |
| 118 | + it "sets @scope" do |
| 119 | + subject.instance_variable_get(:@scope) == instance_of(Struct) |
| 120 | + end |
| 121 | + |
| 122 | + it "calls super" do |
| 123 | + skip "Add test here" |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + |
| 128 | + describe "#permission_template" do |
| 129 | + |
| 130 | + context "when @permission_template is not set" do |
| 131 | + before { |
| 132 | + allow(subject).to receive(:model).and_return OpenStruct.new(id: 555) |
| 133 | + allow(Hyrax::PermissionTemplate).to receive(:find_or_create_by).with(source_id: 555).and_return "template model" |
| 134 | + allow(Hyrax::Forms::PermissionTemplateForm).to receive(:new).with("template model").and_return "permission template" |
| 135 | + } |
| 136 | + it "calls PermissionTemplate.find_or_create_by and PermissionTemplateForm.new" do |
| 137 | + expect(Hyrax::PermissionTemplate).to receive(:find_or_create_by).with(source_id: 555) |
| 138 | + expect(Hyrax::Forms::PermissionTemplateForm).to receive(:new).with("template model") |
| 139 | + expect(subject.permission_template).to eq "permission template" |
| 140 | + |
| 141 | + subject.instance_variable_get(:@permission_template) == "permission template" |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + context "when @permission_template is set" do |
| 146 | + before { |
| 147 | + subject.instance_variable_set(:@permission_template, "set template") |
| 148 | + } |
| 149 | + it "returns @permission_template" do |
| 150 | + expect(Hyrax::PermissionTemplate).not_to receive(:find_or_create_by).with(source_id: anything) |
| 151 | + expect(Hyrax::Forms::PermissionTemplateForm).not_to receive(:new) |
| 152 | + |
| 153 | + expect(subject.permission_template).to eq "set template" |
| 154 | + |
| 155 | + subject.instance_variable_get(:@permission_template) == "set template" |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + describe "#select_files" do |
| 161 | + before { |
| 162 | + allow(subject).to receive(:all_files_with_access).and_return [["apples", 3], ["bananas", 2], ["oranges", 1]] |
| 163 | + } |
| 164 | + |
| 165 | + it "calls all_files_with_access and creates a Hash from the result" do |
| 166 | + expect(subject).to receive(:all_files_with_access).and_return [["apples", 3], ["bananas", 2], ["oranges", 1]] |
| 167 | + expect(subject.select_files).to eq Hash[[["apples", 3], ["bananas", 2], ["oranges", 1]]] |
| 168 | + end |
| 169 | + end |
| 170 | + |
| 171 | + describe "#primary_terms" do |
| 172 | + it "equals array" do |
| 173 | + expect(subject.primary_terms).to eq expected_default_work_primary_terms |
| 174 | + end |
| 175 | + end |
| 176 | + |
| 177 | + describe "#secondary_terms" do |
| 178 | + it "returns empty array" do |
| 179 | + expect(subject.secondary_terms).to be_blank |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + describe "#relative_url_root" do |
| 184 | + context "when DeepBlueDocs::Application.config.relative_url_root has value" do |
| 185 | + before { |
| 186 | + allow(::DeepBlueDocs::Application.config).to receive(:relative_url_root).and_return "rootin' tootin'" |
| 187 | + } |
| 188 | + it "returns DeepBlueDocs::Application.config.relative_url_root" do |
| 189 | + expect(subject.relative_url_root).to eq "rootin' tootin'" |
| 190 | + end |
| 191 | + end |
| 192 | + |
| 193 | + context "when DeepBlueDocs::Application.config.relative_url_root has no value" do |
| 194 | + before { |
| 195 | + allow(::DeepBlueDocs::Application.config).to receive(:relative_url_root).and_return nil |
| 196 | + } |
| 197 | + it "returns empty string" do |
| 198 | + expect(subject.relative_url_root).to eq "" |
| 199 | + end |
| 200 | + end |
| 201 | + end |
| 202 | + |
| 203 | + describe "#banner_info" do |
| 204 | + |
| 205 | + context "when @banner_info is not set" do |
| 206 | + before { |
| 207 | + allow(subject).to receive(:id).and_return 202 |
| 208 | + allow(subject).to receive(:branding_banner_info).with(id: 202).and_return "branding" |
| 209 | + } |
| 210 | + it "calls branding_banner_info" do |
| 211 | + expect(subject).to receive(:branding_banner_info).with(id: 202) |
| 212 | + expect(subject.banner_info).to eq "branding" |
| 213 | + |
| 214 | + subject.instance_variable_get(:@banner_info) == "branding" |
| 215 | + end |
| 216 | + end |
| 217 | + |
| 218 | + context "when @banner_info is set" do |
| 219 | + before { |
| 220 | + allow(subject).to receive(:id).and_return 202 |
| 221 | + subject.instance_variable_set(:@banner_info, "banner information") |
| 222 | + } |
| 223 | + it "returns @banner_info" do |
| 224 | + expect(subject).not_to receive(:branding_banner_info) |
| 225 | + expect(subject.banner_info).to eq "banner information" |
| 226 | + end |
| 227 | + end |
| 228 | + end |
| 229 | + |
| 230 | + describe "#logo_info" do |
| 231 | + context "when @logo_info is not set" do |
| 232 | + before { |
| 233 | + allow(subject).to receive(:id).and_return 303 |
| 234 | + allow(subject).to receive(:branding_logo_info).with(id: 303).and_return "logo" |
| 235 | + } |
| 236 | + it "calls branding_logo_info" do |
| 237 | + expect(subject).to receive(:branding_logo_info).with(id: 303) |
| 238 | + expect(subject.logo_info).to eq "logo" |
| 239 | + |
| 240 | + subject.instance_variable_get(:@logo_info) == "logo" |
| 241 | + end |
| 242 | + end |
| 243 | + |
| 244 | + context "when @logo_info is set" do |
| 245 | + before { |
| 246 | + allow(subject).to receive(:id).and_return 303 |
| 247 | + subject.instance_variable_set(:@logo_info, "information") |
| 248 | + } |
| 249 | + |
| 250 | + it "returns @logo_info" do |
| 251 | + expect(subject).not_to receive(:branding_logo_info) |
| 252 | + expect(subject.logo_info).to eq "information" |
| 253 | + end |
| 254 | + end |
| 255 | + end |
| 256 | + |
| 257 | + describe "#display_additional_fields?" do |
| 258 | + context "secondary terms present" do |
| 259 | + before { |
| 260 | + allow(subject).to receive(:secondary_terms).and_return ["title"] |
| 261 | + } |
| 262 | + it "returns true" do |
| 263 | + expect(subject.display_additional_fields?).to eq true |
| 264 | + end |
| 265 | + end |
| 266 | + |
| 267 | + context "secondary terms not present" do |
| 268 | + before { |
| 269 | + allow(subject).to receive(:secondary_terms).and_return [] |
| 270 | + } |
| 271 | + it "returns false" do |
| 272 | + expect(subject.display_additional_fields?).to eq false |
| 273 | + end |
| 274 | + end |
| 275 | + end |
| 276 | + |
| 277 | + describe "#thumbnail_title" do |
| 278 | + context "when model.thumbnail is nil" do |
| 279 | + before { |
| 280 | + allow(model).to receive(:thumbnail).and_return( nil ) |
| 281 | + } |
| 282 | + it "returns nil" do |
| 283 | + expect(subject.thumbnail_title).to be_blank |
| 284 | + end |
| 285 | + end |
| 286 | + |
| 287 | + context "when model.thumbnail has value" do |
| 288 | + before { |
| 289 | + allow(model).to receive(:thumbnail).and_return OpenStruct.new( title: ["rutabaga", "cauliflower", "tomato"] ) |
| 290 | + } |
| 291 | + it "returns model.thumbnail.title.first" do |
| 292 | + expect(subject.thumbnail_title).to eq "rutabaga" |
| 293 | + end |
| 294 | + end |
| 295 | + end |
| 296 | + |
| 297 | + describe "#list_parent_collections" do |
| 298 | + before { |
| 299 | + allow(model).to receive(:member_of_collections).and_return(["sunflower", "bluebell", "petunia"]) |
| 300 | + } |
| 301 | + it "calls collection.member_of_collections" do |
| 302 | + expect(subject.list_parent_collections).to eq ["sunflower", "bluebell", "petunia"] |
| 303 | + end |
| 304 | + end |
| 305 | + |
| 306 | + describe "#list_child_collections" do |
| 307 | + before { |
| 308 | + allow(subject.membership_service_class).to receive(:new) |
| 309 | + .with(scope: anything, collection: model, params: anything) |
| 310 | + .and_return OpenStruct.new( available_member_subcollections: OpenStruct.new(documents: ["vanilla", "saffron", "nutmeg"]) ) |
| 311 | + } |
| 312 | + |
| 313 | + it "returns documents from collection_member_service.available_member_subcollections" do |
| 314 | + expect(subject.list_child_collections).to eq ["vanilla", "saffron", "nutmeg"] |
| 315 | + end |
| 316 | + end |
| 317 | + |
| 318 | + describe "#available_parent_collections" do |
| 319 | + context "available_parents is present" do |
| 320 | + before { |
| 321 | + subject.instance_variable_set(:@available_parents, "available parents") |
| 322 | + } |
| 323 | + it "returns @available_parents" do |
| 324 | + expect(subject.available_parent_collections scope: "scope").to eq "available parents" |
| 325 | + end |
| 326 | + end |
| 327 | + |
| 328 | + context "available_parents is not present" do |
| 329 | + before { |
| 330 | + allow(subject).to receive(:id).and_return "ZZ101" |
| 331 | + collectionObj = CollectionMock.new id: "ZZ101", title: "Entitled Collection" |
| 332 | + allow(Collection).to receive(:find).and_return(collectionObj) |
| 333 | + allow(Hyrax::Collections::NestedCollectionQueryService).to receive(:available_parent_collections) |
| 334 | + .with(child: collectionObj, scope: "scope", limit_to_id: nil) |
| 335 | + .and_return [(CollectionMock.new id: "XX202", title: ["Bingo"]), (CollectionMock.new id: "YY303", title: ["Yahtzee"])] |
| 336 | + } |
| 337 | + |
| 338 | + it "calls NestedCollectionQueryService.available_parent_collections" do |
| 339 | + expect(subject.available_parent_collections scope: "scope") |
| 340 | + .to eq "[{\"id\":\"XX202\",\"title_first\":\"Bingo\"},{\"id\":\"YY303\",\"title_first\":\"Yahtzee\"}]" |
| 341 | + subject.instance_variable_get(:@available_parents) == [{id:"XX202",title_first:"Bingo"},{id:"YY303","title_first":"Yahtzee"}] |
| 342 | + end |
| 343 | + end |
38 | 344 | end |
39 | 345 |
|
40 | 346 | end |
0 commit comments