cable-ready-testing 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b752cd35388baa27b97647d9620422a89aa96ef8785617f2b3e33e5d0cd40c94
4
- data.tar.gz: 86654e8f6cc38912d7d2b9d6153c55cf45c321a2512902af981986ee14028eb2
3
+ metadata.gz: a02948deb7ad8a23d9cfd305f1828a3761b2610f20dcffe399b2b298a0a40d38
4
+ data.tar.gz: 78cfe150e631d5813da6443e49745dd094de05588310200d3c62d53e2da35634
5
5
  SHA512:
6
- metadata.gz: 93786abac4782aa9bd4b9ce5b1920a18b60eedddbddc97cb32ad304d72c9971d9211cd057323f7c743c1370b914dedc8b31c43b6211bb4be302292ace3ed53ca
7
- data.tar.gz: 056b2f6bb38f0204354ffaef64d46d1d73440a6ea14b76ca6e70e0c4bd49026f541d4664215648a1e62b09bdad4bbbe54f07c6c1207645d9a41304f9482e981d
6
+ metadata.gz: 8c1da3ea6327f12a27d724b5487c0e861cfe5761a4d66ea86f7e277750ec18d699bf4f02202fae3063fd089c7653ea2db9d8cd37cbdcf62ead5f0c4d5231c9db
7
+ data.tar.gz: 8048d3554eb6f2f00001daf251948375c066fe0f5ed8b7894f8b75631ebd764705fcf3a056e65c80edbe152c2d8b822c0dfd05fa3950ff3c9ff593648d626c3a
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cable_ready/testing'
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cable_ready/testing/version'
4
+
5
+ require 'cable_ready'
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cable-ready-testing'
4
+ require 'rspec/rails'
5
+ require 'rspec/rails/matchers/cable_ready'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CableReady
4
+ module Testing
5
+ VERSION = '0.0.2'
6
+ end
7
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/rails/matchers/cable_ready/mutated_element'
4
+
5
+ module RSpec
6
+ module Rails
7
+ module Matchers
8
+ # Namespace for various implementations of CableReady features
9
+ #
10
+ # @api private
11
+ module CableReady
12
+ end
13
+
14
+ # @api public
15
+ # Passes if a message has been sent to a stream/object inside a block.
16
+ # To specify channel from which message has been broadcasted to object use `on_channel`.
17
+ #
18
+ #
19
+ # @example
20
+ # expect {
21
+ # cable_ready['channel'].outer_html(
22
+ # selector: '#content',
23
+ # html: 'some html'
24
+ # )
25
+ #
26
+ # cable_ready.broadcast
27
+ # }.to mutated_element('#content')
28
+ # .on_channel('channel')
29
+ # .with(:outer_html, { 'html' => 'some html' })
30
+ #
31
+ #
32
+
33
+ def mutated_element(target = nil)
34
+ CableReady::MutatedElement.new(target)
35
+ end
36
+
37
+ %i[mutated_attribute mutated_css_class mutated_dataset mutated_style].each do |alt_method|
38
+ alias_method alt_method, :mutated_element
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ module Rails
5
+ module Matchers
6
+ module CableReady
7
+ # @private
8
+ class MutatedElement < RSpec::Matchers::BuiltIn::BaseMatcher
9
+ def initialize(target)
10
+ @target = target
11
+ @channel_not_found = false
12
+ @mutation_not_found = false
13
+ @element_not_found = false
14
+ end
15
+
16
+ def with(action, data = {}, &block)
17
+ @data = data
18
+ @action = action.to_s.camelize(:lower)
19
+ @data = @data.with_indifferent_access if @data.is_a?(Hash)
20
+ @data = @data.transform_keys { |key| key.camelize(:lower) }
21
+ @block = block if block_given?
22
+ self
23
+ end
24
+
25
+ def on_channel(channel_name)
26
+ @channel_name = channel_name
27
+ self
28
+ end
29
+
30
+ def matches?(proc)
31
+ verify_channel_name_presence
32
+ verify_action_presence
33
+
34
+ proc.call
35
+
36
+ if (options = mutation_options)
37
+ @message_data = mutated_element_options(options)
38
+
39
+ if @message_data.present?
40
+ @message_data.except('selector') == @data
41
+ else
42
+ @element_not_found = true
43
+ false
44
+ end
45
+ elsif broadcasted_message.blank?
46
+ @channel_not_found = true
47
+ false
48
+ else
49
+ @mutation_not_found = true
50
+ false
51
+ end
52
+ end
53
+
54
+ def supports_block_expectations?
55
+ true
56
+ end
57
+
58
+ def failure_message
59
+ if @channel_not_found
60
+ "#{base_failure_message} but no broadcasted messages were found"
61
+ elsif @mutation_not_found
62
+ "#{base_failure_message} but no broadcasted messages with this mutation were found"
63
+ elsif @element_not_found
64
+ "#{base_failure_message} but message for given element was not found"
65
+ else
66
+ "#{base_failure_message} with #{@data} but mutated element with #{@message_data.except('selector')}"
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def mutation_options
73
+ return unless broadcasted_message
74
+
75
+ broadcasted_message.dig('operations', @action)
76
+ end
77
+
78
+ def broadcasted_message
79
+ message = pubsub_adapter.broadcasts(@channel_name).first
80
+ return unless message
81
+
82
+ decoded = ActiveSupport::JSON.decode(message)
83
+ decoded.with_indifferent_access if decoded.is_a?(Hash)
84
+ end
85
+
86
+ def verify_action_presence
87
+ return if @action.present?
88
+
89
+ message = 'Please specify the type of element mutation using .with(type_of_mutation, options)'
90
+ raise ArgumentError, message
91
+ end
92
+
93
+ def verify_channel_name_presence
94
+ return if @channel_name.present?
95
+
96
+ message = 'Please specify the channel using .on_channel'
97
+ raise ArgumentError, message
98
+ end
99
+
100
+ def base_failure_message
101
+ "expected to mutate element `#{@target}` on channel #{@channel_name}"
102
+ end
103
+
104
+ def pubsub_adapter
105
+ ::ActionCable.server.pubsub
106
+ end
107
+
108
+ def mutated_element_options(values)
109
+ values.flatten.find { |el| el['selector'] == @target }
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cable-ready-testing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paweł Dąbrowski
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-23 00:00:00.000000000 Z
11
+ date: 2020-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actioncable
@@ -72,12 +72,18 @@ email:
72
72
  executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
- files: []
75
+ files:
76
+ - lib/cable-ready-testing.rb
77
+ - lib/cable_ready/testing.rb
78
+ - lib/cable_ready/testing/rspec.rb
79
+ - lib/cable_ready/testing/version.rb
80
+ - lib/rspec/rails/matchers/cable_ready.rb
81
+ - lib/rspec/rails/matchers/cable_ready/mutated_element.rb
76
82
  homepage: http://github.com/pdabrowski6/cable-ready-testing
77
83
  licenses:
78
84
  - MIT
79
85
  metadata: {}
80
- post_install_message:
86
+ post_install_message:
81
87
  rdoc_options: []
82
88
  require_paths:
83
89
  - lib
@@ -93,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
99
  version: '0'
94
100
  requirements: []
95
101
  rubygems_version: 3.0.8
96
- signing_key:
102
+ signing_key:
97
103
  specification_version: 4
98
104
  summary: Testing utils for Cable Ready
99
105
  test_files: []