summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/graphql/connection_redaction_shared_examples.rb
blob: 12a7b3fe414e2c02c01fac1041f50cdccfb9d9ee (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
# frozen_string_literal: true

# requires:
#  - `connection` (no-empty, containing `unwanted` and at least one more item)
#  - `unwanted` (single item in collection)
RSpec.shared_examples 'a redactable connection' do
  context 'no redactor set' do
    it 'contains the unwanted item' do
      expect(connection.nodes).to include(unwanted)
    end

    it 'does not redact more than once' do
      connection.nodes
      r_state = connection.send(:redaction_state)

      expect(r_state.redacted { raise 'Should not be called!' }).to be_present
    end
  end

  let_it_be(:constant_redactor) do
    Class.new do
      def initialize(remove)
        @remove = remove
      end

      def redact(items)
        items - @remove
      end
    end
  end

  context 'redactor is set' do
    let(:redactor) do
      constant_redactor.new([unwanted])
    end

    before do
      connection.redactor = redactor
    end

    it 'does not contain the unwanted item' do
      expect(connection.nodes).not_to include(unwanted)
      expect(connection.nodes).not_to be_empty
    end

    it 'does not redact more than once' do
      expect(redactor).to receive(:redact).once.and_call_original

      connection.nodes
      connection.nodes
      connection.nodes
    end
  end
end