No commit activity in last 3 years
No release in over 3 years
An RSpec matcher that tests the text order on the page.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
~> 2.15
>= 0
~> 10.0
~> 3.0
 Project Readme

rspec-text-order

Build Status

A set of RSpec matchers for detecting the position of parts of text relative to other parts.

Installation

Add the gem to your Gemfile:

gem 'rspec-text-order', '~> 0.1'

Usage

include_text

The simplest matcher is include_text. It is mostly just a duplicate of RSpec's built-in include matcher, with the added benefit of additional chainable matchers.

RSpec.describe 'text order matchers' do
  it 'asserts subtext position relative to other subtext' do
    actual = 'first middle last'
    expect(actual).to include_text('middle')
    expect(actual).to include_text('middle').before('last')
    expect(actual).to include_text('middle').after('first')
    expect(actual).to include_text('middle').after('first').before('last')
  end
end

Capybara

A common use case for this type of matcher is to ensure text is presented in a certain order in an acceptance test. For that reason, there are also a couple of shortcut matchers included for convenience.

# this is a capybara-enabled acceptance test
RSpec.describe 'name page', type: :feature do
  it 'displays names in alphabetical order' do
    create_person('Charlie')
    create_person('Barbara')
    create_person('Anne')

    visit '/'

    expect('Barbara').to appear_before('Charlie')
    expect('Barbara').to appear_after('Anne')
  end
end

This is roughly equivalent to:

expect(page.text).to include_text('Barbara').before('Charlie')
expect(page.text).to include_text('Barbara').after('Anne')

Attribution

Thank you https://github.com/phillipgray for inspiring this project with his contributions to an app we worked on together ❤️