Welcome to mirror list, hosted at ThFree Co, Russian Federation.

timeline_text_and_date_time_separator_spec.rb « quick_actions « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89fe19b8f60088520c2f05a5842a19c8f6783101 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::QuickActions::TimelineTextAndDateTimeSeparator do
  subject(:timeline_text_and_datetime_separator) { described_class }

  shared_examples 'arg line with invalid parameters' do
    it 'returns nil' do
      expect(timeline_text_and_datetime_separator.new(invalid_arg).execute).to eq(nil)
    end
  end

  shared_examples 'arg line with valid parameters' do
    it 'returns text and date time array' do
      freeze_time do
        expect(timeline_text_and_datetime_separator.new(valid_arg).execute).to eq(expected_response)
      end
    end
  end

  describe 'execute' do
    context 'with invalid parameters in arg line' do
      context 'with empty arg line' do
        it_behaves_like 'arg line with invalid parameters' do
          let(:invalid_arg) { '' }
        end
      end

      context 'with invalid date' do
        it_behaves_like 'arg line with invalid parameters' do
          let(:invalid_arg) { 'timeline comment | 2022-13-13 09:30' }
        end

        it_behaves_like 'arg line with invalid parameters' do
          let(:invalid_arg) { 'timeline comment | 2022-09/09 09:30' }
        end

        it_behaves_like 'arg line with invalid parameters' do
          let(:invalid_arg) { 'timeline comment | 2022-09.09 09:30' }
        end
      end

      context 'with invalid time' do
        it_behaves_like 'arg line with invalid parameters' do
          let(:invalid_arg) { 'timeline comment | 2022-11-13 29:30' }
        end
      end

      context 'when date is invalid in arg line' do
        let(:invalid_arg) { 'timeline comment | wrong data type' }

        it 'return current date' do
          timeline_args = timeline_text_and_datetime_separator.new(invalid_arg).execute

          expect(timeline_args).to be_an_instance_of(Array)
          expect(timeline_args.first).to eq('timeline comment')
          expect(timeline_args.second).to match(Gitlab::QuickActions::TimelineTextAndDateTimeSeparator::DATETIME_REGEX)
        end
      end
    end

    context 'with valid parameters' do
      context 'when only timeline text present in arg line' do
        it_behaves_like 'arg line with valid parameters' do
          let(:timeline_text) { 'timeline comment' }
          let(:valid_arg) { timeline_text }
          let(:date) { DateTime.current.strftime("%Y-%m-%d %H:%M:00 UTC") }
          let(:expected_response) { [timeline_text, date] }
        end
      end

      context 'when only timeline text and time present in arg line' do
        it_behaves_like 'arg line with valid parameters' do
          let(:timeline_text) { 'timeline comment' }
          let(:date) { '09:30' }
          let(:valid_arg) { "#{timeline_text} | #{date}" }
          let(:parsed_date) { DateTime.parse(date) }
          let(:expected_response) { [timeline_text, parsed_date] }
        end
      end

      context 'when timeline text and date is present in arg line' do
        it_behaves_like 'arg line with valid parameters' do
          let(:timeline_text) { 'timeline comment' }
          let(:date) { '2022-06-05 09:30' }
          let(:valid_arg) { "#{timeline_text} | #{date}" }
          let(:parsed_date) { DateTime.parse(date) }
          let(:expected_response) { [timeline_text, parsed_date] }
        end
      end
    end
  end
end