mercredi 27 novembre 2019

Jest + React + Rails: how to create objects in database for testing?

I have a Rails app with React front. I would like to use Jest for feature testing because the rest of people working on this project specialize in React. Front and back contact with each other through API.

We have a page which lists a number of phone codes. These codes exist in database. I want to test it if index page is actually rendering phone codes.

If I was using RSpec + Capybara, I would write a feature test like this:

before do
  phone_code = PhoneCode.create(prefix: 495, description: 'Moscow')
  another_code = PhoneCode.create(prefix: 8692, description: 'Sevastopol')
end

describe 'index' do
  it 'lists all phone codes in database' do
    visit 'api/v1/phone_codes'
    expect(page.body).to include(phone_code.description)
    expect(page.body).to include(another_code.description)
  end
end

However, I don't know how to create things in database in React and its test frameworks; except, maybe, through creation form, which doesn't exist in our app.

So, what should I do? Is there a way to create object from Jest? Or I should just make everybody use Capybara?

Aucun commentaire:

Enregistrer un commentaire