|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +if opal? |
| 4 | + RSpec.describe React::Test::Session do |
| 5 | + subject { described_class.new } |
| 6 | + before do |
| 7 | + stub_const 'Greeter', Class.new |
| 8 | + Greeter.class_eval do |
| 9 | + include React::Component |
| 10 | + |
| 11 | + params do |
| 12 | + optional :message |
| 13 | + optional :from |
| 14 | + end |
| 15 | + |
| 16 | + def render |
| 17 | + span { "Hello #{params.message}" } |
| 18 | + end |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + describe '#mount' do |
| 23 | + it 'returns an instance of the mounted component' do |
| 24 | + expect(subject.mount(Greeter)).to be_a(Greeter) |
| 25 | + end |
| 26 | + |
| 27 | + it 'actualy mounts the component' do |
| 28 | + expect(subject.mount(Greeter)).to be_mounted |
| 29 | + end |
| 30 | + |
| 31 | + it 'optionaly passes params to the component' do |
| 32 | + component = subject.mount(Greeter, message: 'world') |
| 33 | + expect(component.params.message).to eq('world') |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe '#component' do |
| 38 | + it 'returns the instance of the mounted component' do |
| 39 | + component = subject.mount(Greeter) |
| 40 | + expect(subject.component).to eq(component) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + describe '#element' do |
| 45 | + it 'returns the React::Element for the mounted component' do |
| 46 | + subject.mount(Greeter) |
| 47 | + expect(subject.element).to be_a(React::Element) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + describe '#instance' do |
| 52 | + it 'returns the React native instance of the component' do |
| 53 | + component = subject.mount(Greeter) |
| 54 | + native = component.instance_variable_get('@native') |
| 55 | + expect(subject.instance).to eq(native) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + describe '#update_params' do |
| 60 | + it 'sends new params to the component' do |
| 61 | + component = subject.mount(Greeter, message: 'world') |
| 62 | + subject.update_params(message: 'moon') |
| 63 | + expect(component.params.message).to eq('moon') |
| 64 | + end |
| 65 | + |
| 66 | + it 'leaves unspecified params in tact' do |
| 67 | + component = subject.mount(Greeter, message: 'world', from: 'outerspace') |
| 68 | + subject.update_params(message: 'moon') |
| 69 | + expect(component.params.from).to eq('outerspace') |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | +end |
0 commit comments