Skip to content
2 changes: 1 addition & 1 deletion spec/jobs/get_usps_proofing_results_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,8 @@
'GetUspsProofingResultsJob: Unexpected response received',
hash_including(
reason: 'Unexpected number of days before enrollment expired',
job_name: 'GetUspsProofingResultsJob',
),
job_name: 'GetUspsProofingResultsJob',
)
end
end
Expand Down
60 changes: 0 additions & 60 deletions spec/support/fake_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,63 +180,3 @@ def browser_attributes
FakeAnalytics::UndocumentedParamsChecker.allowed_extra_analytics = []
end
end

RSpec::Matchers.define :have_logged_event do |event, attributes_matcher|
match do |actual|
if event.nil? && attributes_matcher.nil?
expect(actual.events.count).to be > 0
elsif attributes_matcher.nil?
expect(actual.events).to have_key(event)
else
expect(actual.events[event]).to include(match(attributes_matcher))
end
end

failure_message do |actual|
matching_events = actual.events[event]
if matching_events&.length == 1 && attributes_matcher.instance_of?(Hash)
# We found one matching event. Let's show the user a diff of the actual and expected
# attributes
expected = attributes_matcher
actual = matching_events.first
message = "Expected that FakeAnalytics would have received matching event #{event}\n"
message += "expected: #{expected}\n"
message += " got: #{actual}\n\n"
message += "Diff:#{differ.diff(actual, expected)}"
message
elsif matching_events&.length == 1 &&
attributes_matcher.instance_of?(RSpec::Matchers::BuiltIn::Include)
# We found one matching event and an `include` matcher. Let's show the user a diff of the
# actual and expected attributes
expected = attributes_matcher.expecteds.first
actual_attrs = matching_events.first
actual_compared = actual_attrs.slice(*expected.keys)
actual_ignored = actual_attrs.except(*expected.keys)
message = "Expected that FakeAnalytics would have received matching event #{event}"
message += "expected: include #{expected}\n"
message += " got: #{actual_attrs}\n\n"
message += "Diff:#{differ.diff(actual_compared, expected)}\n"
message += "Attributes ignored by the include matcher:#{differ.diff(
actual_ignored, {}
)}"
message
else
<<~MESSAGE
Expected that FakeAnalytics would have received event #{event.inspect}
with #{attributes_matcher.inspect}.

Events received:
#{actual.events.pretty_inspect}
MESSAGE
end
end

def differ
RSpec::Support::Differ.new(
object_preparer: lambda do |object|
RSpec::Matchers::Composable.surface_descriptions_in(object)
end,
color: RSpec::Matchers.configuration.color?,
)
end
end
207 changes: 186 additions & 21 deletions spec/support/fake_analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event nil
with nil.

Events received:
{}
Expand Down Expand Up @@ -45,7 +44,6 @@
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event
with nil.

Events received:
{}
Expand All @@ -60,7 +58,6 @@
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event
with nil.

Events received:
{:my_other_event=>[{}]}
Expand All @@ -81,6 +78,43 @@
expect(&code_under_test).
not_to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

describe '.once' do
let(:code_under_test) { -> { expect(analytics).to have_logged_event(:my_event).once } }

it 'raises if no event has been logged' do
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event once but it was received 0 times

Events received:
{}
MESSAGE
end
end

it 'does not raise if event was logged 1x' do
track_event.call
expect(&code_under_test).
not_to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

it 'raises if event was logged 2x' do
track_event.call
track_event.call

expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event once but it was received twice

Events received:
{:my_event=>[{}, {}]}
MESSAGE
end
end
end
end

context 'event name + hash' do
Expand All @@ -94,7 +128,7 @@
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event
with {:arg1=>42}.
with {:arg1=>42}

Events received:
{}
Expand All @@ -109,7 +143,7 @@
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event
with {:arg1=>42}.
with {:arg1=>42}

Events received:
{:my_other_event=>[{}]}
Expand All @@ -123,7 +157,7 @@
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received matching event my_event
Expected that FakeAnalytics would have received event :my_event
expected: {:arg1=>42}
got: {:arg1=>43}

Expand Down Expand Up @@ -162,6 +196,49 @@
expect(&code_under_test).
not_to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

describe '.once' do
let(:code_under_test) do
-> {
expect(analytics).to have_logged_event(:my_event, arg1: 42).once
}
end

