importmap_mocha-rails 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 11365cfee72382ffbf5e419f2c8a197adb2fe8d327d4c6b2350448fd09be05e6
4
+ data.tar.gz: c557c7151e4957c2993542bd3cd62816564f65c896676bd0e0494569c50879ec
5
+ SHA512:
6
+ metadata.gz: 73912e0a9e2c03310ac7526a842ac99c669fe59b5517bc0c5ede105a96063e106f74f1c1f11b1a646c409b9704d1e036e35b79474582b0f37eeabfcea43ed555
7
+ data.tar.gz: 7c5c02c7edd5fc9b5772c47635d503119b9ada153e9e6170bc40850e2807fd5859f152899604921d572bcf9dac49ffbec6c5d0a25e04b2cbb4285f15582ab7e4
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2023 Takashi Kato <tohosaku@users.osdn.me>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # importmap_mocha-rails
2
+
3
+ This plugin makes it easy to test ES modules with [importmap-rails](https://github.com/rails/importmap-rails) when using Rails 7 or later. It integrates the [Mocha](https://mochajs.org/) JavaScript testing library (using [Chai](https://www.chaijs.com/) as the assertion library) and runs tests for ES modules delivered with importmap in the browser.
4
+
5
+ # Installation
6
+
7
+ Assuming you have already installed importmap-rails with Rails 7, add the following to your Gemfile and run `bundle install`.
8
+
9
+ ```ruby
10
+ group :test, :development do
11
+ gem 'importmap_mocha-rails'
12
+ end
13
+ ```
14
+
15
+ # Usage
16
+
17
+ Write your JavaScript tests in `test/javascripts` or `spec/javascripts`. One-to-one tests are named foo.test.js for a module named foo.js. Access `http://localhost:3000/rails/info/mocha` in the Rails testing or development environment to view the test results.
18
+
19
+ By default, importmap-rails manages ES modules under `app/javascript`, `app/assets/javascripts`, and `vendor/javascripts`.
20
+
21
+ # Example
22
+
23
+ controllers/clear_controller.js
24
+
25
+ ```javascript
26
+ import { Controller } from "@hotwired/stimulus"
27
+
28
+ export default class extends Controller {
29
+
30
+ static targets = ["clear"];
31
+
32
+ clear(e) {
33
+ this.clearTargets.forEach(o => { o.value = ''});
34
+ }
35
+ }
36
+ ```
37
+
38
+ controllers/clear_controller.test.js
39
+
40
+ ```javascript
41
+ import { Application } from "@hotwired/stimulus"
42
+ import ClearController from 'controllers/clear_controller'
43
+
44
+ const html = `<div data-controller="clear">
45
+ <input id="target" type="text" value="foo" data-clear-target="clear">
46
+ <button data-action="clear#clear">test</button>
47
+ </div>`
48
+
49
+ const config = { attributes: true, childList: true, subtree: true };
50
+
51
+ // test!! test!!
52
+ describe('clear_controller', () => {
53
+
54
+ let container;
55
+
56
+ beforeEach(() => {
57
+ container = document.getElementById('container')
58
+ container.insertAdjacentHTML('afterbegin', html)
59
+ });
60
+
61
+ afterEach(() => {
62
+ const clone = container.cloneNode(false);
63
+ container.parentNode.replaceChild(clone, container);
64
+ });
65
+
66
+ const watch = (fn) => {
67
+ const observer = new MutationObserver(fn);
68
+ observer.observe(container, config);
69
+ }
70
+
71
+ describe('click', () => {
72
+ it('The value of input element is cleard', () => {
73
+ const app = Application.start();
74
+ app.register('clear', ClearController);
75
+
76
+ const target = container.querySelector('#target');
77
+
78
+ watch(() => target.value.to.equal(''));
79
+
80
+ const button = container.querySelector('button');
81
+ button.click()
82
+ });
83
+ });
84
+ });
85
+
86
+ ```
87
+
88
+ # Configuration
89
+
90
+ * config.importmap_mocha_style: The style of the test code, `"bdd"` or `"tdd"`. Default is `"bdd"`.
91
+ * config.importmap_mocha_path: The location where the test code is stored. Default is `test/javascripts` and `spec/javascripts`.
92
+
93
+ # Author
94
+
95
+ Takashi Kato tohosaku@users.osdn.me
96
+
97
+ # License
98
+
99
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'bundler/gem_tasks'
File without changes
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImportmapMocha
4
+ class TestController < ActionController::Base
5
+ layout false
6
+
7
+ def index; end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImportmapMocha
4
+ module TestHelper
5
+ PATTERN = ['**/*.test.js', '**/*.spec.js'].freeze
6
+
7
+ def testcase
8
+ files.map { |m| javascript_import_module_tag(m.to_s.sub('.js', '')) }.join("\n").html_safe
9
+ end
10
+
11
+ def files
12
+ root_path.flat_map { |path| path.glob(PATTERN).map { |m| m.relative_path_from(path) } }
13
+ end
14
+
15
+ def root_path
16
+ Rails.application.config.importmap_mocha_path
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <title>Results of test execution </title>
6
+ <%= javascript_include_tag 'mocha', 'chai' %>
7
+ <%= stylesheet_link_tag 'mocha' %>
8
+ <%= javascript_importmap_tags 'importmap_mocha' %>
9
+ <meta name="viewport" content="width=device-width, initial-scale=1">
10
+ <script>
11
+ mocha.setup('<%= Rails.application.config.importmap_mocha_style %>');
12
+ </script>
13
+ </head>
14
+ <body>
15
+ <%= testcase %>
16
+ <div id="main">
17
+ <div id="content">
18
+ <div id="container">
19
+ </div>
20
+ <div id="mocha">
21
+ </div>
22
+ </div>
23
+ </div>
24
+ <script type="module">
25
+ mocha.run();
26
+ </script>
27
+ </body>
28
+ </html>
@@ -0,0 +1,4 @@
1
+ pin 'importmap_mocha'
2
+ Rails.application.config.importmap_mocha_path.each do |path|
3
+ pin_all_from path
4
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImportmapMocha
4
+ class Engine < ::Rails::Engine
5
+ config.before_initialize do
6
+ Rails.application.config.importmap_mocha_path = []
7
+ %w[test spec].each do |d|
8
+ path = Rails.root.join(d, 'javascripts')
9
+ Rails.application.config.importmap_mocha_path << path if path.exist?
10
+ end
11
+ end
12
+
13
+ initializer 'importmap_mocha.assets' do
14
+ if Rails.application.config.respond_to?(:assets)
15
+ Rails.application.config.assets.paths << Engine.root.join('app/assets/javascripts')
16
+ Rails.application.config.assets.paths << Engine.root.join('app/assets/stylesheets')
17
+ Rails.application.config.assets.paths << Engine.root.join('vendor/javascripts')
18
+ Rails.application.config.assets.paths << Engine.root.join('vendor/stylesheets')
19
+ Rails.application.config.assets.paths += Rails.application.config.importmap_mocha_path
20
+ end
21
+ end
22
+
23
+ PRECOMPILE_ASSETS = %w[importmap_mocha.js chai.js mocha.js mocha.css].freeze
24
+
25
+ initializer 'turbo.assets' do
26
+ Rails.application.config.assets.precompile += PRECOMPILE_ASSETS if Rails.application.config.respond_to?(:assets)
27
+ end
28
+
29
+ initializer 'importmap_mocha.importmap', before: 'importmap' do |app|
30
+ app.config.importmap.paths << Engine.root.join('config/importmap.rb') if Rails.application.respond_to?(:importmap)
31
+ end
32
+
33
+ initializer 'importmap_mocha.routes' do
34
+ Rails.application.routes.prepend do
35
+ scope module: 'importmap_mocha' do
36
+ get '/rails/info/mocha' => 'test#index'
37
+ end
38
+ end
39
+ end
40
+
41
+ initializer 'importmap_mocha.config' do
42
+ unless Rails.application.config.respond_to?(:importmap_mocha_style)
43
+ Rails.application.config.importmap_mocha_style = 'bdd'
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImportmapMocha
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'importmap_mocha/version'
2
+ require 'importmap_mocha/engine'
3
+
4
+ module ImportmapMocha
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :importmap_mocha_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: importmap_mocha-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takashi Kato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ description: Add JavaScript testing tools in importmap environment.
28
+ email:
29
+ - tohosaku@users.osdn.me
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/assets/javascripts/importmap_mocha.js
38
+ - app/controllers/importmap_mocha/test_controller.rb
39
+ - app/helpers/importmap_mocha/test_helper.rb
40
+ - app/views/importmap_mocha/test/index.html.erb
41
+ - config/importmap.rb
42
+ - lib/importmap_mocha-rails.rb
43
+ - lib/importmap_mocha/engine.rb
44
+ - lib/importmap_mocha/version.rb
45
+ - lib/tasks/importmap_mocha/rails_tasks.rake
46
+ homepage: https://github.com/tohosaku/importmap_mocha-rails
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/tohosaku/importmap_mocha-rails
51
+ source_code_uri: https://github.com/tohosaku/importmap_mocha-rails
52
+ changelog_uri: https://github.com/tohosaku/importmap_mocha-rails
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.7.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.2.5
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: mochajs rails integration
72
+ test_files: []