|
1 | 1 | require 'spec_helper' |
2 | | -#Opal::RSpec::Runner.autorun |
3 | 2 |
|
4 | | -class BaseClass < ActiveRecord::Base |
5 | | -end |
6 | | - |
7 | | -class SubClass < BaseClass |
8 | | -end |
9 | | - |
10 | | -class Funky < ActiveRecord::Base |
11 | | - self.primary_key = :funky_id |
12 | | - self.inheritance_column = :funky_type |
13 | | -end |
14 | | - |
15 | | -class BelongsTo < ActiveRecord::Base |
16 | | - belongs_to :has_many |
17 | | - belongs_to :has_one |
18 | | - belongs_to :best_friend, class_name: "HasMany", foreign_key: :bf_id |
19 | | -end |
20 | | - |
21 | | -class HasMany < ActiveRecord::Base |
22 | | - has_many :belongs_to |
23 | | - has_many :best_friends, class_name: "BelongsTo", foreign_key: :bf_id |
24 | | -end |
25 | | - |
26 | | -class HasOne < ActiveRecord::Base |
27 | | - has_one :belongs_to |
28 | | -end |
29 | | - |
30 | | -class Scoped < ActiveRecord::Base |
31 | | - scope :only_those_guys, -> () {} |
32 | | -end |
| 3 | +# this never completed conversion from old react.rb days... |
| 4 | +# all this content is tested elsewhere, but it would be nice |
| 5 | +# to have these basic unit tests executing as well. |
33 | 6 |
|
34 | | -describe "ActiveRecord" do |
| 7 | +describe 'ActiveRecord client side basics', js: true do |
35 | 8 |
|
36 | | - before(:all) { Hyperstack::Component::IsomorphicHelpers.load_context } |
37 | | - |
38 | | - after(:each) { React::API.clear_component_class_cache } |
39 | | - |
40 | | - # uncomment if you are having trouble with tests failing. One non-async test must pass for things to work |
41 | | - |
42 | | - # describe "a passing dummy test" do |
43 | | - # it "passes" do |
44 | | - # expect(true).to be(true) |
45 | | - # end |
46 | | - # end |
47 | | - |
48 | | - describe "reactive_record active_record base methods" do |
49 | | - |
50 | | - it "will find the base class" do |
51 | | - expect(SubClass.base_class).to eq(BaseClass) |
| 9 | + before(:all) do |
| 10 | + class ActiveRecord::Base |
| 11 | + class << self |
| 12 | + def public_columns_hash |
| 13 | + @public_columns_hash ||= {} |
| 14 | + end |
| 15 | + end |
52 | 16 | end |
53 | 17 |
|
54 | | - it "knows the primary key" do |
55 | | - expect(BaseClass.primary_key).to eq(:id) |
| 18 | + class BaseClass < ActiveRecord::Base |
56 | 19 | end |
57 | 20 |
|
58 | | - it "can override the primary key" do |
59 | | - expect(Funky.primary_key).to eq(:funky_id) |
| 21 | + class SubClass < BaseClass |
60 | 22 | end |
61 | 23 |
|
62 | | - it "knows the inheritance column" do |
63 | | - expect(BaseClass.inheritance_column).to eq(:type) |
| 24 | + class Funky < ActiveRecord::Base |
| 25 | + self.primary_key = :funky_id |
| 26 | + self.inheritance_column = :funky_type |
64 | 27 | end |
65 | 28 |
|
66 | | - it "can override the inheritance column" do |
67 | | - expect(Funky.inheritance_column).to eq(:funky_type) |
| 29 | + class BelongsTo < ActiveRecord::Base |
| 30 | + belongs_to :has_many |
| 31 | + belongs_to :has_one |
| 32 | + belongs_to :best_friend, class_name: "HasMany", foreign_key: :bf_id |
68 | 33 | end |
69 | 34 |
|
70 | | - it "knows the model name" do |
71 | | - expect(BaseClass.model_name).to eq("BaseClass") |
| 35 | + class HasMany < ActiveRecord::Base |
| 36 | + has_many :belongs_to |
| 37 | + has_many :best_friends, class_name: "BelongsTo", foreign_key: :bf_id |
72 | 38 | end |
73 | 39 |
|
74 | | - it "can find a record by id" do |
75 | | - expect(BaseClass.find(12).id).to eq(12) |
| 40 | + class HasOne < ActiveRecord::Base |
| 41 | + has_one :belongs_to |
76 | 42 | end |
77 | 43 |
|
78 | | - it "has a find_by_xxx method" do |
79 | | - expect(BaseClass.find_by_xxx("beer").xxx).to eq("beer") |
| 44 | + class Scoped < ActiveRecord::Base |
| 45 | + scope :only_those_guys, -> () {} |
80 | 46 | end |
81 | 47 |
|
82 | | - it "will correctly infer the model type from the inheritance column" do |
83 | | - expect(BaseClass.find_by_type("SubClass").class).to eq(SubClass) |
84 | | - expect(BaseClass.find_by_type(nil).class).to eq(BaseClass) |
85 | | - end |
| 48 | + end |
86 | 49 |
|
87 | | - it "can have a has_many association" do |
88 | | - expect(HasMany.reflect_on_association(:belongs_to).klass.reflect_on_association(:has_many).klass).to eq(HasMany) |
89 | | - end |
| 50 | + it "will find the base class" do |
| 51 | + expect(SubClass.base_class).to eq(BaseClass) |
| 52 | + end |
90 | 53 |
|
91 | | - it "can have a has_one association" do |
92 | | - expect(HasOne.reflect_on_association(:belongs_to).klass.reflect_on_association(:has_one).klass).to eq(HasOne) |
93 | | - end |
| 54 | + it "knows the primary key" do |
| 55 | + expect(BaseClass.primary_key).to eq(:id) |
| 56 | + end |
94 | 57 |
|
95 | | - it "can override the class and foreign_key values when creating an association" do |
96 | | - reflection = HasMany.reflect_on_association(:best_friends) |
97 | | - expect(reflection.klass).to eq(BelongsTo) |
98 | | - expect(reflection.association_foreign_key).to eq(:bf_id) |
99 | | - end |
| 58 | + it "can override the primary key" do |
| 59 | + expect(Funky.primary_key).to eq(:funky_id) |
| 60 | + end |
100 | 61 |
|
101 | | - it "can have a scoping method" do |
102 | | - expect(Scoped.only_those_guys.respond_to? :all).to be_truthy |
103 | | - end |
| 62 | + it "knows the inheritance column" do |
| 63 | + expect(BaseClass.inheritance_column).to eq(:type) |
| 64 | + end |
104 | 65 |
|
105 | | - it "can type check parameters" do |
106 | | - expect(SubClass._react_param_conversion({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n, :validate_only)).to be(true) |
107 | | - end |
| 66 | + it "can override the inheritance column" do |
| 67 | + expect(Funky.inheritance_column).to eq(:funky_type) |
| 68 | + end |
108 | 69 |
|
109 | | - it "can type check parameters with native wrappers" do |
110 | | - expect(SubClass._react_param_conversion(Native({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n), :validate_only)).to be(true) |
111 | | - end |
| 70 | + it "knows the model name" do |
| 71 | + expect(BaseClass.model_name).to eq("BaseClass") |
| 72 | + end |
112 | 73 |
|
113 | | - it "will fail type checking if type does not match" do |
114 | | - expect(SubClass._react_param_conversion({attr1: 1, attr2: 2, type: nil, id: 123}.to_n, :validate_only)).to be_falsy |
115 | | - end |
| 74 | + it "can find a record by id" do |
| 75 | + expect(BaseClass.find(12).id).to eq(12) |
| 76 | + end |
116 | 77 |
|
117 | | - it "will convert a hash to an instance" do |
118 | | - ar = SubClass._react_param_conversion({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n) |
119 | | - expect(ar.attr1).to eq(1) |
120 | | - expect(ar.attr2).to eq(2) |
121 | | - expect(ar.id).to eq(123) |
122 | | - end |
| 78 | + it "has a find_by_xxx method" do |
| 79 | + expect(BaseClass.find_by_xxx("beer").xxx).to eq("beer") |
| 80 | + end |
123 | 81 |
|
| 82 | + it "will correctly infer the model type from the inheritance column" do |
| 83 | + expect(BaseClass.find_by_type("SubClass").class).to eq(SubClass) |
| 84 | + expect(BaseClass.find_by_type(nil).class).to eq(BaseClass) |
124 | 85 | end |
125 | 86 |
|
| 87 | + it "can have a has_many association" do |
| 88 | + expect(HasMany.reflect_on_association(:belongs_to).klass.reflect_on_association(:has_many).klass).to eq(HasMany) |
| 89 | + end |
| 90 | + |
| 91 | + it "can have a has_one association" do |
| 92 | + expect(HasOne.reflect_on_association(:belongs_to).klass.reflect_on_association(:has_one).klass).to eq(HasOne) |
| 93 | + end |
| 94 | + |
| 95 | + it "can override the class and foreign_key values when creating an association" do |
| 96 | + reflection = HasMany.reflect_on_association(:best_friends) |
| 97 | + expect(reflection.klass).to eq(BelongsTo) |
| 98 | + expect(reflection.association_foreign_key).to eq(:bf_id) |
| 99 | + end |
| 100 | + |
| 101 | + it "can have a scoping method" do |
| 102 | + expect(Scoped.only_those_guys.respond_to? :all).to be_truthy |
| 103 | + end |
| 104 | + |
| 105 | + it "can type check parameters" do |
| 106 | + expect(SubClass._react_param_conversion({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n, :validate_only)).to be(true) |
| 107 | + end |
| 108 | + |
| 109 | + it "can type check parameters with native wrappers" do |
| 110 | + expect(SubClass._react_param_conversion(Native({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n), :validate_only)).to be(true) |
| 111 | + end |
| 112 | + |
| 113 | + it "will fail type checking if type does not match" do |
| 114 | + expect(SubClass._react_param_conversion({attr1: 1, attr2: 2, type: nil, id: 123}.to_n, :validate_only)).to be_falsy |
| 115 | + end |
| 116 | + |
| 117 | + it "will convert a hash to an instance" do |
| 118 | + ar = SubClass._react_param_conversion({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n) |
| 119 | + expect(ar.attr1).to eq(1) |
| 120 | + expect(ar.attr2).to eq(2) |
| 121 | + expect(ar.id).to eq(123) |
| 122 | + end |
126 | 123 | end |
0 commit comments