Sha256: f405ff91739f2096a973dc8a20c2938f88c4aa2c59c4920be886d82d0bd24c64

Contents?: true

Size: 1.78 KB

Versions: 16

Compression:

Stored size: 1.78 KB

Contents

require 'spec_helper'
require 'js'

describe 'javascript operations using JS module' do
  it 'JS.typeof uses typeof to return underlying javascript type' do
    [1, `null`, `undefined`, Object.new, [], ""].each do |v|
      JS.typeof(v).should == `typeof #{v}`
    end
  end

  it 'JS.new uses new to create new instance' do
    f = `function(){}`
    f.JS[:prototype].JS[:color] = 'black'
    JS.new(f).JS[:color].should == 'black'
  end

  it 'JS.new handles blocks' do
    f = `function(a){this.a = a}`
    JS.new(f){1}.JS.a.should == 1
  end

  it 'JS.instanceof uses instanceof to check if value is an instance of a function' do
    f = `function(){}`
    JS.instanceof(JS.new(f), f).should == true
    JS.instanceof(JS.new(f), `function(){}`).should == false
  end

  it 'JS.delete uses delete to remove properties from objects' do
    f = `{a:1}`
    f.JS[:a].should == 1
    JS.delete(f, :a)
    `#{f.JS[:a]} === undefined`.should == true
  end

  it 'JS.in uses in to check for properties in objects' do
    f = `{a:1}`
    JS.in(:a, f).should == true
    JS.in(:b, f).should == false
  end

  it 'JS.void returns undefined' do
    f = 1
    `#{JS.void(f += 1)} === undefined`.should == true
    f.should == 2
  end

  it 'JS.call calls global javascript methods' do
    JS.call(:parseFloat, '1.0').should == 1
    JS.call(:parseInt, '1').should == 1
    JS.call(:eval, "({a:1})").JS[:a].should == 1
  end

  it 'JS.method_missing calls global javascript methods' do
    JS.parseFloat('1.0').should == 1
    JS.parseInt('1').should == 1
  end

  it 'JS.call calls global javascript methods with blocks' do
    begin
      JS.global.JS[:_test_global_function] = lambda{|pr| pr.call + 1}
      JS._test_global_function{1}.should == 2
    ensure
      JS.delete(JS.global, :_test_global_function)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
opal-0.10.6 spec/opal/stdlib/js_spec.rb
opal-0.10.6.beta spec/opal/stdlib/js_spec.rb
opal-0.10.5 spec/opal/stdlib/js_spec.rb
opal-0.10.4 spec/opal/stdlib/js_spec.rb
opal-0.11.0.rc1 spec/opal/stdlib/js_spec.rb
opal-0.10.3 spec/opal/stdlib/js_spec.rb
opal-0.10.2 spec/opal/stdlib/js_spec.rb
opal-0.10.1 spec/opal/stdlib/js_spec.rb
opal-0.10.0 spec/opal/stdlib/js_spec.rb
opal-0.10.0.rc2 spec/opal/stdlib/js_spec.rb
opal-0.10.0.rc1 spec/opal/stdlib/js_spec.rb
opal-0.10.0.beta5 spec/opal/stdlib/js_spec.rb
opal-0.10.0.beta4 spec/opal/stdlib/js_spec.rb
opal-0.10.0.beta3 spec/opal/stdlib/js_spec.rb
opal-0.10.0.beta2 spec/opal/stdlib/js_spec.rb
opal-0.10.0.beta1 spec/opal/stdlib/js_spec.rb