|
| 1 | +require_relative "../../../support/utils" |
| 2 | +include Utils |
| 3 | + |
| 4 | +describe "Isolate Component defer", type: :feature, js: true do |
| 5 | + |
| 6 | + before :all do |
| 7 | + class TouchedElementsCounter |
| 8 | + include Singleton |
| 9 | + attr_reader :counter |
| 10 | + |
| 11 | + def initialize |
| 12 | + @counter = 0 |
| 13 | + end |
| 14 | + |
| 15 | + def increment |
| 16 | + @counter = @counter + 1 |
| 17 | + end |
| 18 | + |
| 19 | + def reset |
| 20 | + @counter = 0 |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + class SomeIsolatedComponent < Matestack::Ui::IsolatedComponent |
| 25 | + register_self_as(:some_isolated_component_2) |
| 26 | + |
| 27 | + def prepare |
| 28 | + @counter = TouchedElementsCounter.instance |
| 29 | + @foo = "foo from isolated component" |
| 30 | + end |
| 31 | + |
| 32 | + def response |
| 33 | + @counter.increment |
| 34 | + div class: "some-isolated-component" do |
| 35 | + @counter.increment |
| 36 | + span id: "text", text: "some isolated compenent" |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + def authorized? |
| 41 | + true |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + before :each do |
| 47 | + TouchedElementsCounter.instance.reset |
| 48 | + end |
| 49 | + |
| 50 | + it "is resolved completely independent on the server side if defer true is set" do |
| 51 | + class ExamplePage < Matestack::Ui::Page |
| 52 | + def prepare |
| 53 | + @counter = TouchedElementsCounter.instance |
| 54 | + @foo = "bar" |
| 55 | + end |
| 56 | + |
| 57 | + def response |
| 58 | + @counter.increment |
| 59 | + div id: "page-div" do |
| 60 | + plain "page!" |
| 61 | + @counter.increment |
| 62 | + some_isolated_component_2 defer: true |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + visit "/example" |
| 68 | + # the first request resolves the whole page --> counter + 2 |
| 69 | + # the isolated component requests its content right after mount --> counter + 2 |
| 70 | + expect(TouchedElementsCounter.instance.counter).to eq 4 |
| 71 | + expect(page).to have_css('.some-isolated-component') |
| 72 | + |
| 73 | + TouchedElementsCounter.instance.reset |
| 74 | + |
| 75 | + visit "/example?component_class=SomeIsolatedComponent" |
| 76 | + expect(page).to have_css('.some-isolated-component') |
| 77 | + |
| 78 | + # the above URL is used within the async request of the isolated component in order to get its content |
| 79 | + # only the component itself is resolved on the server side --> counter + 2 |
| 80 | + expect(TouchedElementsCounter.instance.counter).to eq 2 |
| 81 | + end |
| 82 | + |
| 83 | + it "is deferred by a given time" do |
| 84 | + class ExamplePage < Matestack::Ui::Page |
| 85 | + def prepare |
| 86 | + @counter = TouchedElementsCounter.instance |
| 87 | + @foo = "bar" |
| 88 | + end |
| 89 | + |
| 90 | + def response |
| 91 | + @counter.increment |
| 92 | + div id: "page-div" do |
| 93 | + plain "page!" |
| 94 | + @counter.increment |
| 95 | + some_isolated_component_2 defer: 2000 |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + visit "/example" |
| 101 | + # the first request resolves the whole page --> counter + 2 |
| 102 | + # the isolated component requests its content right after mount --> counter + 2 |
| 103 | + expect(TouchedElementsCounter.instance.counter).to eq 4 |
| 104 | + # isolated component should not be rendered directly |
| 105 | + expect(page).not_to have_css('.some-isolated-component', wait: 1) |
| 106 | + # isolated component should be rendered after 2000ms |
| 107 | + expect(page).to have_css('.some-isolated-component', wait: 3) |
| 108 | + |
| 109 | + TouchedElementsCounter.instance.reset |
| 110 | + |
| 111 | + visit "/example?component_class=SomeIsolatedComponent" |
| 112 | + expect(page).to have_css('.some-isolated-component') |
| 113 | + |
| 114 | + # the above URL is used within the async request of the isolated component in order to get its content |
| 115 | + # only the component itself is resolved on the server side --> counter + 2 |
| 116 | + expect(TouchedElementsCounter.instance.counter).to eq 2 |
| 117 | + end |
| 118 | + |
| 119 | + it "is deferred by a given time" do |
| 120 | + class ExamplePage < Matestack::Ui::Page |
| 121 | + def prepare |
| 122 | + @counter = TouchedElementsCounter.instance |
| 123 | + @foo = "bar" |
| 124 | + end |
| 125 | + |
| 126 | + def response |
| 127 | + @counter.increment |
| 128 | + div id: "page-div" do |
| 129 | + plain "page!" |
| 130 | + @counter.increment |
| 131 | + some_isolated_component_2 |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + visit "/example" |
| 137 | + # the first request resolves the whole page --> counter + 2 |
| 138 | + # the isolated component should be rendered with the page --> counter + 2 |
| 139 | + expect(TouchedElementsCounter.instance.counter).to eq 4 |
| 140 | + expect(page).to have_css('.some-isolated-component') |
| 141 | + |
| 142 | + TouchedElementsCounter.instance.reset |
| 143 | + |
| 144 | + visit "/example?component_class=SomeIsolatedComponent" |
| 145 | + expect(page).to have_css('.some-isolated-component') |
| 146 | + |
| 147 | + # the above URL is used within the async request of the isolated component in order to get its content |
| 148 | + # only the component itself is resolved on the server side --> counter + 2 |
| 149 | + expect(TouchedElementsCounter.instance.counter).to eq 2 |
| 150 | + end |
| 151 | + |
| 152 | +end |
0 commit comments