Capybara
view_component
Our great sponsors
Capybara | view_component | |
---|---|---|
20 | 74 | |
9,958 | 3,139 | |
0.3% | 1.0% | |
7.7 | 9.0 | |
21 days ago | 7 days ago | |
Ruby | Ruby | |
MIT License | MIT License |
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.
Capybara
-
16 Best Ruby Frameworks For Web Development [2024]
Cuba takes help from a lot of other technologies to bring the best of everything. For example, the responses in Cuba are the optimized version of the Rack responses. The templates are integrated via Tilt and testing via Cutest and Capybara.
-
š©° Scheduling automated tests
I am going to use a browser based testing tool called Playwright (But you could use Capybara, or Selenium WebDriver etc.).
-
Building GitHub with Ruby on Rails
Even as a much smaller team, building Heii On-Call [0] as a lightweight alerting/monitoring/on-call rotations SaaS based on Ruby on Rails has basically been a pleasure!
And as the article highlights, perhaps the key reason for smooth deployments and upgrades is that the CI testing story is so, so good: RSpec [1] plus Capybara [2] for us. That means we have decently extensive tests of just about all behavior. The few small Rails and Ruby upgrades we've done have gone quite smoothly and confidently, with usually just a few non-Rails gem dependencies needing to be manually updated as well.
The "microservices" story is where we've pulled in the Crystal programming language [3] to great effect. After dabbling with Go and Rust, we've found that Crystal is truly a breath of fresh air. Crystal powers the parts of Heii On-Call that need to be fast and low-RAM, specifically the inbound API https://api.heiioncall.com/ and the outbound HTTP(S) prober background processes. I've ported some shared utility classes from Ruby to Crystal almost completely by just copy-and-pasting ___.rb to ___.cr; porting the tests for those classes was far more onerous than porting the class code itself. (Perhaps another point of evidence toward the superiority of RoR's testing story...)
The front-end story is nice but just a bit weaker. Using Hotwire / Turbo successfully, but I have an open PR to fix a fairly obvious stale cache bug in Turbo [4] that has been sitting unloved for nearly a month, despite other users reporting the same issue. I'm hopeful that it will get merged in the next release, but definitely less active than the backend side.
For me, the key conclusion is that the excellent Ruby on Rails testing story is what enables everything to go a lot more smoothly and have such a strong foundation. I'd be curious if any GitHubbers can talk more about whether they too are using Rspec+Capybara or something else? Are there internal guidelines for test coverage?
-
Minitest vs. RSpec in Rails
Since the Capybara library drives the underlying tests, Minitest also has the same syntax.
- Is it a common practice to test JS code in a browser instead of Node.js?
-
From partials to ViewComponents: writing reusable front-end code in Rails
The nice thing about partial templates is that templates are unit-testable with View specs (or similarly in Minitest) and the rendered output can even be verified using Capybara matchers.
-
Tip: if you're changing all your form_for to form_with, take the opportunity to make sure all forms are being tested.
To piggyback: This would be a type of browser test, so you would want to use something like Cypress (https://github.com/testdouble/cypress-rails) or Capybara (https://github.com/teamcapybara/capybara). RSpec has a good integration with Capybara. Cypress is JS-based so it will require some additional config.
- How to use undocumented web APIs
- Don't make me think, or why I switched to Rails from JavaScript SPAs
-
The Benefits of Acceptance Testing
For instance, the acceptance test above requires a log in routine. Here's where the expressive power of a DSL like Capybara manifests:
view_component
- Things I wish I knew before moving 50K lines of code to React Server Components
- Have you been using ViewComponent. What advantages do you see in it?
-
Vanilla Rails view components with partials | Stanko K.R.
I used to do "pure ruby" approach to that -- but basically wound up realizing I was re-inventing github's view_component. Their design goals were similar enough to what I was trying to do, that it made more sense just to use that, rather than try to re-invent it myself.
-
Gnarly Learnings from March 2023
ViewComponent
- Os benefĆcios de componentizar as views do Rails
-
Does anyone kind of miss simpler webpages?
The linked one is my Rails implementation, written for ViewComponent. The official version uses Nunjucks.
-
I QUIT MY JOB
In short, we migrated to ViewComponentViewComponent, opting for good ole Rails views, which allowed us to simplify the stack while retaining the benefits of the presenter pattern.
-
Rails 7 + Hotwire (Turbo + Stimulus) = Modern web applications
tl;dr; Letās understand how we can migrate a scaffold generated by the command ābin/rails g scaffold Kit::Product name:stringā, to an improved and performative screen. Inserting partial load and DOM manipulation writing a very little code in VanillaJS (āalmostā without to write Javascript). Scaffold (Rails 7) Getting Started with Rails (https://guides.rubyonrails.org/getting_started.html#creating-a-new-rails-project-installing-rails) Generating the scaffold (https://guides.rubyonrails.org/command_line.html#bin-rails-generate): Using just one command you will generate all of these files: Model, Migration, Controllers, Routes, Tests, Helpers, and JSON. $ **bin/rails generate scaffold HighScore game:string score:integer** invoke active_record create db/migrate/20190416145729_create_high_scores.rb create app/models/high_score.rb invoke test_unit create test/models/high_score_test.rb create test/fixtures/high_scores.yml invoke resource_route route resources :high_scores invoke scaffold_controller create app/controllers/high_scores_controller.rb invoke erb create app/views/high_scores create app/views/high_scores/index.html.erb create app/views/high_scores/edit.html.erb create app/views/high_scores/show.html.erb create app/views/high_scores/new.html.erb create app/views/high_scores/_form.html.erb invoke test_unit create test/controllers/high_scores_controller_test.rb create test/system/high_scores_test.rb invoke helper create app/helpers/high_scores_helper.rb invoke test_unit invoke jbuilder create app/views/high_scores/index.json.jbuilder create app/views/high_scores/show.json.jbuilder create app/views/high_scores/_high_score.json.jbuilder Enter fullscreen mode Exit fullscreen mode After installing Rails, create your project, and generate your scaffold. Let's try to improve the default interface. I am using the Tailwind to insert styles on the interfaces and the ViewComponent for ācreating reusable, testable & encapsulated view components, built to integrate seamlessly with Ruby on Railsā. You could see the resulting code at https://github.com/raphox/rails-7-fullstack. Turbo (heart of Hotwire) Hotwire is an alternative approach to building modern web applications without using much JavaScript by sending HTML instead of JSON over the wire. You can see the unique applicationās screen in the following image. Yes, this project has just one screen. Letās try to be simple to focus on the most important things. In this screen, we have two dynamic areas: List of products Form to create and edit the selected product I created a PR so you can see the exact part of the code that was changed or inserted to use the Hotwire on your project. You can see this PR on https://github.com/raphox/rails-7-fullstack/pull/1. The unique screen on the system has a few triggers to call actions or make requests. Plus button to access the ākit/products/newā page Search input field Products links to access the productās edit page Buttons to update, remove or create products based on the formās fields data Using the Turbo, we can override the click event of the plus button to request the HTML of a new product form from the backend and put it in the āproduct_formā area. You donāt need to write Javascript to override the events on your elements, just insert a new attribute āturbo_frameā on your HTML tag. Like here: We can use the same approach of the plus button in the link of products and prevent reloading the entire screen HTML and assets. Now we need to use a little Javascript to insert the event āchangeā to request a filtered list of products from the backend. In my code, I insert the āturbo_frameā attribute into the form to change the submit action making the request using Turbo. In the next topic, we can see how to use the Stimulus and Javascript controllers to change the event āchangeā of the input. Now we will need to submit the form and refresh the list of products. But we can do this better. We know that the updated list has changed on the created/updated product. So, we need to update the list with this product, instead refresh and query all the list of products from the database. To do this we need to use a lot of the Javascript code or a few actions from the Stimulus. You can see about the Javascript controller in the next topic. Stimulus (modest JavaScript framework) You wonāt see any difference. But the code savings and ease of integration will help you a lot when inserting new features. The objective here is to insert an event on the search input form to make a request when the user changes the inputās value. When it occurs, the system will make a request to get all products filtered by the ānameā column. The result will be inserted into the list after it. To implement the Stimulus in our current project, we need to follow the following steps: 1. Insert the attribute ādata-controller=āsearch-listāā to the wrapper with the form and the list of the products (app/components/search_list_component.html.erb line 1). ā This attribute is responsible to relate the following code to the DIV element. Get more info here https://stimulus.hotwired.dev/. ā You can use the command ā./bin/rails generate stimulus controllerNameā to generate the Javascript file with the class definition and import it in the āapp/javascript/controllers/index.jsā automatically. 2. Insert the attribute ādata-search_list_target=āinputSearchāā to the form search field (app/components/search_list_form_component.html.erb line 2). 3. Insert the action (click->search-list#selectItem) to activate the currently selected product (app/components/search_list_item_component.rb lines 20). 4. Insert the attribute āsearch_list_target=āitemāā to each item of the product list (app/components/search_list_item_component.rb lines 21). I am using https://viewcomponent.org/ to define the view components instead to use a view or partial. Pay attention to this part https://viewcomponent.org/#performance. I really liked having this structure in my project: rails-7-fullstack/_list.html.erb at 11a479dafa5c84a1e769721d57792f39914a1204 Ā·ā¦ This approach is very similar to the React components or other Javascript Frameworks. I created a PR where you can see the exact part of the code I changed or inserted to use the Stimulus on my project. You can see this PR on https://github.com/raphox/rails-7-fullstack/pull/2.
-
Are utility classes horrible design or am I dumb?
Rails has view components. GitHub started the pattern years ago in their Rails app.
-
Can anyone recommend good, free component libraries for Tailwind?
In Rails, I personally like to reuse a components and for that I use viewcomponents Gem (https://viewcomponent.org) My erb are still very verbose but I dont dupplicate the HTML.
What are some alternatives?
Playwright - Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
Aruba - Test command-line applications with Cucumber-Ruby, RSpec or Minitest.
shoulda-matchers - Simple one-liner tests for common Rails functionality
Emoji-RSpec - Custom Emoji Formatters for RSpec
Cucumber - A home for issues that are common to multiple cucumber repositories
Bacon - a small RSpec clone
stimulus-use - A collection of composable behaviors for your Stimulus Controllers
Spinach - Spinach is a BDD framework on top of Gherkin.
Stimulus - A modest JavaScript framework for the HTML you already have
turbo-rails - Use Turbo in your Ruby on Rails app
RSpec - RSpec meta-gem that depends on the other components
Tailwind CSS - A utility-first CSS framework for rapid UI development.