stimulus_reflex_testing 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 78135e70cab771486197a65392f65fbc585450aaf8bd58095c2777ced12834ca
4
+ data.tar.gz: f12fe3d40d1be2f3b90368d3ca2350f41b5b41543550cdccfcb668810975143f
5
+ SHA512:
6
+ metadata.gz: 1d1d25e8419746630001c914421c2e8428f1ef3752af48ef4d02fa3aa3971acb74ec055f3e0fa0ee3a2826d096495474390cdff3bcd1511c18c204f23a40d02e
7
+ data.tar.gz: 04e79184baa0b5bc3c0f643b3d314143c77161bb0d42cdb54c67f965542e7535fe25831db30743bb767928c47829e4dc19c53c3243f7b59db0503df3e8510747
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.6
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,6 @@
1
+ {
2
+ "ruby.lint": {
3
+ "rubocop": false,
4
+ "standard": true
5
+ }
6
+ }
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jason@thecharnes.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in stimulus_reflex_testing.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ stimulus_reflex_testing (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.14.1)
10
+ rake (12.3.3)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ minitest (~> 5.0)
17
+ rake (~> 12.0)
18
+ stimulus_reflex_testing!
19
+
20
+ BUNDLED WITH
21
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jason Charnes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,152 @@
1
+ # StimulusReflex Testing
2
+
3
+ So, you've started using the magic that is StimulusReflex. Neat, us too!
4
+
5
+ At this time, there aren't official testing helpers in the StimulusReflex library. We wanted to unit-test our reflexes, so we thought we share what we've been working on.
6
+
7
+ Things we'd like to add/improve on:
8
+
9
+ - MiniTest support
10
+ - Integration style testing?
11
+ - The ability to run assertions/expectations against the HTML re-rendered by a reflex
12
+ - `expect(subject.to_html).to include('Hi, I rendered from a Reflex!')`
13
+ - Session is stubbed out (Rspec) right now, but we'd love to find a way to support sessions in testing
14
+ - Things you find useful that we can't think of, yet
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'stimulus_reflex_testing'
22
+ ```
23
+
24
+ ### Using 5.2 or below? Or using a version of RSpec Rails lower than 4?
25
+
26
+ Both Rails 6 and RSpec Rails 4 introduce the `action-cable-testing` library. If you're using Rails 5.2 and a version of RSpec Rails lower than 4, include the `action-cable-testing` gem in your Gemfile.
27
+
28
+ ```ruby
29
+ gem 'stimulus_reflex_testing'
30
+ gem 'action-cable-testing'
31
+ ```
32
+
33
+ And then execute:
34
+
35
+ $ bundle install
36
+
37
+ ### RSpec instructions
38
+
39
+ Require the library into your `rails_helper`:
40
+
41
+ ```ruby
42
+ require 'stimulus_reflex_testing/rspec'
43
+ ```
44
+
45
+ Rspec tests include the reflex testing functionality when `type: :reflex` is provided. If this type is not provided, your Reflex tests won't work.
46
+
47
+ ## Usage
48
+
49
+ ### `build_reflex`
50
+
51
+ To build an instance of your reflex with the required setup handled (channel, url, element, etc) you can use the `build_reflex` method.
52
+
53
+ ```ruby
54
+ # With a valid URL so StimulusReflex can build the correct request
55
+ build_reflex(url: posts_url)
56
+
57
+ # Does your test rely on a `current_user` (or similar) defined in `ApplicationCable::Connection`?
58
+ build_reflex(url: posts_url, connection: { current_user: create(:user) })
59
+
60
+ # Do you rely on form params?
61
+ build_reflex(url: edit_post_url(post), params: { post: { title: 'A new title!' } })
62
+
63
+ # Need to give your element some data?
64
+ reflex = build_reflex(url: posts_url)
65
+ reflex.element.value = "Hello"
66
+ reflex.element.dataset.id = "123"
67
+ ```
68
+
69
+ ### `#run`
70
+
71
+ To unit test a reflex, we provide a method (when using `build_reflex`) called `#run`. This method takes a method name, and any arguments.
72
+
73
+ ```ruby
74
+ reflex = build_reflex(url: posts_url)
75
+ reflex.run(:method, arg1, arg2)
76
+ ```
77
+
78
+ **Why can't I just call the method directly?**
79
+
80
+ You're more than welcome to call the method directly. But, be advised you will lose callbacks. If you need to run `before_reflex` (etc) use the run method.
81
+
82
+ _`#run` is a wrapper around the underlying `#process` method in StimulusReflex that runs callbacks._
83
+
84
+ ### `#get`
85
+
86
+ To grab an instance variable set by a reflex, you can use the `#get` method when using a reflex built with `build_reflex`.
87
+
88
+ ```ruby
89
+ # app/reflexes/post_reflex.rb
90
+ class PostReflex < ApplicationReflex
91
+ def find_post
92
+ @post = Post.find(params[:id])
93
+ end
94
+ end
95
+
96
+ reflex = build_reflex(url: edit_post_url(post), params: { id: post.id })
97
+ reflex.run(:find_post)
98
+ reflex.get(:post) #=> returns the @post instance variable
99
+ ```
100
+
101
+ ## RSpec example
102
+
103
+ ```ruby
104
+ # app/reflexes/post_reflex.rb
105
+ class PostReflex < ApplicationReflex
106
+ def validate
107
+ @post = Post.find(element.dataset.post_id)
108
+ @post.validate
109
+ end
110
+ end
111
+
112
+ # spec/reflexes/post_reflex_spec.rb
113
+ RSpec.describe PostReflex, type: :reflex do
114
+ let(:post) { create(:post) }
115
+ let(:reflex) { build_reflex(url: edit_post_url(post)) }
116
+
117
+ describe '#validate' do
118
+ subject { reflex.run(:validate) }
119
+
120
+ before do
121
+ reflex.element.dataset.post_id = post.id
122
+ subject
123
+ end
124
+
125
+ it 'should find the post' do
126
+ expect(reflex.get(:post)).to eq(post)
127
+ end
128
+
129
+ it 'should validate the post' do
130
+ expect(reflex.get(:post).errors).to be_present
131
+ end
132
+ end
133
+ end
134
+ ```
135
+
136
+ ## Development
137
+
138
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
139
+
140
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
141
+
142
+ ## Contributing
143
+
144
+ Bug reports and pull requests are welcome on GitHub at https://github.com/podia/stimulus_reflex_testing. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/podia/stimulus_reflex_testing/blob/master/CODE_OF_CONDUCT.md).
145
+
146
+ ## License
147
+
148
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
149
+
150
+ ## Code of Conduct
151
+
152
+ Everyone interacting in the StimulusReflexTesting project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/podia/stimulus_reflex_testing/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "stimulus_reflex_testing"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ if defined?(StimulusReflex)
2
+ module RSpec
3
+ module Rails
4
+ module ReflexExampleGroup
5
+ extend ActiveSupport::Concern
6
+ include StimulusReflex::TestCase::Behavior
7
+
8
+ module ClassMethods
9
+ def reflex_class
10
+ described_class
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ require "rspec/matchers"
2
+
3
+ RSpec::Matchers.define :have_set do |instance_variable, expected_value|
4
+ match do |actual|
5
+ actual.get(instance_variable) == expected_value
6
+ end
7
+
8
+ description do |actual|
9
+ "set #{instance_variable} to #{expected_value}, got #{actual.get(instance_variable)}"
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ require "active_support"
2
+ require "active_support/test_case"
3
+
4
+ class StimulusReflex::TestCase < ActiveSupport::TestCase
5
+ class TestChannel < ActionCable::Channel::TestCase
6
+ _channel_class = StimulusReflex::Channel
7
+
8
+ def initialize(connection_opts = {})
9
+ super("StimulusReflex::Channel")
10
+ @connection = stub_connection(connection_opts.merge(env: {}))
11
+ end
12
+ end
13
+
14
+ module Behavior
15
+ extend ActiveSupport::Concern
16
+
17
+ module ClassMethods
18
+ def reflex_class
19
+ end
20
+ end
21
+
22
+ def build_reflex(opts = {})
23
+ channel = opts.fetch(:channel, TestChannel.new(opts.fetch(:connection, {})))
24
+ element = opts.fetch(:element, StimulusReflex::Element.new)
25
+
26
+ self.class.reflex_class.new(
27
+ channel, element: element, url: opts.dig(:url), params: opts.fetch(:params, {})
28
+ )
29
+ end
30
+ end
31
+
32
+ include Behavior
33
+ rescue NameError => e
34
+ if e.missing_name == "ActionCable::Channel::TestCase"
35
+ raise "Please install action-cable-testing https://github.com/palkan/action-cable-testing"
36
+ else
37
+ raise
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ module StimulusReflex::TestReflexPatches
2
+ def get(instance_variable)
3
+ instance_variable_get("@#{instance_variable}")
4
+ end
5
+
6
+ def run(method_name, *args)
7
+ process(method_name, *args)
8
+ end
9
+ end
10
+
11
+ StimulusReflex::Reflex.include(StimulusReflex::TestReflexPatches)
@@ -0,0 +1,3 @@
1
+ require "stimulus_reflex_testing/version"
2
+ require "stimulus_reflex/test_case"
3
+ require "stimulus_reflex/test_reflex_patches"
@@ -0,0 +1,15 @@
1
+ require "rspec/rails/example/reflex_example_group"
2
+ require "rspec/rails/matchers/stimulus_reflex"
3
+
4
+ RSpec.configure do |config|
5
+ if defined?(StimulusReflex)
6
+ config.include Rails.application.routes.url_helpers, type: :reflex
7
+ config.include RSpec::Rails::ReflexExampleGroup, type: :reflex
8
+
9
+ config.before type: :reflex do
10
+ allow_any_instance_of(ActionDispatch::Request).to(
11
+ receive(:session).and_return(double(:session, load!: true))
12
+ )
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module StimulusReflexTesting
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ require_relative "lib/stimulus_reflex_testing/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "stimulus_reflex_testing"
5
+ spec.version = StimulusReflexTesting::VERSION
6
+ spec.authors = ["Jason Charnes", "Jamie Lawrence", "Basil Khan"]
7
+ spec.email = ["jason@thecharnes.com", "jamie@ideasasylum.com", "basil@podia.com"]
8
+
9
+ spec.summary = "An environment and set of helpers for testing StimulusReflex."
10
+ spec.homepage = "https://github.com/podia/stimulus_reflex_testing"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = spec.homepage
16
+ # spec.metadata["changelog_uri"] = "Put your gem's CHANGELOG.md URL here."
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stimulus_reflex_testing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Charnes
8
+ - Jamie Lawrence
9
+ - Basil Khan
10
+ autorequire:
11
+ bindir: exe
12
+ cert_chain: []
13
+ date: 2020-06-11 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description:
16
+ email:
17
+ - jason@thecharnes.com
18
+ - jamie@ideasasylum.com
19
+ - basil@podia.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - ".gitignore"
25
+ - ".travis.yml"
26
+ - ".vscode/settings.json"
27
+ - CODE_OF_CONDUCT.md
28
+ - Gemfile
29
+ - Gemfile.lock
30
+ - LICENSE.txt
31
+ - README.md
32
+ - Rakefile
33
+ - bin/console
34
+ - bin/setup
35
+ - lib/rspec/rails/example/reflex_example_group.rb
36
+ - lib/rspec/rails/matchers/stimulus_reflex.rb
37
+ - lib/stimulus_reflex/test_case.rb
38
+ - lib/stimulus_reflex/test_reflex_patches.rb
39
+ - lib/stimulus_reflex_testing.rb
40
+ - lib/stimulus_reflex_testing/rspec.rb
41
+ - lib/stimulus_reflex_testing/version.rb
42
+ - stimulus_reflex_testing.gemspec
43
+ homepage: https://github.com/podia/stimulus_reflex_testing
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/podia/stimulus_reflex_testing
48
+ source_code_uri: https://github.com/podia/stimulus_reflex_testing
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.3.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.0.3
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: An environment and set of helpers for testing StimulusReflex.
68
+ test_files: []