Method: Volt.spec_setup

Defined in:
lib/volt/spec/setup.rb

.spec_setup(app_path = '.') ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/volt/spec/setup.rb', line 5

def spec_setup(app_path = '.')
  require 'volt'

  ENV['SERVER'] = 'true'
  ENV['VOLT_ENV'] = 'test'

  require 'volt/boot'

  # Create a main volt app for tests
  volt_app = Volt.boot(app_path)

  unless RUBY_PLATFORM == 'opal'
    begin
      require 'volt/spec/capybara'

      setup_capybara(app_path, volt_app)
    rescue LoadError => e
      Volt.logger.warn("unable to load capybara, if you wish to use it for tests, be sure it is in the app's Gemfile")
      Volt.logger.error(e)
    end
  end

  unless ENV['BROWSER']
    # Not running integration tests with ENV['BROWSER']
    RSpec.configuration.filter_run_excluding type: :feature
  end



  cleanup_db = -> do
    volt_app.database.drop_database

    # Clear cached for a reset
    volt_app.page.instance_variable_set('@store', nil)
    volt_app.reset_query_pool!
  end

  if RUBY_PLATFORM != 'opal'
    # Call once during setup to clear if we killed the last run
    cleanup_db.call
  end

  # Run everything in the context of this app
  Thread.current['volt_app'] = volt_app

  # Setup the spec collection accessors
  # RSpec.shared_context "volt collections", {} do
  RSpec.shared_context 'volt collections', {} do
    # Page conflicts with capybara's page method, so we call it the_page for now.
    # TODO: we need a better solution for page

    let(:the_page) { Model.new }
    let(:store) do
      @__store_accessed = true
      $page ||= volt_app.page
      $page.store
    end
    let(:volt_app) { volt_app }
    let(:params) { volt_app.page.params }

    after do
      # Clear params if used
      url = volt_app.page.url
      if url.instance_variable_get('@params')
        url.instance_variable_set('@params', nil)
      end
    end

    if RUBY_PLATFORM != 'opal'
      after do |example|
        if @__store_accessed || example.[:type] == :feature
          # Clear the database after each spec where we use store
          cleanup_db.call
        end
      end

      # Cleanup after integration tests also.
      before(:example, {type: :feature}) do
        @__store_accessed = true
      end
    end
  end
end