e2e_tests_connector 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: 6278f234d995c75f505a708c1f8631c3ee25878fb42066d6f96d78f58b0f8d51
4
+ data.tar.gz: '0769662a217b16c330d26c8cb9315caa228a865eea39e2f738e65c3345ae5ddf'
5
+ SHA512:
6
+ metadata.gz: daa2d87357337e775c28c80110b3276ac26df591ea0adfcf9aa7b75a495e55cbaf2bc4d3a54ec97b30e46914f816aeeed4704b13df576a66e685674647c7535b
7
+ data.tar.gz: 4d2be916085fdfbdd7f5babaeb2503cd92f138f20ab1a46c71f8bd9bdcec19ef51d79e8cc6d02bef654a1851677ff37e5f1138afc6ee8334ea7329f8ba580cbb
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 owen2345
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.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # E2eTestsConnector
2
+
3
+
4
+ ## Usage
5
+ This gem permits to connect frontend apps like react, angular, etc with backend rails apps to run end to end tests using libraries like [cypress](https://www.cypress.io/)
6
+
7
+ ## Backend Installation
8
+ - Add dependency
9
+ Add this line to your application's Gemfile:
10
+ ```ruby
11
+ group :test do
12
+ gem 'e2e_tests_connector'
13
+ end
14
+ ```
15
+
16
+ And then execute:
17
+ ```bash
18
+ $ bundle install
19
+ ```
20
+ - Mount route
21
+ ```ruby
22
+ mount E2eTestsConnector::Engine, at: '/e2e_tests_connector'
23
+ ```
24
+
25
+ ## Frontend Installation
26
+ - Copy [backend_connector.ts](/docs/backend_connector.ts?raw=true) into `cypress/support/backend_connector.ts`
27
+ - Import the connector in `cypress/support/index.js`
28
+ `import './backend_connector';`
29
+
30
+ ## Configuration
31
+ Optionally change default configurations when using custom factories or database cleaners
32
+ ```ruby
33
+ # config/initializers/e2e_tests_connector.rb
34
+ return unless defined?(E2eTestsConnector)
35
+
36
+ E2eTestsConnector::Config.init_custom_mocks = lambda do |_params|
37
+ # here custom mocks or initializers when starting e2e tests (default empty)
38
+ end
39
+
40
+ E2eTestsConnector::Config.reset_db = lambda do |_params|
41
+ # here custom database cleaner (default DatabaseCleaner.clean)
42
+ end
43
+
44
+ E2eTestsConnector::Config.run_factory = lambda do |_params|
45
+ # here custom factory builder (default FactoryBot)
46
+ end
47
+ ```
48
+
49
+ ## Running
50
+ - Start test applications, like:
51
+ `cd backend_app/ && RAILS_ENV=test bundle exec rails s -e test -p 3001 &`
52
+ `cd frontend_app/ && PORT=3000 BROWSER=none RAILS_PORT=3001 yarn start test --silent &`
53
+ - Run e2e tests (hosts and ports may vary based on your test apps)
54
+ `CYPRESS_BASE_URL=http://localhost:3000 CYPRESS_BACKEND_URL=http://localhost:3001 yarn cypress run`
55
+
56
+ ## Samples
57
+ - Factory command
58
+ ```typescript
59
+ // Multiple values
60
+ cy.factory('create_list(:article, 2)').as('articles');
61
+ // => [{ id: 1, title: "Sample article", created_at: "..", ... }, { id: 2, title: "Sample article", created_at: "..", ... }]
62
+
63
+ // Provide custom data
64
+ cy.factory('create(:article, title: "Sample article")').as('article');
65
+ // => { id: 1, title: "Sample article", created_at: "..", ... }
66
+
67
+ // Return custom data
68
+ cy.factory('create(:article).as_json(only: [:id, :title])').as('article');
69
+ // => { id: 1, title: "..." }
70
+
71
+ // Multiple commands
72
+ cy.factory(['create(:article, kind: :external)', 'create(:article, kind: :internal)']).as('articles');
73
+ // => [{ id: 1, title: "..", kind: "external", ... }, { id: 2, title: "..", kind: "internal", ... }]
74
+ ```
75
+ - Custom command
76
+ ```typescript
77
+ // Single value
78
+ cy.cmd('Article.find(1)').as('article');
79
+ // => { id: 1, title: "..", kind: "external", ... }
80
+
81
+ // Multiple values
82
+ cy.cmd('Article.where(kind: :external)').as('articles');
83
+ // => [{ id: 1, title: "..", kind: "external", ... }, { id: 2, title: "..", kind: "external", ... }, ...]
84
+
85
+ // Multiple commands
86
+ cy.cmd(['Article.find(1)', 'Article.find(2)']).as('articles');
87
+ // => [{ id: 1, title: "..", kind: "external", ... }, { id: 2, title: "..", kind: "internal", ... }]
88
+ ```
89
+ - Full example
90
+ ```typescript
91
+ describe('When listing articles', () => {
92
+ beforeEach(() => {
93
+ // cy.stubLoginWith();
94
+ cy.factory('create_list(:article, 2)').as('articles');
95
+ });
96
+
97
+ it('includes all expected articles', function () {
98
+ cy.visit('/articles');
99
+ cy.get('table tbody tr').should('have.length', this.articles.length);
100
+ });
101
+
102
+ describe('When filtering', () => {
103
+ it('returns expected article', function () {
104
+ const article = this.articles[0];
105
+ cy.visit(`/articles/filter=${article.number}`);
106
+ cy.get('table').contains(article.number);
107
+ });
108
+ });
109
+ });
110
+ ```
111
+
112
+ ## Troubleshooting
113
+ - `uninitialized constant E2eTestsConnector::Config::DatabaseCleaner`
114
+ Require the corresponding library in `spec/spec_helper.rb`
115
+ ```ruby
116
+ require 'factory_bot_rails' # OR
117
+ require 'database_cleaner/active_record'
118
+ ```
119
+
120
+ ## Contributing
121
+ Contribution directions go here.
122
+
123
+ ## License
124
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+
5
+ APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
6
+ load 'rails/tasks/engine.rake'
7
+
8
+ load 'rails/tasks/statistics.rake'
9
+
10
+ require 'bundler/gem_tasks'
11
+
12
+ require 'rake/testtask'
13
+
14
+ Rake::TestTask.new(:spec) do |t|
15
+ t.libs << 'spec'
16
+ t.pattern = 'spec/**/*_spec.rb'
17
+ t.verbose = false
18
+ end
19
+
20
+ task default: :test
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module E2eTestsConnector
4
+ class ApplicationController < ActionController::API
5
+ end
6
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/rails' if defined?(RSpec)
4
+ require Rails.root.join('spec/rails_helper').to_s if defined?(RSpec)
5
+ require 'database_cleaner/active_record' if defined?(DatabaseCleaner)
6
+
7
+ module E2eTestsConnector
8
+ class ConnectorController < ApplicationController
9
+ def call
10
+ case params[:kind]
11
+ when 'reset_db'
12
+ run_reset_db
13
+ when 'factory'
14
+ run_factory
15
+ when 'init_custom_mocks'
16
+ init_custom_mocks
17
+ else
18
+ run_command
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def single_command?
25
+ params[:commands].is_a?(String)
26
+ end
27
+
28
+ def run_reset_db
29
+ E2eTestsConnector::Config.reset_db.call(params)
30
+ render json: { res: nil }
31
+ end
32
+
33
+ def run_factory
34
+ res = E2eTestsConnector::Config.run_factory.call(params)
35
+ render json: { res: single_command? ? res.first : res }
36
+ end
37
+
38
+ def run_command
39
+ res = Array(params[:commands]).map { |cmd| eval(cmd) } # rubocop:disable Security/Eval
40
+ render json: { res: single_command? ? res.first : res }
41
+ end
42
+
43
+ def init_custom_mocks
44
+ E2eTestsConnector::Config.init_custom_mocks.call(params)
45
+ render json: { res: nil }
46
+ end
47
+ end
48
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ E2eTestsConnector::Engine.routes.draw do
4
+ match 'call', to: 'connector#call', via: %i[get post]
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'e2e_tests_connector/version'
4
+ require 'e2e_tests_connector/engine'
5
+ require 'e2e_tests_connector/config'
6
+
7
+ module E2eTestsConnector
8
+ def self.table_name_prefix
9
+ ''
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module E2eTestsConnector
4
+ class Config
5
+ # By default void
6
+ # @return nil
7
+ cattr_accessor :init_custom_mocks do
8
+ lambda do |_params|
9
+ end
10
+ end
11
+
12
+ # By default using database cleaner
13
+ # @return nil
14
+ cattr_accessor :reset_db do
15
+ lambda do |_params|
16
+ DatabaseCleaner.strategy = :truncation
17
+ DatabaseCleaner.clean
18
+ end
19
+ end
20
+
21
+ # By default using factory bot
22
+ # @return (Array<ActiveRecord>)
23
+ cattr_accessor :run_factory do
24
+ lambda do |params|
25
+ Array(params[:commands]).map do |cmd|
26
+ FactoryBot.class_eval(cmd).as_json(params[:jsonArgs])
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module E2eTestsConnector
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace E2eTestsConnector
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module E2eTestsConnector
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: e2e_tests_connector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - owen2345
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-08 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Permits to connect frontend apps with backend apps to run e2e tests like
28
+ cypress
29
+ email:
30
+ - owenperedo@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - app/controllers/e2e_tests_connector/application_controller.rb
39
+ - app/controllers/e2e_tests_connector/connector_controller.rb
40
+ - config/routes.rb
41
+ - lib/e2e_tests_connector.rb
42
+ - lib/e2e_tests_connector/config.rb
43
+ - lib/e2e_tests_connector/engine.rb
44
+ - lib/e2e_tests_connector/version.rb
45
+ homepage: https://github.com/owen2345/e2e_tests_connector
46
+ licenses:
47
+ - MIT
48
+ metadata:
49
+ homepage_uri: https://github.com/owen2345/e2e_tests_connector
50
+ source_code_uri: https://github.com/owen2345/e2e_tests_connector
51
+ changelog_uri: https://github.com/owen2345/e2e_tests_connector
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '2.2'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.1.2
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Permits to connect frontend apps with backend apps to run e2e tests like
71
+ cypress
72
+ test_files: []