summaryrefslogtreecommitdiff
path: root/spec/services/projects/record_target_platforms_service_spec.rb
blob: 85311f36428d7683b3b869d09079ba22a387374e (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::RecordTargetPlatformsService, '#execute' do
  let_it_be(:project) { create(:project) }

  subject(:execute) { described_class.new(project).execute }

  context 'when project is an XCode project' do
    before do
      double = instance_double(Projects::AppleTargetPlatformDetectorService, execute: [:ios, :osx])
      allow(Projects::AppleTargetPlatformDetectorService).to receive(:new) { double }
    end

    it 'creates a new setting record for the project', :aggregate_failures do
      expect { execute }.to change { ProjectSetting.count }.from(0).to(1)
      expect(ProjectSetting.last.target_platforms).to match_array(%w(ios osx))
    end

    it 'returns array of detected target platforms' do
      expect(execute).to match_array %w(ios osx)
    end

    context 'when a project has an existing setting record' do
      before do
        create(:project_setting, project: project, target_platforms: saved_target_platforms)
      end

      def project_setting
        ProjectSetting.find_by_project_id(project.id)
      end

      context 'when target platforms changed' do
        let(:saved_target_platforms) { %w(tvos) }

        it 'updates' do
          expect { execute }.to change { project_setting.target_platforms }.from(%w(tvos)).to(%w(ios osx))
        end

        it { is_expected.to match_array %w(ios osx) }
      end

      context 'when target platforms are the same' do
        let(:saved_target_platforms) { %w(osx ios) }

        it 'does not update' do
          expect { execute }.not_to change { project_setting.updated_at }
        end
      end
    end
  end

  context 'when project is not an XCode project' do
    before do
      double = instance_double(Projects::AppleTargetPlatformDetectorService, execute: [])
      allow(Projects::AppleTargetPlatformDetectorService).to receive(:new).with(project) { double }
    end

    it 'does nothing' do
      expect { execute }.not_to change { ProjectSetting.count }
    end

    it { is_expected.to be_nil }
  end
end