Add implementation for matching view elements in QA

This commit is contained in:
Grzegorz Bizon 2017-12-22 15:22:54 +01:00
parent b51ba96e4d
commit 481f461380
5 changed files with 116 additions and 0 deletions

View File

@ -48,6 +48,10 @@ module QA
@evaluator ||= Page::Base::DSL.new
end
def self.validator
Page::Validator.new(self)
end
class DSL
attr_reader :views

View File

@ -7,6 +7,18 @@ module QA
@name = name
@pattern = pattern
end
def expression?
@pattern.is_a?(Regexp)
end
def matches?(line)
if expression?
line =~ pattern
else
line.includes?(pattern)
end
end
end
end
end

18
qa/qa/page/validator.rb Normal file
View File

@ -0,0 +1,18 @@
module QA
module Page
class Validator
def initialize(page)
@page = page
@views = page.views
end
def errors
@errors ||= @views.map do |view|
end
end
def message
end
end
end
end

View File

@ -8,6 +8,27 @@ module QA
@elements = elements
end
def pathname
Pathname.new(File.join( __dir__, '../../../', @path))
.cleanpath.expand_path
end
def errors
##
# Reduce required elements by streaming views and making assertions on
# elements' patterns.
#
@missing ||= @elements.dup.tap do |elements|
File.new(pathname.to_s).foreach do |line|
elements.reject! { |element| element.matches?(line) }
end
end
@missing.map do |missing|
"Missing element `#{missing}` in `#{pathname}` view partial!"
end
end
def self.evaluate(&block)
Page::View::DSL.new.tap do |evaluator|
evaluator.instance_exec(&block)

61
qa/spec/page/view_spec.rb Normal file
View File

@ -0,0 +1,61 @@
describe QA::Page::View do
let(:element) do
double('element', name: :something, pattern: /some element/)
end
subject { described_class.new('some/file.html', [element]) }
describe '.evaluate' do
it 'evaluates a block and returns a DSL object' do
results = described_class.evaluate do
element :something, 'my pattern'
element :something_else, /another pattern/
end
expect(results.elements.size).to eq 2
end
end
describe '#pathname' do
it 'returns an absolute and clean path to the view' do
expect(subject.pathname.to_s).not_to include 'qa/page/'
expect(subject.pathname.to_s).to include 'some/file.html'
end
end
describe '#errors' do
let(:file) { spy('file') }
before do
allow(File).to receive(:new).and_return(file)
end
context 'when pattern is found' do
before do
allow(file).to receive(:foreach)
.and_yield('some element').once
allow(element).to receive(:matches?)
.with('some element').and_return(true)
end
it 'walks through the view and asserts on elements existence' do
expect(subject.errors).to be_empty
end
end
context 'when pattern has not been found' do
before do
allow(file).to receive(:foreach)
.and_yield('some element').once
allow(element).to receive(:matches?)
.with('some element').and_return(false)
end
it 'returns an array of errors related to missing elements' do
expect(subject.errors).not_to be_empty
expect(subject.errors.first)
.to match %r(Missing element `.*` in `.*/some/file.html` view)
end
end
end
end