Module: Trailblazer::Test::Operation::Assertions

Included in:
Deprecation::Operation::Assertions, PolicyAssertions
Defined in:
lib/trailblazer/test/operation/assertions.rb

Defined Under Namespace

Classes: CtxHash, ExpectedErrorsTypeError

Instance Method Summary collapse

Instance Method Details

#_assert_call(operation_class, operation_inputs, user_block: raise) {|result| ... } ⇒ Object

Yields:

  • (result)


54
55
56
57
58
59
60
61
62
# File 'lib/trailblazer/test/operation/assertions.rb', line 54

def _assert_call(operation_class, operation_inputs, user_block: raise)
  result = _call_operation(operation_class, operation_inputs)

  return user_block.call(result) if user_block # DISCUSS: result or model?

  yield(result)

  result
end

#_call_operation(operation_class, operation_inputs) ⇒ Object



80
81
82
# File 'lib/trailblazer/test/operation/assertions.rb', line 80

def _call_operation(operation_class, operation_inputs)
  operation_class.(operation_inputs)
end

#_model(result) ⇒ Object



85
86
87
# File 'lib/trailblazer/test/operation/assertions.rb', line 85

def _model(result)
  result[:model]
end

#assert_fail(operation_class, operation_inputs, expected_errors: nil, contract_name: "default", &block) ⇒ Object



26
27
28
# File 'lib/trailblazer/test/operation/assertions.rb', line 26

def assert_fail(operation_class, operation_inputs, expected_errors: nil, contract_name: "default", &block)
  assert_fail_with_model(operation_class, operation_inputs, expected_errors: expected_errors, contract_name: contract_name, &block)
end

#assert_fail_with_model(operation_class, operation_inputs, expected_errors: nil, contract_name: raise, &user_block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/trailblazer/test/operation/assertions.rb', line 40

def assert_fail_with_model(operation_class, operation_inputs, expected_errors: nil, contract_name: raise, &user_block)
  _assert_call(operation_class, operation_inputs, user_block: user_block) do |result|
    assert_equal true, result.failure?

    raise ExpectedErrorsTypeError, "expected_errors has to be an Array" unless expected_errors.is_a?(Array)

    # only test _if_ errors are present, not the content.
    errors = result["contract.#{contract_name}"].errors.messages # TODO: this will soon change with the operation Errors object.

    assert_equal expected_errors.sort, errors.keys.sort
  end
end

#assert_pass(operation_class, operation_inputs, expected_attributes, default_attributes: expected_attrs, deep_merge: true, &block) ⇒ Object



20
21
22
23
24
# File 'lib/trailblazer/test/operation/assertions.rb', line 20

def assert_pass(operation_class, operation_inputs, expected_attributes, default_attributes: expected_attrs, deep_merge: true, &block)
  expected_attributes = merge_for(default_attributes, expected_attributes, deep_merge)

  assert_pass_with_model(operation_class, operation_inputs, expected_model_attributes: expected_attributes, &block)
end

#assert_pass_with_model(operation_class, operation_inputs, expected_model_attributes: {}, &user_block) ⇒ Object

TODO: test expected_attributes default param and explicit!



32
33
34
35
36
37
# File 'lib/trailblazer/test/operation/assertions.rb', line 32

def assert_pass_with_model(operation_class, operation_inputs, expected_model_attributes: {}, &user_block)
  _assert_call(operation_class, operation_inputs, user_block: user_block) do |result|
    assert_equal true, result.success?
    assert_exposes(_model(result), expected_model_attributes)
  end
end

#ctx(new_params, default_options: self.default_options, deep_merge: true, **options) ⇒ Object



13
14
15
16
17
18
# File 'lib/trailblazer/test/operation/assertions.rb', line 13

def ctx(new_params, default_options: self.default_options, deep_merge: true, **options)
  new_params  = merge_for(params[:params], new_params, deep_merge)
  new_options = merge_for(default_options, options, deep_merge)

  {params: new_params, **new_options}
end

#merge_for(dest, source, deep_merge) ⇒ Object



73
74
75
76
77
# File 'lib/trailblazer/test/operation/assertions.rb', line 73

def merge_for(dest, source, deep_merge)
  return dest.merge(source) unless deep_merge

  CtxHash[dest].deep_merge(CtxHash[source])
end

#params(default_params: self.default_params, deep_merge: true, **new_params) ⇒ Object



9
10
11
# File 'lib/trailblazer/test/operation/assertions.rb', line 9

def params(default_params: self.default_params, deep_merge: true, **new_params)
  {params: merge_for(default_params, new_params, deep_merge)}
end