Skip to content
This repository has been archived by the owner on Sep 30, 2018. It is now read-only.

Commit

Permalink
Initial commit using rspec (opal-rspec)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 20, 2013
1 parent 1fc9329 commit f2371e9
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ gemspec

gem 'opal', :github => 'opal/opal'
gem 'opal-jquery', :github => 'opal/opal-jquery'
gem 'opal-spec', :github => 'opal/opal-spec'
gem 'opal-rspec', :path => '../opal-rspec'
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ require 'bundler'
Bundler.require
require 'opal/activesupport'

require 'opal/spec/rake_task'
Opal::Spec::RakeTask.new(:default)
require 'opal/rspec/rake_task'
Opal::RSpec::RakeTask.new(:default)
4 changes: 4 additions & 0 deletions opal/vienna/record_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def initialize
@records = []
end

def ==(arr)
@records == arr
end

def method_missing(sym, *args, &block)
@records.__send__(sym, *args, &block)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/eventable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def events

obj.off(:foo, handler)
obj.trigger(:foo)
called.should be_false
called.should eq(false)
end
end

Expand Down
4 changes: 0 additions & 4 deletions spec/model/accessing_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ def last_name=(name)
model.first_name = "Adam"
model[:first_name].should eq("Adam")
end

it "raises an exception when accessing an undefined attribute" do
lambda { model[:foo] }.should raise_error(Exception)
end
end

describe "#[]=" do
Expand Down
5 changes: 2 additions & 3 deletions spec/model/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ class ModelAttributeSpec < Vienna::Model
describe ".attribute" do

let(:model) { ModelAttributeSpec.new }
let(:attributes) { model.instance_variable_get(:@attributes) }

it "should create a reader method for attribute" do
model.respond_to?(:first_name).should be_true
model.should respond_to(:first_name)
end

it "should create a writer method for attribute" do
model.respond_to?(:first_name=).should be_true
model.should respond_to(:first_name=)
end

describe "writer" do
Expand Down
4 changes: 2 additions & 2 deletions spec/model/initialize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class ModelInitializeSpec < Vienna::Model
end

it "marks the model as being a new record" do
ModelInitializeSpec.new.new_record?.should be_true
ModelInitializeSpec.new.should be_new_record
end

it "marks the model as not being loaded" do
ModelInitializeSpec.new.loaded?.should be_false
ModelInitializeSpec.new.should_not be_loaded
end
end
end
4 changes: 2 additions & 2 deletions spec/model/load_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

it "marks the model instance as loaded" do
model.load({})
model.loaded?.should be_true
model.should be_loaded
end

it "marks the model as not being a new record" do
model.load({})
model.new_record?.should be_false
model.should_not be_new_record
end
end
end
10 changes: 5 additions & 5 deletions spec/model/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
called = false
model.on(:destroy) { called = true }
model.did_destroy
called.should be_true
called.should eq(true)
end

it "triggers a :destroy event on the class" do
called = false
SimpleModel.on(:destroy) { called = true }
model.did_destroy
called.should be_true
called.should eq(true)
end

it "removes the record from the class identity_map" do
Expand All @@ -32,7 +32,7 @@
describe "#did_create" do
it "sets @new_record to false" do
model.did_create
model.new_record?.should be_false
model.new_record?.should eq(false)
end

it "adds record to class identity_map" do
Expand All @@ -51,14 +51,14 @@
called = false
model.on(:create) { called = true }
model.did_create
called.should be_true
called.should eq(true)
end

it "triggers a :create event on the class" do
called = false
model.class.on(:create) { called = true }
model.did_create
called.should be_true
called.should eq(true)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

describe ".new" do
it "should set @new_record to true" do
SimpleModel.new.new_record?.should be_true
SimpleModel.new.new_record?.should eq(true)
end
end

Expand All @@ -35,7 +35,7 @@

it "should set @new_record to false on the model" do
model = SimpleModel.load(id: 42)
model.new_record?.should be_false
model.new_record?.should eq(false)
end

it "should cache model" do
Expand Down
35 changes: 16 additions & 19 deletions spec/route_spec.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,62 @@
require 'spec_helper'

describe Vienna::Router::Route do
describe "#initialize" do
let(:route) { Vienna::Router::Route }
subject { described_class }

describe "#initialize" do
it "creates a regexp from the given pattern" do
route.new('foo').regexp.should eq(/^foo$/)
subject.new('foo').regexp.should eq(/^foo$/)
end

it "escapes slashes in the pattern" do
route.new('/foo/bar/').regexp.should eq(/^\/foo\/bar\/$/)
subject.new('/foo/bar/').regexp.should eq(/^\/foo\/bar\/$/)
end

it "finds named params in pattern" do
r = route.new('/foo/:bar')
r = subject.new('/foo/:bar')
r.named.should eq(['bar'])

