bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 115 forks source link

RSpec View Testing for Datagrid #256

Closed jonmchan closed 5 years ago

jonmchan commented 5 years ago

I do a rails scaffold and it generates the standard rspec view tests:

    render
    assert_select "tr>td", :text => "Email".to_s, :count => 2
    assert_select "tr>td", :text => "First Name".to_s, :count => 2
    assert_select "tr>td", :text => "Last Name".to_s, :count => 2
...
    assert_select "tr>td", :text => "Company".to_s, :count => 2
  end

I was thinking about what the best way to do view specs with datagrid. Any best practices?

bogdan commented 5 years ago

I generally don't test views. Especially with the default setup that is only one line of code. The test above makes sense for datagrid if you are really concerned.

zsan commented 1 year ago

Perhaps

RSpec.describe "contacts/index", type: :view do
  before(:each) do
    create_list(:contact, 11)
  end

  context "admin" do
    before(:each) do
      allow(view).to receive(:current_user).and_return(create(:admin))

      contact_grid = assign(:grid, ContactsGrid.new)
      assign(:pagy, pagy(contact_grid.assets)[0])
      assign(:records, pagy(contact_grid.assets)[1])
      render
    end

    it "renders a list of users with heading and paginations" do
      # do assert
    end
 end

And my index action

 def index
    @grid = ContactsGrid.new(params[:contacts_grid])
    @pagy, @records = pagy(@grid.assets)
    authorize @grid
  end