|
34 | 34 | module Wikis |
35 | 35 | RSpec.describe PageLinkMetadataService do |
36 | 36 | let(:relation) { PageLink.limit(30) } |
| 37 | + let(:query_double) { instance_double(Adapters::Providers::Internal::Queries::PageInfo) } |
| 38 | + let(:query_class_double) { class_double(Adapters::Providers::Internal::Queries::PageInfo) } |
37 | 39 |
|
38 | 40 | shared_let(:provider) { create(:internal_wiki_provider) } |
39 | 41 | shared_let(:page_links) { create_list(:relation_wiki_page_link, 3, provider:) } |
40 | 42 |
|
41 | 43 | subject(:service) { described_class.new(relation) } |
42 | 44 |
|
43 | 45 | before do |
44 | | - query_double = instance_double(Adapters::Providers::Internal::Queries::PageInfo) |
45 | | - query_class_double = class_double(Adapters::Providers::Internal::Queries::PageInfo, new: query_double) |
| 46 | + allow(query_class_double).to receive(:new).with(model: provider).and_return(query_double) |
46 | 47 | Adapters::Registry.stub("internal.queries.page_info", query_class_double) |
47 | 48 |
|
48 | 49 | build_inputs.each do |input| |
@@ -73,10 +74,52 @@ module Wikis |
73 | 74 | expect(page_links.first.title).to eq("Wikis, now with more cheese! Part #{page_links.first.identifier}") |
74 | 75 | end |
75 | 76 |
|
| 77 | + context "when page links have the same identifier but different providers" do |
| 78 | + shared_let(:xwiki_provider) { create(:xwiki_provider) } |
| 79 | + let(:new_page_links) do |
| 80 | + page_links.map do |pl| |
| 81 | + create(:relation_wiki_page_link, provider: xwiki_provider, identifier: pl) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + before do |
| 86 | + allow(query_class_double).to receive(:new).with(model: xwiki_provider).and_return(query_double) |
| 87 | + Adapters::Registry.stub("xwiki.queries.page_info", query_class_double) |
| 88 | + |
| 89 | + new_page_links.map do |pl| |
| 90 | + input = Adapters::Input::PageInfo.build(identifier: pl.identifier).value_or(nil) |
| 91 | + allow(query_double).to receive(:call).with(input).and_return( |
| 92 | + Success( |
| 93 | + Adapters::Results::PageInfo.new(title: "Wikis, now with more cheese! Part #{pl.id}", |
| 94 | + identifier: input.identifier, |
| 95 | + href: "totally_valid_url", |
| 96 | + provider: pl.provider) |
| 97 | + ) |
| 98 | + ) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + it "maps the titles to the correct page link" do |
| 103 | + service_result = service.call |
| 104 | + |
| 105 | + expect(service_result).to be_success |
| 106 | + relation = service_result.result |
| 107 | + |
| 108 | + relation.find_each do |page_link| |
| 109 | + case page_link.provider_id |
| 110 | + when xwiki_provider.id |
| 111 | + expect(page_link.title).to eq("Wikis, now with more cheese! Part #{page_link.id}") |
| 112 | + else |
| 113 | + expect(page_link.title).to eq("Wikis, now with more cheese! Part #{page_link.identifier}") |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + |
76 | 119 | private |
77 | 120 |
|
78 | | - def build_inputs |
79 | | - page_links.filter_map { Adapters::Input::PageInfo.build(identifier: it.identifier).value_or(nil) } |
| 121 | + def build_inputs(relation = page_links) |
| 122 | + relation.filter_map { Adapters::Input::PageInfo.build(identifier: it.identifier).value_or(nil) } |
80 | 123 | end |
81 | 124 | end |
82 | 125 | end |
0 commit comments