Skip to content

Commit

Permalink
✅ Add proper tests & coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Robdel12 committed May 4, 2021
1 parent ae9a1cd commit 1f6d6e6
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ gem 'guard-rspec', require: false

group :test, :development do
gem 'pry'
gem 'puma'
gem 'webmock'
gem 'simplecov', require: false
end
13 changes: 6 additions & 7 deletions lib/percy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module PercyCapybara
include Capybara::DSL

CLIENT_INFO = "percy-capybara/#{VERSION}".freeze
ENV_INFO = "selenium/#{Capybara::VERSION} ruby/#{RUBY_VERSION}".freeze
ENV_INFO = "capybara/#{Capybara::VERSION} ruby/#{RUBY_VERSION}".freeze

PERCY_DEBUG = ENV['PERCY_LOGLEVEL'] == 'debug'
PERCY_SERVER_ADDRESS = ENV['PERCY_SERVER_ADDRESS'] || 'http://localhost:5338'
Expand Down Expand Up @@ -38,14 +38,13 @@ def percy_snapshot(name, options = {})
end
end

def _clear_cache!
def __percy_clear_cache!
@percy_dom = nil
@percy_enabled = nil
end

private
# Determine if the Percy server is running, caching the result so it is only checked once
def percy_enabled?
private def percy_enabled?
return @percy_enabled unless @percy_enabled.nil?

begin
Expand Down Expand Up @@ -79,20 +78,20 @@ def percy_enabled?
end

# Fetch the @percy/dom script, caching the result so it is only fetched once
def fetch_percy_dom
private def fetch_percy_dom
return @percy_dom unless @percy_dom.nil?

response = fetch('percy/dom.js')
@percy_dom = response.body
end

def log(msg)
private def log(msg)
puts "#{LABEL} #{msg}"
end

# Make an HTTP request (GET,POST) using Ruby's Net::HTTP. If `data` is present,
# `fetch` will POST as JSON.
def fetch(url, data = nil)
private def fetch(url, data = nil)
uri = URI("#{PERCY_SERVER_ADDRESS}/#{url}")

response = if data
Expand Down
1 change: 1 addition & 0 deletions spec/fixture/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><title>I am a page</title></head><body>Snapshot me</body></html>
101 changes: 101 additions & 0 deletions spec/lib/percy/percy_capybara_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
RSpec.describe PercyCapybara, type: :feature do
before(:each) do
WebMock.disable_net_connect!(allow: '127.0.0.1', disallow: 'localhost')
## @TODO hm
page.__percy_clear_cache!
end

describe 'snapshot', type: :feature, js: true do
it 'disables when healthcheck version is incorrect' do
stub_request(:get, "#{PercyCapybara::PERCY_SERVER_ADDRESS}/percy/healthcheck")
.to_return(status: 200, body: '', headers: {'x-percy-core-version': '0.1.0'})

expect { page.percy_snapshot('Name') }
.to output("#{PercyCapybara::LABEL} Unsupported Percy CLI version, 0.1.0\n").to_stdout
end

it 'disables when healthcheck version is missing' do
stub_request(:get, "#{PercyCapybara::PERCY_SERVER_ADDRESS}/percy/healthcheck")
.to_return(status: 200, body: '', headers: {})

expect { page.percy_snapshot('Name') }
.to output(
"#{PercyCapybara::LABEL} You may be using @percy/agent which" \
' is no longer supported by this SDK. Please uninstall' \
' @percy/agent and install @percy/cli instead.' \
" https://docs.percy.io/docs/migrating-to-percy-cli\n",
).to_stdout
end

it 'disables when healthcheck fails' do
stub_request(:get, "#{PercyCapybara::PERCY_SERVER_ADDRESS}/percy/healthcheck")
.to_return(status: 500, body: '', headers: {})

expect { page.percy_snapshot('Name') }
.to output("#{PercyCapybara::LABEL} Percy is not running, disabling snapshots\n").to_stdout
end

it 'disables when healthcheck fails to connect' do
stub_request(:get, "#{PercyCapybara::PERCY_SERVER_ADDRESS}/percy/healthcheck")
.to_raise(StandardError)

expect { page.percy_snapshot('Name') }
.to output("#{PercyCapybara::LABEL} Percy is not running, disabling snapshots\n").to_stdout
end

it 'throws an error when name is not provided' do
stub_request(:get, "#{PercyCapybara::PERCY_SERVER_ADDRESS}/percy/healthcheck")
.to_return(status: 500, body: '', headers: {})

expect { page.percy_snapshot() }.to raise_error(ArgumentError)
end

it 'logs an error when sending a snapshot fails' do
stub_request(:get, "#{PercyCapybara::PERCY_SERVER_ADDRESS}/percy/healthcheck")
.to_return(status: 200, body: '', headers: {'x-percy-core-version': '1.0.0'})

stub_request(:get, "#{PercyCapybara::PERCY_SERVER_ADDRESS}/percy/dom.js")
.to_return(
status: 200,
body: 'window.PercyDOM = { serialize: () => document.documentElement.outerHTML };',
headers: {},
)

stub_request(:post, 'http://localhost:5338/percy/snapshot')
.to_return(status: 200, body: '', headers: {})

expect { page.percy_snapshot('Name') }
.to output("#{PercyCapybara::LABEL} Could not take DOM snapshot 'Name'\n").to_stdout
end

it 'sends snapshots to the local server' do
stub_request(:get, "#{PercyCapybara::PERCY_SERVER_ADDRESS}/percy/healthcheck")
.to_return(status: 200, body: '', headers: {'x-percy-core-version': '1.0.0'})

stub_request(:get, "#{PercyCapybara::PERCY_SERVER_ADDRESS}/percy/dom.js")
.to_return(
status: 200,
body: 'window.PercyDOM = { serialize: () => document.documentElement.outerHTML };',
headers: {},
)

stub_request(:post, 'http://localhost:5338/percy/snapshot')
.to_return(status: 200, body: '{"success": "true" }', headers: {})

visit 'index.html'
page.percy_snapshot('Name')

expect(WebMock).to have_requested(:post, "#{PercyCapybara::PERCY_SERVER_ADDRESS}/percy/snapshot")
.with(
body: {
name: 'Name',
url: 'http://127.0.0.1:3003/index.html',
dom_snapshot:
"<html><head><title>I am a page</title></head><body>Snapshot me\n</body></html>",
client_info: "percy-capybara/#{PercyCapybara::VERSION}",
environment_info: "capybara/#{Capybara::VERSION} ruby/#{RUBY_VERSION}",
}.to_json,
).once
end
end
end
10 changes: 0 additions & 10 deletions spec/lib/percy/percy_spec.rb

This file was deleted.

11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# This must be required & started before any app code (for proper coverage)
require 'simplecov'
SimpleCov.start
SimpleCov.minimum_coverage 100

require 'capybara/rspec'
require 'webmock/rspec'
require 'percy'

RSpec.configure do |config|
Expand Down Expand Up @@ -29,4 +35,9 @@
# See https://github.com/teamcapybara/capybara#selecting-the-driver for other options
Capybara.default_driver = :selenium_headless
Capybara.javascript_driver = :selenium_headless

# Setup for Capybara to test Jekyll static files served by Rack
Capybara.server_port = 3003
Capybara.server = :puma, { Silent: true }
Capybara.app = Rack::File.new(File.join(File.dirname(__FILE__), 'fixture'))
end

0 comments on commit 1f6d6e6

Please sign in to comment.