p = route.new('/:woosh/:kapow')
p = subject.new('/:woosh/:kapow')
p.named.should eq(['woosh', 'kapow'])
end

it "finds splatted params in pattern" do
route.new('/foo/*baz').named.should eq(['baz'])
subject.new('/foo/*baz').named.should eq(['baz'])
end

it "produces a regexp to match given pattern" do
route.new('/foo').regexp.match('/bar').should be_nil
route.new('/foo').regexp.match('/foo').should be_kind_of(MatchData)
subject.new('/foo').regexp.match('/bar').should be_nil
subject.new('/foo').regexp.match('/foo').should be_kind_of(MatchData)
end
end

describe "#match" do
let(:route) { Vienna::Router::Route }

it "returns false for a non matching route" do
route.new('/foo').match('/a/b/c').should be_false
subject.new('/foo').match('/a/b/c').should eq(false)
end

it "returns true for a matching route" do
route.new('/foo').match('/foo').should be_true
subject.new('/foo').match('/foo').should eq(true)
end

it "calls block given to #initialize on matching a route" do
called = false
route.new('/foo') { called = true }.match('/foo')
called.should be_true
subject.new('/foo') { called = true }.match('/foo')
called.should eq(true)
end

it "calls handler with an empty hash for a simple route" do
route.new('/foo') { |params| params.should eq({}) }.match('/foo')
subject.new('/foo') { |params| params.should eq({}) }.match('/foo')
end

it "returns a hash of named params for matching route" do
route.new('/foo/:first') { |params|
subject.new('/foo/:first') { |params|
params.should eq({'first' => '123' })
}.match('/foo/123')

route.new('/:first/:second') { |params|
subject.new('/:first/:second') { |params|
params.should eq({ 'first' => 'woosh', 'second' => 'kapow' })
}.match('/woosh/kapow')
end
end
end

66 changes: 32 additions & 34 deletions spec/router_spec.rb
Original file line number Diff line number Diff line change
@@ -1,104 +1,102 @@
require 'spec_helper'

describe Vienna::Router do
let(:router) { Vienna::Router.new }

describe "#update" do
it "should update Router.path" do
$global.location.hash = "#/hello/world"
router.update
subject.update

router.path.should eq('/hello/world')
subject.path.should eq('/hello/world')
end

it "calls #match with the new @path" do
$global.location.hash = "#/foo/bar"
called = nil

router.define_singleton_method(:match) { |m| called = m }
subject.define_singleton_method(:match) { |m| called = m }
called.should be_nil

router.update
subject.update
called.should eq("/foo/bar")
end
end

describe "#route" do
it "should add a route" do
router.route('/users') {}
router.routes.size.should eq(1)
subject.route('/users') {}
subject.routes.size.should eq(1)

router.route('/hosts') {}
router.routes.size.should eq(2)
subject.route('/hosts') {}
subject.routes.size.should eq(2)
end
end

describe "#match" do
it "returns nil when no routes on router" do
router.match('/foo').should be_nil
subject.match('/foo').should be_nil
end

it "returns a matching route for the path" do
a = router.route('/foo') {}
b = router.route('/bar') {}
a = subject.route('/foo') {}
b = subject.route('/bar') {}

router.match('/foo').should eq(a)
router.match('/bar').should eq(b)
subject.match('/foo').should eq(a)
subject.match('/bar').should eq(b)
end

it "returns nil when there are no matching routes" do
router.route('/woosh') {}
router.route('/kapow') {}
subject.route('/woosh') {}
subject.route('/kapow') {}

router.match('/ping').should be_nil
subject.match('/ping').should be_nil
end

it "calls handler of matching route" do
out = []
router.route('/foo') { out << :foo }
router.route('/bar') { out << :bar }
subject.route('/foo') { out << :foo }
subject.route('/bar') { out << :bar }

router.match('/foo')
subject.match('/foo')
out.should eq([:foo])

router.match('/bar')
subject.match('/bar')
out.should eq([:foo, :bar])

router.match('/eek')
subject.match('/eek')
out.should eq([:foo, :bar])
end

it "works with / too" do
out = []
router.route('/') { out << :index }
subject.route('/') { out << :index }

$global.location.hash = ""
router.update
subject.update

$global.location.hash = "#/"
router.update
subject.update

out.should == [:index, :index]
end
end

describe "#navigate" do
it "should update location.hash" do
router.navigate "foo"
subject.navigate "foo"
$global.location.hash.should eq("#foo")
end

it "triggers the route matchers" do
called = false
router.route("/foo") { called = true }
subject.route("/foo") { called = true }

router.navigate("/bar")
router.update
called.should be_false
subject.navigate("/bar")
subject.update
called.should eq(false)

router.navigate("/foo")
router.update
called.should be_true
subject.navigate("/foo")
subject.update
called.should eq(true)
end
end
end
Expand Down
Loading

0 comments on commit f2371e9

Please sign in to comment.