kzkn / gretel

Flexible Ruby on Rails breadcrumbs plugin.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Subtle test order dependency when mixing Gretel breadcrumbs with TrailBlazer Cells

MrShemek opened this issue · comments

Leaving this here for anyone that ends up encountering the same test order dependency failures I did.

TL;DR: Add Gretel::Crumbs.reload_if_needed at the beginning of your cell test case that uses breadcrumbs(...) to ensure the "crumbs" are always loaded. (see code at bottom)

I have my application layout broken up into cells, one particular cell view is used to render my breadcrumbs which works fine in production code. We always show the :root crumb on all pages so in my unit tests for the ApplicationLayoutCell I explicitly check for a breadcrumbs HTML node to exist. However, due to some semantics inside Gretel::Renderer#links the breadcrumbs are not loaded because the instance variable @breadcrumb_key inside the Gretel::Renderer is nil.

Because the "crumbs" are not loaded Gretel::Crumbs.crumb_defined?(:root) returns false which causes no "root" link to be added in Gretel::Renderer#links_for_render

Relevant portion of Gretel::Renderer#links_for_render

      ...
      # Handle autoroot
      if options[:autoroot] && out.map(&:key).exclude?(:root) && Gretel::Crumbs.crumb_defined?(:root)
        out.unshift *Gretel::Crumb.new(context, :root).links
      end
      ...

The contents of my ApplicationLayoutCell#breadcrumbs view

<%= breadcrumbs(display_single_fragment: true, separator: '&nbsp;&nbsp;›&nbsp;&nbsp;').to_s %>

The contents of the relevant test case in ApplicationLayoutCellTest

  test 'header' do
    Gretel::Crumbs.reload_if_needed
    html = parse_html(@cell.call(:header))
    #
    assert html.css('form.site-search-wrapper').present?, 'site-wide search form should exist'
    assert html.css('nav.nav').present?, 'a nav element should appear in the header'
    assert html.css('.breadcrumbs').present?, 'breadcrumbs should get added by the header'
  end

Original issue: #99