|
| 1 | +require 'spec_helper' |
| 2 | +require 'test_components' |
| 3 | +require 'rspec-steps' |
| 4 | + |
| 5 | +describe "one to one relationships", js: true do |
| 6 | + |
| 7 | + before(:all) do |
| 8 | + require 'pusher' |
| 9 | + require 'pusher-fake' |
| 10 | + Pusher.app_id = "MY_TEST_ID" |
| 11 | + Pusher.key = "MY_TEST_KEY" |
| 12 | + Pusher.secret = "MY_TEST_SECRET" |
| 13 | + require "pusher-fake/support/base" |
| 14 | + |
| 15 | + Hyperstack.configuration do |config| |
| 16 | + config.transport = :pusher |
| 17 | + config.channel_prefix = "synchromesh" |
| 18 | + config.opts = {app_id: Pusher.app_id, key: Pusher.key, secret: Pusher.secret}.merge(PusherFake.configuration.web_options) |
| 19 | + end |
| 20 | + |
| 21 | + class ActiveRecord::Base |
| 22 | + class << self |
| 23 | + def public_columns_hash |
| 24 | + @public_columns_hash ||= {} |
| 25 | + end |
| 26 | + end |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + before(:each) do |
| 31 | + |
| 32 | + # spec_helper resets the policy system after each test so we have to setup |
| 33 | + # before each test |
| 34 | + stub_const 'TestApplicationPolicy', Class.new |
| 35 | + TestApplicationPolicy.class_eval do |
| 36 | + always_allow_connection |
| 37 | + regulate_all_broadcasts { |policy| policy.send_all } |
| 38 | + allow_change(to: :all, on: [:create, :update, :destroy]) { true } |
| 39 | + end |
| 40 | + |
| 41 | + size_window(:small, :large) |
| 42 | + |
| 43 | + isomorphic do |
| 44 | + |
| 45 | + class Parent < ActiveRecord::Base |
| 46 | + def self.build_tables |
| 47 | + connection.create_table :parents, force: true do |t| |
| 48 | + t.string :name |
| 49 | + t.timestamps |
| 50 | + end |
| 51 | + ActiveRecord::Base.public_columns_hash[name] = columns_hash |
| 52 | + end |
| 53 | + |
| 54 | + has_one :child |
| 55 | + end |
| 56 | + |
| 57 | + class Child < ActiveRecord::Base |
| 58 | + def self.build_tables |
| 59 | + connection.create_table :children, force: true do |t| |
| 60 | + t.string :name |
| 61 | + t.integer :parent_id |
| 62 | + t.timestamps |
| 63 | + end |
| 64 | + ActiveRecord::Base.public_columns_hash[name] = columns_hash |
| 65 | + end |
| 66 | + belongs_to :parent |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + [Parent, Child].each { |klass| klass.build_tables } |
| 71 | + |
| 72 | + end |
| 73 | + |
| 74 | + def compare_to_server(model, expression, expected_result, load=true) |
| 75 | + server_side = eval("#{model.class}.find(#{model.id}).#{expression}") |
| 76 | + expect(server_side).to eq(expected_result) |
| 77 | + be_expected_result = expected_result.is_a?(Array) ? contain_exactly(*expected_result) : eq(expected_result) |
| 78 | + if load |
| 79 | + expect_promise("Hyperstack::Model.load { #{model.class}.find(#{model.id}).#{expression} }") |
| 80 | + .to be_expected_result |
| 81 | + else |
| 82 | + wait_for_ajax |
| 83 | + expect_evaluate_ruby("#{model.class}.find(#{model.id}).#{expression}").to be_expected_result |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + before(:each) do |
| 88 | + @parent1 = Parent.create(name: 'parent1') |
| 89 | + @child1 = Child.create(name: 'child1', parent: @parent1) |
| 90 | + end |
| 91 | + |
| 92 | + it 'will load the parent' do |
| 93 | + compare_to_server @child1, 'parent.name', 'parent1' |
| 94 | + end |
| 95 | + |
| 96 | + it 'will load the child' do |
| 97 | + compare_to_server @parent1, 'child.name', 'child1' |
| 98 | + end |
| 99 | +end |
0 commit comments