|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "test_helper" |
| 4 | + |
| 5 | +class StimulusReflex::ReflexFactoryTest < ActionCable::Channel::TestCase |
| 6 | + tests StimulusReflex::Channel |
| 7 | + |
| 8 | + test "reflex class needs to be an ancestor of StimulusReflex::Reflex" do |
| 9 | + exception = assert_raises(NameError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "Object#inspect"}).call } |
| 10 | + assert_equal "uninitialized constant ObjectReflex", exception.message |
| 11 | + |
| 12 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "NoReflex#no_reflex"}).call } |
| 13 | + assert_equal "NoReflex is not a StimulusReflex::Reflex", exception.message |
| 14 | + |
| 15 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "No#no_reflex"}).call } |
| 16 | + assert_equal "NoReflex is not a StimulusReflex::Reflex", exception.message |
| 17 | + end |
| 18 | + |
| 19 | + test "doesn't raise if owner of method is ancestor of reflex class and descendant of StimulusReflex::Reflex" do |
| 20 | + assert_nothing_raised { StimulusReflex::ReflexFactory.new(subscribe, {version: StimulusReflex::VERSION, target: "ApplicationReflex#default_reflex"}).call } |
| 21 | + assert_nothing_raised { StimulusReflex::ReflexFactory.new(subscribe, {version: StimulusReflex::VERSION, target: "ApplicationReflex#application_reflex"}).call } |
| 22 | + |
| 23 | + assert_nothing_raised { StimulusReflex::ReflexFactory.new(subscribe, {version: StimulusReflex::VERSION, target: "PostReflex#default_reflex"}).call } |
| 24 | + assert_nothing_raised { StimulusReflex::ReflexFactory.new(subscribe, {version: StimulusReflex::VERSION, target: "PostReflex#application_reflex"}).call } |
| 25 | + assert_nothing_raised { StimulusReflex::ReflexFactory.new(subscribe, {version: StimulusReflex::VERSION, target: "PostReflex#post_reflex"}).call } |
| 26 | + |
| 27 | + assert_nothing_raised { StimulusReflex::ReflexFactory.new(subscribe, {version: StimulusReflex::VERSION, target: "CounterReflex#default_reflex"}).call } |
| 28 | + assert_nothing_raised { StimulusReflex::ReflexFactory.new(subscribe, {version: StimulusReflex::VERSION, target: "CounterReflex#application_reflex"}).call } |
| 29 | + assert_nothing_raised { StimulusReflex::ReflexFactory.new(subscribe, {version: StimulusReflex::VERSION, target: "CounterReflex#increment"}).call } |
| 30 | + end |
| 31 | + |
| 32 | + test "raises if method is not owned by a descendant of StimulusReflex::Reflex" do |
| 33 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "ApplicationReflex#itself"}).call } |
| 34 | + assert_equal "Reflex method 'itself' is not defined on class 'ApplicationReflex' or on any of its ancestors", exception.message |
| 35 | + |
| 36 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "ApplicationReflex#itself"}).call } |
| 37 | + assert_equal "Reflex method 'itself' is not defined on class 'ApplicationReflex' or on any of its ancestors", exception.message |
| 38 | + |
| 39 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "PostReflex#itself"}).call } |
| 40 | + assert_equal "Reflex method 'itself' is not defined on class 'PostReflex' or on any of its ancestors", exception.message |
| 41 | + |
| 42 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "PostReflex#binding"}).call } |
| 43 | + assert_equal "Reflex method 'binding' is not defined on class 'PostReflex' or on any of its ancestors", exception.message |
| 44 | + |
| 45 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "PostReflex#byebug"}).call } |
| 46 | + assert_equal "Reflex method 'byebug' is not defined on class 'PostReflex' or on any of its ancestors", exception.message |
| 47 | + |
| 48 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "PostReflex#debug"}).call } |
| 49 | + assert_equal "Reflex method 'debug' is not defined on class 'PostReflex' or on any of its ancestors", exception.message |
| 50 | + |
| 51 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "ApplicationReflex#post_reflex"}).call } |
| 52 | + assert_equal "Reflex method 'post_reflex' is not defined on class 'ApplicationReflex' or on any of its ancestors", exception.message |
| 53 | + end |
| 54 | + |
| 55 | + test "raises if method is a private method" do |
| 56 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "ApplicationReflex#private_application_reflex"}).call } |
| 57 | + assert_equal "Reflex method 'private_application_reflex' is not defined on class 'ApplicationReflex' or on any of its ancestors", exception.message |
| 58 | + |
| 59 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "PostReflex#private_application_reflex"}).call } |
| 60 | + assert_equal "Reflex method 'private_application_reflex' is not defined on class 'PostReflex' or on any of its ancestors", exception.message |
| 61 | + |
| 62 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "PostReflex#private_post_reflex"}).call } |
| 63 | + assert_equal "Reflex method 'private_post_reflex' is not defined on class 'PostReflex' or on any of its ancestors", exception.message |
| 64 | + |
| 65 | + exception = assert_raises(ArgumentError) { StimulusReflex::ReflexFactory.new(subscribe, {target: "CounterReflex#private_post_reflex"}).call } |
| 66 | + assert_equal "Reflex method 'private_post_reflex' is not defined on class 'CounterReflex' or on any of its ancestors", exception.message |
| 67 | + end |
| 68 | + |
| 69 | + test "safe_ancestors" do |
| 70 | + reflex_factory = StimulusReflex::ReflexFactory.new(subscribe, {target: "ApplicationReflex#default_reflex"}) |
| 71 | + assert_equal [ApplicationReflex, StimulusReflex::CableReadiness], reflex_factory.send(:safe_ancestors) |
| 72 | + |
| 73 | + reflex_factory = StimulusReflex::ReflexFactory.new(subscribe, {target: "PostReflex#default_reflex"}) |
| 74 | + assert_equal [PostReflex, ApplicationReflex, StimulusReflex::CableReadiness], reflex_factory.send(:safe_ancestors) |
| 75 | + |
| 76 | + reflex_factory = StimulusReflex::ReflexFactory.new(subscribe, {target: "CounterReflex#increment"}) |
| 77 | + assert_equal [CounterReflex, CounterConcern, ApplicationReflex, StimulusReflex::CableReadiness], reflex_factory.send(:safe_ancestors) |
| 78 | + end |
| 79 | +end |
0 commit comments