Skip to content

Commit

Permalink
Refactor ScreenshotTaker to improve clipboard functionality and add s…
Browse files Browse the repository at this point in the history
…pecs for its behavior
  • Loading branch information
muriloime committed Feb 22, 2025
1 parent 1400543 commit f2f01e9
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/lookbook_visual_tester/screenshot_taker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ def call
# nil
# end
if path == CLIPBOARD
save_to_clipboard
print_and_save_to_clipboard
else
save_printscreen
end
# Additional wait for any JavaScript animations
sleep 1
rescue StandardError => e
logger.puts "Error capturing screenshot for #{preview_url}: #{e.message}"
logger.info "Error capturing screenshot for #{preview_url}: #{e.message}"
raise e
end

Expand Down
96 changes: 96 additions & 0 deletions spec/lib/lookbook_visual_tester/screenshot_taker_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require 'spec_helper'
require 'lookbook_visual_tester/screenshot_taker'
require 'tempfile'

RSpec.describe LookbookVisualTester::ScreenshotTaker do
let(:preview_url) { 'http://localhost:3000/preview' }
let(:path) { '/tmp/test_screenshot.png' }
let(:logger) { instance_double(Logger, info: true, puts: true) }
let(:session) { instance_double(Capybara::Session) }
let(:session_manager) { instance_double(LookbookVisualTester::SessionManager, session:) }
let(:service) { described_class.new(preview_url, path, logger:) }

before do
allow(LookbookVisualTester::SessionManager).to receive(:instance).and_return(session_manager)
allow(session).to receive(:visit)
allow(session).to receive(:save_screenshot)
allow(FileUtils).to receive(:mkdir_p)
allow(service).to receive(:system).and_return(true)
end

describe '#initialize' do
it 'sets the preview_url and path' do
expect(service.preview_url).to eq(preview_url)
expect(service.path).to eq(path)
end

it 'defaults crop to true' do
expect(service.crop).to be true
end

it 'allows crop to be set to false' do
service = described_class.new(preview_url, path, crop: false)
expect(service.crop).to be false
end

it 'defaults path to clipboard' do
service = described_class.new(preview_url)
expect(service.path).to eq('clipboard')
end
end

describe '#session' do
it 'returns the session from SessionManager' do
expect(service.session).to eq(session)
end
end

describe '#call' do
it 'creates directory for screenshot' do
expect(FileUtils).to receive(:mkdir_p).with(File.dirname(path))
service.call
end

it 'visits the preview URL' do
expect(session).to receive(:visit).with(preview_url)
service.call
end

it 'saves screenshot to file when path is provided' do
expect(session).to receive(:save_screenshot).with(path)
service.call
end

it 'crops the screenshot when crop is true' do
expect(service).to receive(:system).with("convert #{path} -trim -bordercolor white -border 10x10 #{path}")
service.call
end

context 'when path is clipboard' do
let(:service) { described_class.new(preview_url, described_class::CLIPBOARD, logger:) }

it 'uses tempfile and copies to clipboard' do
expect(service).to receive(:print_and_save_to_clipboard)
service.call
end
end

context 'when an error occurs' do
before do
allow(session).to receive(:visit).and_raise(StandardError.new('Test error'))
end

it 'logs the error and reraises it' do
expect(logger).to receive(:info).with("Error capturing screenshot for #{preview_url}: Test error")
expect { service.call }.to raise_error(StandardError, 'Test error')
end
end
end

describe '#save_to_clipboard' do
it 'copies image to clipboard using xclip' do
expect(service).to receive(:system).with("xclip -selection clipboard -t image/png -i #{path}")
service.save_to_clipboard
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/lookbook_visual_tester/update_previews_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@
before do
allow(Lookbook).to receive(:previews).and_return([preview])
allow(LookbookVisualTester::ScenarioRun).to receive(:new).with(scenario).and_return(scenario_run)
allow(LookbookVisualTester::ScreenshotTaker).to receive(:new).and_return(double(capture: true))
allow(LookbookVisualTester::ScreenshotTaker).to receive(:new).and_return(double(call: true))
allow(Rails.logger).to receive(:info) # Ensure logger.info is stubbed
end

it 'processes each preview and takes screenshots' do
expect(LookbookVisualTester::ScreenshotTaker).to receive(:new)
expect_any_instance_of(LookbookVisualTester::ScreenshotTaker).to receive(:capture).with('url', 'path')
expect_any_instance_of(LookbookVisualTester::ScreenshotTaker).to receive(:call).with('url', 'path')

# Verify logging
expect(Rails.logger).to receive(:info).with("LookbookVisualTester: previews #{[preview].count}")
Expand Down

0 comments on commit f2f01e9

Please sign in to comment.