-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug/11 ensure loaded raises on false (#18)
* Extract some logic from Section onto a module * PreconditionNotMetError inherits from Tabasco::Error * ensure_loaded behavior raises when proc returns falsy values * ensure_loaded always verify the container can be found In order to make our framework even safer to use, we want to make it harder to bypass the verification that the container can be found in the DOM. So even if you provide an ensure_loaded block, we always verify your container can be found.
- Loading branch information
Showing
7 changed files
with
148 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module Tabasco | ||
class Section | ||
module EnsureLoaded | ||
def self.included(base) | ||
base.extend(ClassMethods) | ||
end | ||
|
||
module ClassMethods | ||
def ensure_loaded(&block) | ||
@ensure_loaded_block = block | ||
end | ||
|
||
def ensure_loaded_block | ||
@ensure_loaded_block | ||
end | ||
end | ||
|
||
# Instance method to call the stored block | ||
def ensure_loaded | ||
unless self.class.ensure_loaded_block | ||
raise "Page and section objects must define how to check whether their " \ | ||
"content has loaded with the ensure_loaded { ... } DSL method." | ||
|
||
end | ||
|
||
return if instance_exec(&self.class.ensure_loaded_block) && container | ||
|
||
raise PreconditionNotMetError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Oyster Tabasco Testing Demo Page</title> | ||
</head> | ||
<body> | ||
<header data-testid="section-container"> | ||
<h1>Welcome to the Testing Demo Page</h1> | ||
<p>This page is for system testing purposes.</p> | ||
|
||
<div data-testid="lorem">Lorem</div> | ||
<div data-testid="ipsum"> | ||
Ipsum: | ||
<div data-testid="dolor">Dolor</div> | ||
</div> | ||
</header> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Tabasco::Section::EnsureLoaded do | ||
let(:dummy_class) do | ||
Class.new do | ||
include Tabasco::Section::EnsureLoaded | ||
|
||
def initialize(loaded, container = nil) | ||
@loaded = loaded | ||
@container = container | ||
end | ||
|
||
def loaded? | ||
@loaded | ||
end | ||
|
||
attr_reader :container | ||
end | ||
end | ||
|
||
describe ".ensure_loaded" do | ||
it "stores the block to be used for checking if content is loaded" do | ||
dummy_class.ensure_loaded { loaded? } | ||
expect(dummy_class.ensure_loaded_block).to be_a(Proc) | ||
end | ||
end | ||
|
||
describe "#ensure_loaded" do | ||
context "when ensure_loaded block is defined" do | ||
before do | ||
dummy_class.ensure_loaded { loaded? } | ||
end | ||
|
||
it "does not raise an error if the block evaluates to true and the container is found" do | ||
instance = dummy_class.new(true, double("dummy container")) | ||
expect { instance.ensure_loaded }.not_to raise_error | ||
end | ||
|
||
it "raises PreconditionNotMetError if the block evaluates to false" do | ||
instance = dummy_class.new(false, double("dummy container")) | ||
expect { instance.ensure_loaded }.to raise_error(Tabasco::PreconditionNotMetError) | ||
end | ||
|
||
it "raises PreconditionNotMetError if the block evaluates to true but the container is not found" do | ||
instance = dummy_class.new(false, nil) | ||
expect { instance.ensure_loaded }.to raise_error(Tabasco::PreconditionNotMetError) | ||
end | ||
end | ||
|
||
context "when ensure_loaded block is not defined" do | ||
it "raises an error indicating the block must be defined" do | ||
instance = dummy_class.new(true, double("dummy container")) | ||
expect do | ||
instance.ensure_loaded | ||
end.to raise_error(RuntimeError, /must define how to check whether their content has loaded/) | ||
end | ||
end | ||
|
||
context "when ensure_loaded block raises an error" do | ||
it "propagates the error" do | ||
dummy_class.ensure_loaded { raise "Some error" } | ||
instance = dummy_class.new(true, double("dummy container")) | ||
expect { instance.ensure_loaded }.to raise_error(RuntimeError, "Some error") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters