summaryrefslogtreecommitdiff
path: root/qa/spec/factory/base_spec.rb
blob: 229f93a1041b6a941a3bf55848c4380cb5112a15 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# frozen_string_literal: true

describe QA::Factory::Base do
  include Support::StubENV

  let(:factory) { spy('factory') }
  let(:product) { spy('product') }
  let(:product_location) { 'http://product_location' }

  shared_context 'fabrication context' do
    subject do
      Class.new(described_class) do
        def self.name
          'MyFactory'
        end
      end
    end

    before do
      allow(subject).to receive(:current_url).and_return(product_location)
      allow(subject).to receive(:new).and_return(factory)
      allow(QA::Factory::Product).to receive(:populate!).with(factory, product_location).and_return(product)
    end
  end

  shared_examples 'fabrication method' do |fabrication_method_called, actual_fabrication_method = nil|
    let(:fabrication_method_used) { actual_fabrication_method || fabrication_method_called }

    it 'yields factory before calling factory method' do
      expect(factory).to receive(:something!).ordered
      expect(factory).to receive(fabrication_method_used).ordered.and_return(product_location)

      subject.public_send(fabrication_method_called, factory: factory) do |factory|
        factory.something!
      end
    end

    it 'does not log the factory and build method when QA_DEBUG=false' do
      stub_env('QA_DEBUG', 'false')
      expect(factory).to receive(fabrication_method_used).and_return(product_location)

      expect { subject.public_send(fabrication_method_called, 'something', factory: factory) }
        .not_to output.to_stdout
    end
  end

  describe '.fabricate!' do
    context 'when factory does not support fabrication via the API' do
      before do
        expect(described_class).to receive(:fabricate_via_api!).and_raise(NotImplementedError)
      end

      it 'calls .fabricate_via_browser_ui!' do
        expect(described_class).to receive(:fabricate_via_browser_ui!)

        described_class.fabricate!
      end
    end

    context 'when factory supports fabrication via the API' do
      it 'calls .fabricate_via_browser_ui!' do
        expect(described_class).to receive(:fabricate_via_api!)

        described_class.fabricate!
      end
    end
  end

  describe '.fabricate_via_api!' do
    include_context 'fabrication context'

    it_behaves_like 'fabrication method', :fabricate_via_api!

    it 'instantiates the factory, calls factory method returns fabrication product' do
      expect(factory).to receive(:fabricate_via_api!).and_return(product_location)

      result = subject.fabricate_via_api!(factory: factory, parents: [])

      expect(result).to eq(product)
    end

    it 'logs the factory and build method when QA_DEBUG=true' do
      stub_env('QA_DEBUG', 'true')
      expect(factory).to receive(:fabricate_via_api!).and_return(product_location)

      expect { subject.fabricate_via_api!(factory: factory, parents: []) }
        .to output(/==> Built a MyFactory via api with args \[\] in [\d\w\.\-]+/)
        .to_stdout
    end
  end

  describe '.fabricate_via_browser_ui!' do
    include_context 'fabrication context'

    it_behaves_like 'fabrication method', :fabricate_via_browser_ui!, :fabricate!

    it 'instantiates the factory and calls factory method' do
      subject.fabricate_via_browser_ui!('something', factory: factory, parents: [])

      expect(factory).to have_received(:fabricate!).with('something')
    end

    it 'returns fabrication product' do
      result = subject.fabricate_via_browser_ui!('something', factory: factory, parents: [])

      expect(result).to eq(product)
    end

    it 'logs the factory and build method when QA_DEBUG=true' do
      stub_env('QA_DEBUG', 'true')

      expect { subject.fabricate_via_browser_ui!('something', factory: factory, parents: []) }
        .to output(/==> Built a MyFactory via browser_ui with args \["something"\] in [\d\w\.\-]+/)
        .to_stdout
    end
  end

  describe '.dependency' do
    let(:dependency) { spy('dependency') }

    before do
      stub_const('Some::MyDependency', dependency)
    end

    subject do
      Class.new(described_class) do
        dependency Some::MyDependency, as: :mydep do |factory|
          factory.something!
        end
      end
    end

    it 'appends a new dependency and accessors' do
      expect(subject.dependencies).to be_one
    end

    it 'defines dependency accessors' do
      expect(subject.new).to respond_to :mydep, :mydep=
    end

    describe 'dependencies fabrication' do
      let(:dependency) { double('dependency') }
      let(:instance) { spy('instance') }

      subject do
        Class.new(described_class) do
          dependency Some::MyDependency, as: :mydep
        end
      end

      before do
        stub_const('Some::MyDependency', dependency)

        allow(subject).to receive(:new).and_return(instance)
        allow(subject).to receive(:current_url).and_return(product_location)
        allow(instance).to receive(:mydep).and_return(nil)
        expect(QA::Factory::Product).to receive(:populate!)
      end

      it 'builds all dependencies first' do
        expect(dependency).to receive(:fabricate!).once

        subject.fabricate!
      end
    end
  end

  describe '.product' do
    include_context 'fabrication context'

    subject do
      Class.new(described_class) do
        def fabricate!
          "any"
        end

        product :token
      end
    end

    it 'appends new product attribute' do
      expect(subject.attributes).to be_one
      expect(subject.attributes[0]).to be_a(QA::Factory::Product::Attribute)
      expect(subject.attributes[0].name).to eq(:token)
    end
  end
end