it 'raises if no event has been logged' do
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event once but it was received 0 times
with {:arg1=>42}

Events received:
{}
MESSAGE
end
end

it 'does not raise if event was logged 1x' do
track_event.call
expect(&code_under_test).
not_to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

it 'raises if event was logged 2x' do
track_event.call
track_event.call

expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event once but it was received twice
with {:arg1=>42}

Events received:
{:my_event=>[{:arg1=>42}, {:arg1=>42}]}
MESSAGE
end
end
end
end

context 'event name + include() matcher' do
Expand All @@ -183,8 +260,8 @@
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event
with #<RSpec::Matchers::BuiltIn::Include:<id> @expecteds=[{:arg1=>42}]>.
Expected that FakeAnalytics would have received matching event :my_event
with include(arg1: 42)

Events received:
{}
Expand All @@ -198,8 +275,8 @@
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event
with #<RSpec::Matchers::BuiltIn::Include:<id> @expecteds=[{:arg1=>42}]>.
Expected that FakeAnalytics would have received matching event :my_event
with include(arg1: 42)

Events received:
{:my_other_event=>[{}]}
Expand All @@ -213,15 +290,14 @@
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received matching event my_eventexpected: include {:arg1=>42}
Expected that FakeAnalytics would have received matching event :my_event
expected: include(arg1: 42)
got: {:arg1=>43}

Diff:
@@ -1 +1 @@
-:arg1 => 42,
+:arg1 => 43,

Attributes ignored by the include matcher:
MESSAGE
end
end
Expand Down Expand Up @@ -253,6 +329,49 @@
expect(&code_under_test).
not_to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

describe '.once' do
let(:code_under_test) do
-> {
expect(analytics).to have_logged_event(:my_event, include(arg1: 42)).once
}
end

it 'raises if no event has been logged' do
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received matching event :my_event once but it was received 0 times
with include(arg1: 42)

Events received:
{}
MESSAGE
end
end

it 'does not raise if event was logged 1x' do
track_event.call
expect(&code_under_test).
not_to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

it 'raises if event was logged 2x' do
track_event.call
track_event.call

expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received matching event :my_event once but it was received twice
with include(arg1: 42)

Events received:
{:my_event=>[{:arg1=>42}, {:arg1=>42}]}
MESSAGE
end
end
end
end

context 'event name + hash_including() matcher' do
Expand All @@ -274,8 +393,8 @@
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event
with #<RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher:<id> @expected={:arg1=>42}>.
Expected that FakeAnalytics would have received matching event :my_event
with hash_including(arg1: 42)

Events received:
{}
Expand All @@ -289,8 +408,8 @@
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event
with #<RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher:<id> @expected={:arg1=>42}>.
Expected that FakeAnalytics would have received matching event :my_event
with hash_including(arg1: 42)

Events received:
{:my_other_event=>[{}]}
Expand All @@ -304,11 +423,14 @@
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received event :my_event
with #<RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher:<id> @expected={:arg1=>42}>.
Expected that FakeAnalytics would have received matching event :my_event
expected: hash_including(arg1: 42)
got: {:arg1=>43}

Events received:
{:my_event=>[{:arg1=>43}]}
Diff:
@@ -1 +1 @@
-:arg1 => 42,
+:arg1 => 43,
MESSAGE
end
end
Expand Down Expand Up @@ -340,6 +462,49 @@
expect(&code_under_test).
not_to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

describe '.once' do
let(:code_under_test) do
-> {
expect(analytics).to have_logged_event(:my_event, hash_including(arg1: 42)).once
}
end

it 'raises if no event has been logged' do
expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received matching event :my_event once but it was received 0 times
with hash_including(arg1: 42)

Events received:
{}
MESSAGE
end
end

it 'does not raise if event was logged 1x' do
track_event.call
expect(&code_under_test).
not_to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

it 'raises if event was logged 2x' do
track_event.call
track_event.call

expect(&code_under_test).
to raise_error(RSpec::Expectations::ExpectationNotMetError) do |err|
assert_error_messages_equal(err, <<~MESSAGE)
Expected that FakeAnalytics would have received matching event :my_event once but it was received twice
with hash_including(arg1: 42)

Events received:
{:my_event=>[{:arg1=>42}, {:arg1=>42}]}
MESSAGE
end
end
end
end
end

Expand Down
Loading