-
-
Notifications
You must be signed in to change notification settings - Fork 186
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
3 changed files
with
294 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry/validation/extensions/composable' | ||
|
||
RSpec.describe Dry::Validation::Composable::ResultSet do | ||
subject(:result_set) { Dry::Validation::Composable::ResultSet.new(results) } | ||
|
||
let(:results) { [] } | ||
|
||
let(:email_schema) do | ||
Dry::Schema.define { required(:email).value(:string, format?: /@/) } | ||
end | ||
|
||
let(:name_schema) do | ||
Dry::Schema.define { required(:name).filled(:string) } | ||
end | ||
|
||
context 'with no results' do | ||
it { is_expected.to be_success } | ||
|
||
it '#to_h returns an empty hash' do | ||
expect(result_set.to_h).to eq({}) | ||
end | ||
end | ||
|
||
context 'with 2 successful results' do | ||
let(:results) do | ||
[email_schema[email: 'foo@bar'], name_schema[name: 'Fred']] | ||
end | ||
|
||
it { is_expected.to be_success } | ||
|
||
it '#to_h has the values from both results' do | ||
expect(result_set.to_h).to eq(email: 'foo@bar', name: 'Fred') | ||
end | ||
end | ||
|
||
context '1 successful result, 1 failure result' do | ||
let(:results) do | ||
[email_schema[email: 'nope'], name_schema[name: 'Fred']] | ||
end | ||
|
||
it { is_expected.to be_failure } | ||
|
||
it '#to_h has the values from both results' do | ||
expect(result_set.to_h).to eq(email: 'nope', name: 'Fred') | ||
end | ||
|
||
it 'has the errors from the failure' do | ||
expect(result_set.errors(full: true).map(&:to_s)) | ||
.to eq ['email is in invalid format'] | ||
|
||
expect(result_set.errors.to_h).to eq(email: ['is in invalid format']) | ||
end | ||
end | ||
|
||
context '2 failure results' do | ||
let(:results) do | ||
[email_schema[email: 'nope'], name_schema[name: '']] | ||
end | ||
|
||
it { is_expected.to be_failure } | ||
|
||
it '#to_h has the values from both results' do | ||
expect(result_set.to_h).to eq(email: 'nope', name: '') | ||
end | ||
|
||
it 'has the errors from the failures' do | ||
expect(result_set.errors(full: true).map(&:to_s)) | ||
.to eq ['email is in invalid format', 'name must be filled'] | ||
|
||
expect(result_set.errors.to_h).to eq(email: ['is in invalid format'], | ||
name: ['must be filled']) | ||
end | ||
end | ||
|
||
context 'multiple failure results for the same key' do | ||
let(:smudge_schema) do | ||
Dry::Schema.define { required(:name).value(:string, eql?: 'smudge') } | ||
end | ||
|
||
let(:results) do | ||
[name_schema[name: ''], smudge_schema[name: '']] | ||
end | ||
|
||
it 'concatenates the errors at the key' do | ||
expect(result_set.errors.to_h).to eq(name: ['must be filled', 'must be equal to smudge']) | ||
end | ||
end | ||
|
||
describe '#add_result' do | ||
context 'results at different paths' do | ||
before do | ||
result_set.add_result(email_schema[email: 'paino'], :keeper) | ||
result_set.add_result(email_schema[email: 'smudger'], :captain) | ||
result_set.add_result(email_schema[email: 'gary'], 'leg.fine') | ||
result_set.add_result(email_schema[email: 'cummins'], [:leg, :square, :deep]) | ||
end | ||
|
||
it '#to_h has values at the specified paths' do | ||
expect(result_set.to_h) | ||
.to eq(captain: { email: "smudger"}, | ||
keeper: { email: "paino"}, | ||
leg: { fine: { email: 'gary' }, | ||
square: { deep: { email: "cummins"} } }) | ||
end | ||
|
||
it '#errors has messages at the specified paths' do | ||
expect(result_set.errors.to_h) | ||
.to eq(captain: { email: ['is in invalid format']}, | ||
keeper: { email: ['is in invalid format']}, | ||
leg: { fine: { email: ['is in invalid format'] }, | ||
square: { deep: { email: ['is in invalid format'] } } }) | ||
end | ||
end | ||
end | ||
end |