BigW Consortium Gitlab

wiki_page_message_spec.rb 4.35 KB
Newer Older
1 2
require 'spec_helper'

3
describe ChatMessage::WikiPageMessage, models: true do
4
  subject { described_class.new(args) }
5 6 7 8 9

  let(:args) do
    {
      user: {
        name: 'Test User',
Tiago Botelho committed
10 11
        username: 'test.user',
        avatar_url: 'http://someavatar.com'
12 13
      },
      project_name: 'project_name',
14
      project_url: 'http://somewhere.com',
15 16
      object_attributes: {
        title: 'Wiki page title',
17
        url: 'http://url.com',
18 19 20 21 22
        content: 'Wiki page description'
      }
    }
  end

Tiago Botelho committed
23 24 25 26
  context 'without markdown' do
    describe '#pretext' do
      context 'when :action == "create"' do
        before { args[:object_attributes][:action] = 'create' }
27

Tiago Botelho committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        it 'returns a message that a new wiki page was created' do
          expect(subject.pretext).to eq(
            'test.user created <http://url.com|wiki page> in <http://somewhere.com|project_name>: '\
              '*Wiki page title*')
        end
      end

      context 'when :action == "update"' do
        before { args[:object_attributes][:action] = 'update' }

        it 'returns a message that a wiki page was updated' do
          expect(subject.pretext).to eq(
            'test.user edited <http://url.com|wiki page> in <http://somewhere.com|project_name>: '\
              '*Wiki page title*')
        end
43 44 45
      end
    end

Tiago Botelho committed
46 47
    describe '#attachments' do
      let(:color) { '#345' }
48

Tiago Botelho committed
49 50 51 52 53 54 55
      context 'when :action == "create"' do
        before { args[:object_attributes][:action] = 'create' }

        it 'returns the attachment for a new wiki page' do
          expect(subject.attachments).to eq([
            {
              text: "Wiki page description",
56
              color: color
Tiago Botelho committed
57 58 59 60 61 62 63 64 65 66 67 68
            }
          ])
        end
      end

      context 'when :action == "update"' do
        before { args[:object_attributes][:action] = 'update' }

        it 'returns the attachment for an updated wiki page' do
          expect(subject.attachments).to eq([
            {
              text: "Wiki page description",
69
              color: color
Tiago Botelho committed
70 71 72
            }
          ])
        end
73
      end
74 75 76
    end
  end

Tiago Botelho committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90
  context 'with markdown' do
    before do
      args[:markdown] = true
    end

    describe '#pretext' do
      context 'when :action == "create"' do
        before { args[:object_attributes][:action] = 'create' }

        it 'returns a message that a new wiki page was created' do
          expect(subject.pretext).to eq(
            'test.user created [wiki page](http://url.com) in [project_name](http://somewhere.com): *Wiki page title*')
        end
      end
91

Tiago Botelho committed
92 93
      context 'when :action == "update"' do
        before { args[:object_attributes][:action] = 'update' }
94

Tiago Botelho committed
95 96 97 98
        it 'returns a message that a wiki page was updated' do
          expect(subject.pretext).to eq(
            'test.user edited [wiki page](http://url.com) in [project_name](http://somewhere.com): *Wiki page title*')
        end
99
      end
100
    end
101

Tiago Botelho committed
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 132 133 134 135
    describe '#attachments' do
      context 'when :action == "create"' do
        before { args[:object_attributes][:action] = 'create' }

        it 'returns the attachment for a new wiki page' do
          expect(subject.attachments).to eq('Wiki page description')
        end
      end

      context 'when :action == "update"' do
        before { args[:object_attributes][:action] = 'update' }

        it 'returns the attachment for an updated wiki page' do
          expect(subject.attachments).to eq('Wiki page description')
        end
      end
    end

    describe '#activity' do
      context 'when :action == "create"' do
        before { args[:object_attributes][:action] = 'create' }

        it 'returns the attachment for a new wiki page' do
          expect(subject.activity).to eq({
            title: 'test.user created [wiki page](http://url.com)',
            subtitle: 'in [project_name](http://somewhere.com)',
            text: 'Wiki page title',
            image: 'http://someavatar.com'
          })
        end
      end

      context 'when :action == "update"' do
        before { args[:object_attributes][:action] = 'update' }
136

Tiago Botelho committed
137 138 139 140 141 142 143 144
        it 'returns the attachment for an updated wiki page' do
          expect(subject.activity).to eq({
            title: 'test.user edited [wiki page](http://url.com)',
            subtitle: 'in [project_name](http://somewhere.com)',
            text: 'Wiki page title',
            image: 'http://someavatar.com'
          })
        end
145
      end
146 147 148
    end
  end
end