Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Chart Appointment Example
require 'rails_helper'
feature 'Update OCT data', js: true do
before do
create(:appointment_module_type, name: 'OCT')
appointment = create('schedule/appointment')
login_as(appointment.physician.user)
visit patient_chart_path(appointment.patient)
end
scenario 'valid inputs' do
click_on 'Add'
click_on 'OCT'
click_on 'OCT'
fill_in 'Notes', with: 'OCT data'
click_on 'Save'
click_on 'OCT'
fill_in 'Notes', with: 'other OCT data'
click_on 'Save'
click_on 'OCT'
expect(page).to have_field('Notes', with: 'other OCT data')
end
end
require 'rails_helper'
feature 'Update OCT data', js: true do
before do
create(:appointment_module_type, name: 'OCT')
appointment = create('schedule/appointment')
login_as(appointment.physician.user)
visit patient_chart_path(appointment.patient)
end
scenario 'valid inputs' do
chart_appointment = ChartAppointment.new.add_module('OCT')
.save_module_data('OCT', notes: 'OCT data')
.save_module_data('OCT', notes: 'other OCT data')
expect(chart_appointment).to have_module_data('OCT', notes: 'other OCT data')
end
end
require 'rails_helper'
feature 'Update OCT data', js: true, test_helpers: [:chart_appointment] do
let(:appointment) { chart_appointment.given_there_is_a_scheduled_appointment }
before do
login_as(appointment.physician.user)
visit patient_chart_path(appointment.patient)
end
scenario 'valid inputs' do
chart_appointment.add_module('OCT')
.save_module_data('OCT', notes: 'OCT data')
.save_module_data('OCT', notes: 'other OCT data')
.should.have_module_data('OCT', notes: 'other OCT data')
end
end
class ChartAppointmentTestHelper < Capybara::TestHelper
SELECTORS = {
add_module_button: '.appointment-module-add-button',
notes_input: [:field, 'Notes'],
}
delegate_to_test_context :create
def add_module(module_name, sign: nil)
add_module_button.click
accept_alert if sign
click_on module_name
self
end
def save_module_data(module_name, **options)
click_on module_name
notes_input.fill_in with: options[:notes]
click_on 'Save'
self
end
def have_module_data(module_name, **options)
click_on module_name
have(:notes_input, with: options[:notes])
end
def given_there_is_a_scheduled_appointment
create(:appointment_module_type, name: 'OCT')
create('schedule/appointment')
end
end

In practice, once you are using test helpers, interactions can be encapsulated with greater detail, such as clarifying which is the expected link or button that is being clicked when adding or editing a module.

A common example is to use a FormTestHelper:

def save_module_data(module_name, with:)
  click_on module_name
  form.input_for(:notes).type_in(with[:notes])
  form.save
  self
end

That helper could deal with finding the input using a qa attribute that won't change if the label for the field changes, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment