Skip to content

Commit

Permalink
feat: display V3 provider states in HTML (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatguysimon authored Nov 3, 2020
1 parent 8290189 commit 8e06a7f
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/pact/doc/interaction_view_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class InteractionViewModel

include Pact::ActiveSupportSupport

MARKDOWN_BOLD_CHARACTERS = '**'

def initialize interaction, consumer_contract
@interaction = interaction
@consumer_contract = consumer_contract
Expand Down Expand Up @@ -52,6 +54,16 @@ def provider_state start_of_sentence = false
markdown_escape apply_capitals(@interaction.provider_state.strip, start_of_sentence)
end

def formatted_provider_states mark_bold: false
bold_marker = mark_bold ? MARKDOWN_BOLD_CHARACTERS : ''

(@interaction.provider_states || []).map do |ps|
"#{bold_marker}" \
"#{markdown_escape(apply_capitals(ps.name.strip, false))}" \
"#{bold_marker}"
end.join(' and ')
end

def description start_of_sentence = false
return '' unless @interaction.description
markdown_escape apply_capitals(@interaction.description.strip, start_of_sentence)
Expand Down
6 changes: 3 additions & 3 deletions lib/pact/doc/markdown/interaction.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<a name="<%= interaction.id %>"></a>
<%= if interaction.has_provider_state?
"Given **#{h(interaction.provider_state)}**, upon receiving"
<a name="<%= interaction.id %>"></a><% formatted_provider_states = h(interaction.formatted_provider_states mark_bold: true) %>
<%= if !formatted_provider_states.empty?
"Given #{formatted_provider_states}, upon receiving"
else
"Upon receiving"
end
Expand Down
3 changes: 2 additions & 1 deletion lib/pact/doc/markdown/interaction_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def initialize interaction, pact
end

def render_summary
suffix = interaction.has_provider_state? ? " given #{h(interaction.provider_state)}" : ""
formatted_provider_states = h(interaction.formatted_provider_states)
suffix = formatted_provider_states.empty? ? "" : " given #{formatted_provider_states}"
"* [#{h(interaction.description(true))}](##{interaction.id})#{suffix}\n\n"
end

Expand Down
46 changes: 46 additions & 0 deletions spec/lib/pact/doc/interaction_view_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,52 @@ module Doc
end
end
end

describe "formatted_provider_states" do
let(:consumer_contract) { Pact::ConsumerContract.from_uri './spec/support/markdown_pact.json' }
let(:interaction) { consumer_contract.interactions.first }

context "when no provider state" do
let(:interaction) { consumer_contract.interactions.last }

it "returns an empty string" do
expect(subject.formatted_provider_states).to eq ""
end
end

context "when marking provider states in bold" do
it "formats the provider state in bold" do
expect(subject.formatted_provider_states mark_bold: true).to eq "**alligators exist**"
end
end

context "when not marking provider states in bold" do
it "formats the provider state without bold" do
expect(subject.formatted_provider_states).to eq "alligators exist"
end
end

context "when using v3 specification" do
let(:consumer_contract) { Pact::ConsumerContract.from_uri './spec/support/markdown_pact_v3.json' }
let(:interaction) { consumer_contract.interactions.first }

context "when marking provider states in bold" do
it "formats the provider states in bold" do
expected_result = '**alligators exist** and **the city of Tel Aviv has a zoo** ' \
'and **the zoo keeps record of its alligator population**'
expect(subject.formatted_provider_states mark_bold: true).to eq expected_result
end
end

context "when not marking provider states in bold" do
it "formats the provider states without bold" do
expected_result = 'alligators exist and the city of Tel Aviv has a zoo ' \
'and the zoo keeps record of its alligator population'
expect(subject.formatted_provider_states).to eq expected_result
end
end
end
end
end
end
end
11 changes: 11 additions & 0 deletions spec/lib/pact/doc/markdown/consumer_contract_renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ module Markdown
subject { ConsumerContractRenderer.new(consumer_contract) }

describe "#call" do
context "when using V3 specification" do
context "when an interaction has multiple provider states" do
let(:consumer_contract) { Pact::ConsumerContract.from_uri './spec/support/markdown_pact_v3.json' }

it "displays all provider states in the interaction title" do
expect(subject.call).to include 'Given **alligators exist** and **the city of Tel Aviv has a zoo** ' \
'and **the zoo keeps record of its alligator population**, upon receiving'
end
end
end

context "with markdown characters in the pacticipant names" do
let(:consumer_contract) { Pact::ConsumerContract.from_uri './spec/support/markdown_pact_with_markdown_chars_in_names.json' }

Expand Down
44 changes: 44 additions & 0 deletions spec/support/markdown_pact_v3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"provider": {
"name": "Some Provider"
},
"consumer": {
"name": "Some Consumer"
},
"interactions": [
{
"description": "a request to list all alligators in Tel Aviv",
"providerStates": [
{"name": "alligators exist", "params" : {}},
{"name": "the city of Tel Aviv has a zoo", "params" : {}},
{"name": "the zoo keeps record of its alligator population", "params" : {}}
],
"params": {},
"request": {
"method": "get",
"path": "/alligators"
},
"response": {
"headers" : {"Content-Type": "application/json"},
"status" : 200,
"body" : {
"alligators": [{
"name": "Bob",
"phoneNumber" : {
"json_class": "Pact::Term",
"data": {
"generate": "12345678",
"matcher": {"json_class":"Regexp","o":0,"s":"\\d+"}
}
}
}]
}
}
}
],
"metadata": {
"pactSpecification": {
"version": "3.0.0"
}
}
}

0 comments on commit 8e06a7f

Please sign in to comment.