summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/taskable_shared_examples.rb
blob: f04f509f3d271e62f57be99a2a8ff05f51628347 (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
# frozen_string_literal: true

# Specs for task state functionality for issues and merge requests.
#
# Requires a context containing:
#   subject { Issue or MergeRequest }
shared_examples 'a Taskable' do
  describe 'with multiple tasks' do
    before do
      subject.description = <<-EOT.strip_heredoc
        * [ ] Task 1
        * [x] Task 2
        * [x] Task 3
        * [ ] Task 4
        * [ ] Task 5
      EOT
    end

    it 'returns the correct task status' do
      expect(subject.task_status).to match('2 of')
      expect(subject.task_status).to match('5 tasks completed')
      expect(subject.task_status_short).to match('2/')
      expect(subject.task_status_short).to match('5 tasks')
    end

    describe '#tasks?' do
      it 'returns true when object has tasks' do
        expect(subject.tasks?).to eq true
      end

      it 'returns false when object has no tasks' do
        subject.description = 'Now I have no tasks'
        expect(subject.tasks?).to eq false
      end
    end
  end

  describe 'with nested tasks' do
    before do
      subject.description = <<-EOT.strip_heredoc
        - [ ] Task a
          - [x] Task a.1
          - [ ] Task a.2
        - [ ] Task b

        1. [ ] Task 1
          1. [ ] Task 1.1
          1. [ ] Task 1.2
        1. [x] Task 2
          1. [x] Task 2.1
      EOT
    end

    it 'returns the correct task status' do
      expect(subject.task_status).to match('3 of')
      expect(subject.task_status).to match('9 tasks completed')
      expect(subject.task_status_short).to match('3/')
      expect(subject.task_status_short).to match('9 tasks')
    end
  end

  describe 'with an incomplete task' do
    before do
      subject.description = <<-EOT.strip_heredoc
        * [ ] Task 1
      EOT
    end

    it 'returns the correct task status' do
      expect(subject.task_status).to match('0 of')
      expect(subject.task_status).to match('1 task completed')
      expect(subject.task_status_short).to match('0/')
      expect(subject.task_status_short).to match('1 task')
    end
  end

  describe 'with tasks that are not formatted correctly' do
    before do
      subject.description = <<-EOT.strip_heredoc
        [ ] task 1
        [ ] task 2

        - [ ]task 1
        -[ ] task 2
      EOT
    end

    it 'returns the correct task status' do
      expect(subject.task_status).to match('0 of')
      expect(subject.task_status).to match('0 tasks completed')
      expect(subject.task_status_short).to match('0/')
      expect(subject.task_status_short).to match('0 task')
    end
  end

  describe 'with a complete task' do
    before do
      subject.description = <<-EOT.strip_heredoc
        * [x] Task 1
      EOT
    end

    it 'returns the correct task status' do
      expect(subject.task_status).to match('1 of')
      expect(subject.task_status).to match('1 task completed')
      expect(subject.task_status_short).to match('1/')
      expect(subject.task_status_short).to match('1 task')
    end
  end

  describe 'with tasks in blockquotes' do
    before do
      subject.description = <<-EOT.strip_heredoc
        > - [ ] Task a
        > > - [x] Task a.1

        >>>
        1. [ ] Task 1
        1. [x] Task 2
        >>>
      EOT
    end

    it 'returns the correct task status' do
      expect(subject.task_status).to match('2 of')
      expect(subject.task_status).to match('4 tasks completed')
      expect(subject.task_status_short).to match('2/')
      expect(subject.task_status_short).to match('4 tasks')
    end
  end
end