task_list_toggle_service_spec.rb 4.25 KB
Newer Older
1 2
# frozen_string_literal: true

Brett Walker's avatar
Brett Walker committed
3 4 5
require 'spec_helper'

describe TaskListToggleService do
Brett Walker's avatar
Brett Walker committed
6 7 8 9
  let(:markdown) do
    <<-EOT.strip_heredoc
      * [ ] Task 1
      * [x] Task 2
Brett Walker's avatar
Brett Walker committed
10

Brett Walker's avatar
Brett Walker committed
11
      A paragraph
Brett Walker's avatar
Brett Walker committed
12

Brett Walker's avatar
Brett Walker committed
13 14
      1. [X] Item 1
         - [ ] Sub-item 1
15 16 17 18

      - [ ] loose list

        with an embedded paragraph
Brett Walker's avatar
Brett Walker committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32
    EOT
  end

  let(:markdown_html) do
    <<-EOT.strip_heredoc
      <ul data-sourcepos="1:1-3:0" class="task-list" dir="auto">
        <li data-sourcepos="1:1-1:12" class="task-list-item">
          <input type="checkbox" class="task-list-item-checkbox" disabled> Task 1
        </li>
        <li data-sourcepos="2:1-3:0" class="task-list-item">
          <input type="checkbox" class="task-list-item-checkbox" disabled checked> Task 2
        </li>
      </ul>
      <p data-sourcepos="4:1-4:11" dir="auto">A paragraph</p>
33 34 35 36 37 38
      <ol data-sourcepos="6:1-8:0" class="task-list" dir="auto">
        <li data-sourcepos="6:1-8:0" class="task-list-item">
          <input type="checkbox" class="task-list-item-checkbox" checked="" disabled=""> Item 1
          <ul data-sourcepos="7:4-8:0" class="task-list">
            <li data-sourcepos="7:4-8:0" class="task-list-item">
              <input type="checkbox" class="task-list-item-checkbox" disabled=""> Sub-item 1
Brett Walker's avatar
Brett Walker committed
39 40 41 42
            </li>
          </ul>
        </li>
      </ol>
43 44 45 46 47 48
      <ul data-sourcepos="9:1-11:28" class="task-list" dir="auto">
        <li data-sourcepos="9:1-11:28" class="task-list-item">
          <p data-sourcepos="9:3-9:16"><input type="checkbox" class="task-list-item-checkbox" disabled=""> loose list</p>
          <p data-sourcepos="11:3-11:28">with an embedded paragraph</p>
        </li>
      </ul>
Brett Walker's avatar
Brett Walker committed
49 50 51
    EOT
  end

52 53 54 55
  it 'checks Task 1' do
    toggler = described_class.new(markdown, markdown_html,
                                  toggle_as_checked: true,
                                  line_source: '* [ ] Task 1', line_number: 1)
Brett Walker's avatar
Brett Walker committed
56

57 58 59 60
    expect(toggler.execute).to be_truthy
    expect(toggler.updated_markdown.lines[0]).to eq "* [x] Task 1\n"
    expect(toggler.updated_markdown_html).to include('disabled checked> Task 1')
  end
Brett Walker's avatar
Brett Walker committed
61

62 63 64 65
  it 'unchecks Item 1' do
    toggler = described_class.new(markdown, markdown_html,
                                  toggle_as_checked: false,
                                  line_source: '1. [X] Item 1', line_number: 6)
Brett Walker's avatar
Brett Walker committed
66

67 68 69 70
    expect(toggler.execute).to be_truthy
    expect(toggler.updated_markdown.lines[5]).to eq "1. [ ] Item 1\n"
    expect(toggler.updated_markdown_html).to include('disabled> Item 1')
  end
71

72 73 74 75 76 77 78 79 80 81
  it 'checks task in loose list' do
    toggler = described_class.new(markdown, markdown_html,
                                  toggle_as_checked: true,
                                  line_source: '- [ ] loose list', line_number: 9)

    expect(toggler.execute).to be_truthy
    expect(toggler.updated_markdown.lines[8]).to eq "- [x] loose list\n"
    expect(toggler.updated_markdown_html).to include('disabled checked> loose list')
  end

82 83 84 85
  it 'returns false if line_source does not match the text' do
    toggler = described_class.new(markdown, markdown_html,
                                  toggle_as_checked: false,
                                  line_source: '* [x] Task Added', line_number: 2)
Brett Walker's avatar
Brett Walker committed
86

87
    expect(toggler.execute).to be_falsey
Brett Walker's avatar
Brett Walker committed
88
  end
Brett Walker's avatar
Brett Walker committed
89

90 91 92 93 94 95 96 97 98 99 100
  it 'tolerates \r\n line endings' do
    rn_markdown = markdown.gsub("\n", "\r\n")
    toggler = described_class.new(rn_markdown, markdown_html,
                                  toggle_as_checked: true,
                                  line_source: '* [ ] Task 1', line_number: 1)

    expect(toggler.execute).to be_truthy
    expect(toggler.updated_markdown.lines[0]).to eq "* [x] Task 1\r\n"
    expect(toggler.updated_markdown_html).to include('disabled checked> Task 1')
  end

101 102 103 104 105 106
  it 'returns false if markdown is nil' do
    toggler = described_class.new(nil, markdown_html,
                                  toggle_as_checked: false,
                                  line_source: '* [x] Task Added', line_number: 2)

    expect(toggler.execute).to be_falsey
Brett Walker's avatar
Brett Walker committed
107 108
  end

109 110 111 112
  it 'returns false if markdown_html is nil' do
    toggler = described_class.new(markdown, nil,
                                  toggle_as_checked: false,
                                  line_source: '* [x] Task Added', line_number: 2)
Brett Walker's avatar
Brett Walker committed
113

114
    expect(toggler.execute).to be_falsey
Brett Walker's avatar
Brett Walker committed
115
  end
Brett Walker's avatar
Brett Walker committed
116
end