File: helpers_spec.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (102 lines) | stat: -rw-r--r-- 2,762 bytes parent folder | download
1
2
3
4
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# frozen_string_literal: true

require 'spec_helper'

module Grape
  module DSL
    module HelpersSpec
      class Dummy
        include Grape::DSL::Helpers

        def self.mods
          namespace_stackable(:helpers)
        end

        def self.first_mod
          mods.first
        end
      end
    end

    module BooleanParam
      extend Grape::API::Helpers

      params :requires_toggle_prm do
        requires :toggle_prm, type: Boolean
      end
    end

    class Base < Grape::API
      helpers BooleanParam
    end

    class Child < Base; end

    describe Helpers do
      subject { Class.new(HelpersSpec::Dummy) }

      let(:proc) do
        lambda do |*|
          def test
            :test
          end
        end
      end

      describe '.helpers' do
        it 'adds a module with the given block' do
          expect(subject).to receive(:namespace_stackable).with(:helpers, kind_of(Grape::DSL::Helpers::BaseHelper)).and_call_original
          expect(subject).to receive(:namespace_stackable).with(:helpers).and_call_original
          subject.helpers(&proc)

          expect(subject.first_mod.instance_methods).to include(:test)
        end

        it 'uses provided modules' do
          mod = Module.new

          expect(subject).to receive(:namespace_stackable).with(:helpers, kind_of(Grape::DSL::Helpers::BaseHelper)).and_call_original.twice
          expect(subject).to receive(:namespace_stackable).with(:helpers).and_call_original
          subject.helpers(mod, &proc)

          expect(subject.first_mod).to eq mod
        end

        it 'uses many provided modules' do
          mod  = Module.new
          mod2 = Module.new
          mod3 = Module.new

          expect(subject).to receive(:namespace_stackable).with(:helpers, kind_of(Grape::DSL::Helpers::BaseHelper)).and_call_original.exactly(4).times
          expect(subject).to receive(:namespace_stackable).with(:helpers).and_call_original.exactly(3).times

          subject.helpers(mod, mod2, mod3, &proc)

          expect(subject.mods).to include(mod)
          expect(subject.mods).to include(mod2)
          expect(subject.mods).to include(mod3)
        end

        context 'with an external file' do
          it 'sets Boolean as a Grape::API::Boolean' do
            subject.helpers BooleanParam
            expect(subject.first_mod::Boolean).to eq Grape::API::Boolean
          end
        end

        context 'in child classes' do
          it 'is available' do
            klass = Child
            expect do
              klass.instance_eval do
                params do
                  use :requires_toggle_prm
                end
              end
            end.not_to raise_exception
          end
        end
      end
    end
  end
end