|
| 1 | +require_relative '../../support/utils' |
| 2 | +include Utils |
| 3 | + |
| 4 | +describe 'Creating custom components', type: :feature, js: true do |
| 5 | + |
| 6 | + before :all do |
| 7 | + |
| 8 | + module Components end |
| 9 | + |
| 10 | + module Pages end |
| 11 | + |
| 12 | + module Matestack::Ui::Core end |
| 13 | + |
| 14 | + class ComponentTestController < ActionController::Base |
| 15 | + layout 'application' |
| 16 | + |
| 17 | + include Matestack::Ui::Core::ApplicationHelper |
| 18 | + |
| 19 | + def my_action |
| 20 | + responder_for(Pages::ExamplePage) |
| 21 | + end |
| 22 | + |
| 23 | + end |
| 24 | + |
| 25 | + Rails.application.routes.append do |
| 26 | + get '/component_test', to: 'component_test#my_action', as: 'component_test_action' |
| 27 | + end |
| 28 | + Rails.application.reload_routes! |
| 29 | + |
| 30 | + end |
| 31 | + |
| 32 | + it 'by orchestrating existing core components' do |
| 33 | + |
| 34 | + module Components end |
| 35 | + |
| 36 | + class Components::CrazyComponent < Matestack::Ui::StaticComponent |
| 37 | + |
| 38 | + def response |
| 39 | + components { |
| 40 | + div id: 'my-component-1' do |
| 41 | + plain "I'm a static component!" |
| 42 | + end |
| 43 | + } |
| 44 | + end |
| 45 | + |
| 46 | + end |
| 47 | + |
| 48 | + class Pages::ExamplePage < Matestack::Ui::Page |
| 49 | + |
| 50 | + def response |
| 51 | + components { |
| 52 | + div id: 'div-on-page' do |
| 53 | + custom_crazyComponent |
| 54 | + end |
| 55 | + } |
| 56 | + end |
| 57 | + |
| 58 | + end |
| 59 | + |
| 60 | + visit '/component_test' |
| 61 | + |
| 62 | + expect(page).to have_xpath('//div[@id="div-on-page"]/div[@id="my-component-1" and contains(.,"I\'m a static component!")]') |
| 63 | + |
| 64 | + end |
| 65 | + |
| 66 | + it 'by extending actionview components - static' do |
| 67 | + |
| 68 | + module Components end |
| 69 | + |
| 70 | + class Components::TimeAgo < Matestack::Ui::StaticActionviewComponent |
| 71 | + # class Components::TimeAgo < Matestack::Ui::Core::Actionview::Static # without alias |
| 72 | + |
| 73 | + def prepare |
| 74 | + @from_time = Time.now - 3.days - 14.minutes - 25.seconds |
| 75 | + end |
| 76 | + |
| 77 | + def response |
| 78 | + components { |
| 79 | + div id: 'my-component-1' do |
| 80 | + plain time_ago_in_words(@from_time) |
| 81 | + end |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + end |
| 86 | + |
| 87 | + class Pages::ExamplePage < Matestack::Ui::Page |
| 88 | + |
| 89 | + def response |
| 90 | + components { |
| 91 | + div id: 'div-on-page' do |
| 92 | + custom_timeAgo |
| 93 | + end |
| 94 | + } |
| 95 | + end |
| 96 | + |
| 97 | + end |
| 98 | + |
| 99 | + visit '/component_test' |
| 100 | + |
| 101 | + expect(page).to have_xpath('//div[@id="div-on-page"]/div[@id="my-component-1" and contains(.,"3 days")]') |
| 102 | + |
| 103 | + end |
| 104 | + |
| 105 | + it 'by extending actionview components - dynamic' do |
| 106 | + |
| 107 | + module Components end |
| 108 | + |
| 109 | + class Components::TimeClick < Matestack::Ui::DynamicActionviewComponent |
| 110 | + # class Components::TimeClick < Matestack::Ui::Core::Actionview::Dynamic # without alias |
| 111 | + |
| 112 | + def prepare |
| 113 | + @start_time = Time.now |
| 114 | + end |
| 115 | + |
| 116 | + def response |
| 117 | + components { |
| 118 | + div id: 'my-component-1' do |
| 119 | + plain "{{dynamic_value}} #{distance_of_time_in_words(@start_time, Time.now, include_seconds: true)}" |
| 120 | + end |
| 121 | + } |
| 122 | + end |
| 123 | + |
| 124 | + end |
| 125 | + |
| 126 | + component_definition = <<~javascript |
| 127 | +
|
| 128 | + MatestackUiCore.Vue.component('custom-timeclick', { |
| 129 | + mixins: [MatestackUiCore.componentMixin], |
| 130 | + data: function data() { |
| 131 | + return { |
| 132 | + dynamic_value: "Now I show: " |
| 133 | + }; |
| 134 | + }, |
| 135 | + mounted(){ |
| 136 | + const self = this |
| 137 | + setTimeout(function () { |
| 138 | + self.dynamic_value = "Later I show: " |
| 139 | + }, 300); |
| 140 | + } |
| 141 | + }); |
| 142 | +
|
| 143 | + javascript |
| 144 | + |
| 145 | + class Pages::ExamplePage < Matestack::Ui::Page |
| 146 | + |
| 147 | + def response |
| 148 | + components { |
| 149 | + div id: 'div-on-page' do |
| 150 | + async rerender_on: "refresh" do |
| 151 | + custom_timeClick |
| 152 | + end |
| 153 | + end |
| 154 | + } |
| 155 | + end |
| 156 | + |
| 157 | + end |
| 158 | + |
| 159 | + visit '/component_test' |
| 160 | + |
| 161 | + page.execute_script(component_definition) |
| 162 | + page.execute_script('MatestackUiCore.matestackEventHub.$emit("refresh")') |
| 163 | + |
| 164 | + expect(page).to have_content('Now I show: less than 5 seconds') |
| 165 | + sleep 0.5 |
| 166 | + expect(page).to have_content('Later I show: less than 5 seconds') |
| 167 | + end |
| 168 | + |
| 169 | +end |
0 commit comments