Sha256: 8aeb5e2e684d9649681ff89c546d01e3513e319e29ce8be9f7ccd6bb73c4bdb7

Contents?: true

Size: 1.79 KB

Versions: 28

Compression:

Stored size: 1.79 KB

Contents

require 'spec_helper'

describe '`Opal.hash`' do
  it 'converts object literals to hashes' do
    `Opal.hash({a: 1, b: 2})`.should == {a: 1, b: 2}
  end
end

describe 'javascript access using .JS' do
  it 'should call javascript methods via .JS.method()' do
    "a1234b5678c".JS.indexOf('c').should == 10
    "a1234b5678c".JS.replace(`/[0-9]/g`, '').should == 'abc'
  end

  it 'should call javascript methods via .JS.method arg' do
    ("a1234b5678c".JS.indexOf 'c').should == 10
    ("a1234b5678c".JS.replace `/[0-9]/g`, '').should == 'abc'
  end

  it 'should call javascript methods with blocks via .JS.method' do
    f = `{a:function(f){return f(1) + f(2)}}`
    f.JS.a{|v| v*2}.should == 6
    v = f.JS.a do |v| v*2 end
    v.should == 6
  end

  it 'should call javascript methods with args and blocks via .JS.method' do
    f = `{a:function(a, f){return f(a, 1) + f(a, 2)}}`
    f.JS.a(3){|b, v| b+v*2}.should == 12
    v = f.JS.a 3 do |b, v| b+v*2 end
    v.should == 12
  end

  it 'should support javascript properties via .JS[]' do
    "a1234b5678c".JS['length'].should == 11
    `{a:1}`.JS['a'].should == 1
    `{a:1}`.JS['a'].should == 1
    [2, 4].JS[0].should == 2
    [2, 4].JS[1].should == 4
    [2, 4].JS[:length].should == 2
  end

  it 'should be chainable' do
    "a1234b5678c".JS.replace(`/[0-9]/g`, '').JS.toUpperCase.should == 'ABC'
    "a1234b5678c".JS.replace(`/[0-9]/g`, '').JS[:length].should == 3
    "a1234b5678c".JS[:length].JS.toString.should == "11"
    `{a:{b:1}}`.JS[:a].JS[:b].JS.toString.should == '1'
    f = `{a:function(f){return {b: function(f2){return f(f2(1)) + f(f2(2))}}}}`
    f.JS.a{|v| v*2}.JS.b{|v| v*3}.should == 18
  end

  it 'should set javascript properties via .JS[arg] = rhs' do
    a = `{}`
    a.JS[:foo] = 1
    a.JS[:foo].should == 1
    `a["foo"]`.should == 1
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
opal-1.7.4 spec/opal/core/runtime_spec.rb
opal-1.7.3 spec/opal/core/runtime_spec.rb
opal-1.7.2 spec/opal/core/runtime_spec.rb
opal-1.7.1 spec/opal/core/runtime_spec.rb
opal-1.7.0 spec/opal/core/runtime_spec.rb
opal-1.7.0.rc1 spec/opal/core/runtime_spec.rb
opal-1.6.1 spec/opal/core/runtime_spec.rb
opal-1.6.0 spec/opal/core/runtime_spec.rb
opal-1.6.0.rc1 spec/opal/core/runtime_spec.rb
opal-1.6.0.alpha1 spec/opal/core/runtime_spec.rb
opal-1.5.1 spec/opal/core/runtime_spec.rb
opal-1.5.0 spec/opal/core/runtime_spec.rb
opal-1.5.0.rc1 spec/opal/core/runtime_spec.rb
opal-1.4.1 spec/opal/core/runtime_spec.rb
opal-1.4.0 spec/opal/core/runtime_spec.rb
opal-1.4.0.alpha1 spec/opal/core/runtime_spec.rb
opal-1.3.2 spec/opal/core/runtime_spec.rb
opal-1.3.1 spec/opal/core/runtime_spec.rb
opal-1.3.0 spec/opal/core/runtime_spec.rb
opal-1.3.0.rc1 spec/opal/core/runtime_spec.rb