-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,110 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe_current do | ||
subject(:contract_class) do | ||
Class.new(Karafka::Core::Contractable::Contract) do | ||
configure do |config| | ||
config.error_messages = YAML.safe_load( | ||
File.read( | ||
File.join(Karafka::Core.gem_root, 'config', 'locales', 'errors.yml') | ||
) | ||
).fetch('en').fetch('validations').fetch('test') | ||
end | ||
|
||
required(:id) { |id| id.is_a?(String) } | ||
|
||
optional(:test) { |test| test == 5 } | ||
end | ||
end | ||
|
||
let(:contract) { contract_class.new } | ||
|
||
context 'when there are no errors' do | ||
pending | ||
let(:data) { { id: '123', test: 5 } } | ||
let(:result) { contract.call(data) } | ||
|
||
it 'returns success' do | ||
expect(result.success?).to be true | ||
end | ||
|
||
it 'has no errors' do | ||
expect(result.errors).to be_empty | ||
end | ||
end | ||
|
||
context 'when there are errors and all keys can be mapped to messages' do | ||
pending | ||
let(:data) { { id: 123, test: 4 } } | ||
let(:result) { contract.call(data) } | ||
|
||
it 'does not return success' do | ||
expect(result.success?).to be false | ||
end | ||
|
||
it 'maps errors to messages' do | ||
expect(result.errors).to eq( | ||
id: 'needs to be a String', | ||
test: 'needs to be 5' | ||
) | ||
end | ||
end | ||
|
||
context 'when there are errors with nested keys' do | ||
pending | ||
subject(:contract_class) do | ||
Class.new(Karafka::Core::Contractable::Contract) do | ||
configure do |config| | ||
config.error_messages = YAML.safe_load( | ||
File.read( | ||
File.join(Karafka::Core.gem_root, 'config', 'locales', 'errors.yml') | ||
) | ||
).fetch('en').fetch('validations').fetch('test') | ||
end | ||
|
||
nested(:details) do | ||
required(:id) { |id| id.is_a?(String) } | ||
end | ||
end | ||
end | ||
|
||
let(:data) { { details: { id: 123 } } } | ||
let(:result) { contract.call(data) } | ||
|
||
it 'does not return success' do | ||
expect(result.success?).to be false | ||
end | ||
|
||
it 'maps nested errors to messages with dot notation' do | ||
expect(result.errors).to eq( | ||
'details.id': 'needs to be a String' | ||
) | ||
end | ||
end | ||
|
||
context 'when there are errors and key cannot be mapped to a message' do | ||
pending | ||
subject(:contract_class) do | ||
Class.new(Karafka::Core::Contractable::Contract) do | ||
configure do |config| | ||
config.error_messages = YAML.safe_load( | ||
File.read( | ||
File.join(Karafka::Core.gem_root, 'config', 'locales', 'errors.yml') | ||
) | ||
).fetch('en').fetch('validations').fetch('test') | ||
end | ||
|
||
required(:id) { |id| id.is_a?(String) } | ||
end | ||
end | ||
|
||
let(:data) { { id: 123 } } | ||
let(:result) { contract.call(data) } | ||
|
||
it 'does not return success' do | ||
expect(result.success?).to be false | ||
end | ||
|
||
it 'falls back to a generic error message' do | ||
expect(result.errors).to eq( | ||
id: 'needs to be a String' | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters