summaryrefslogtreecommitdiff
path: root/spec/models/list_spec.rb
blob: 37158584062c9663719c01e1890c979928cba250 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe List do
  it_behaves_like 'having unique enum values'

  describe 'relationships' do
    it { is_expected.to belong_to(:board) }
    it { is_expected.to belong_to(:label) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:board) }
    it { is_expected.to validate_presence_of(:label) }
    it { is_expected.to validate_presence_of(:list_type) }
    it { is_expected.to validate_presence_of(:position) }
    it { is_expected.to validate_numericality_of(:position).only_integer.is_greater_than_or_equal_to(0) }

    context 'when list_type is set to closed' do
      subject { described_class.new(list_type: :closed) }

      it { is_expected.not_to validate_presence_of(:label) }
      it { is_expected.not_to validate_presence_of(:position) }
    end
  end

  describe '#destroy' do
    it 'can be destroyed when list_type is set to label' do
      subject = create(:list)

      expect(subject.destroy).to be_truthy
    end

    it 'can not be destroyed when list_type is set to closed' do
      subject = create(:closed_list)

      expect(subject.destroy).to be_falsey
    end
  end

  describe '#destroyable?' do
    it 'returns true when list_type is set to label' do
      subject.list_type = :label

      expect(subject).to be_destroyable
    end

    it 'returns false when list_type is set to closed' do
      subject.list_type = :closed

      expect(subject).not_to be_destroyable
    end
  end

  describe '#movable?' do
    it 'returns true when list_type is set to label' do
      subject.list_type = :label

      expect(subject).to be_movable
    end

    it 'returns false when list_type is set to closed' do
      subject.list_type = :closed

      expect(subject).not_to be_movable
    end
  end

  describe '#title' do
    it 'returns label name when list_type is set to label' do
      subject.list_type = :label
      subject.label = Label.new(name: 'Development')

      expect(subject.title).to eq 'Development'
    end

    it 'returns Closed when list_type is set to closed' do
      subject.list_type = :closed

      expect(subject.title).to eq 'Closed'
    end
  end

  describe '#update_preferences_for' do
    let(:user) { create(:user) }
    let(:list) { create(:list) }

    context 'when user is present' do
      context 'when there are no preferences for user' do
        it 'creates new user preferences' do
          expect { list.update_preferences_for(user, collapsed: true) }.to change { ListUserPreference.count }.by(1)
          expect(list.preferences_for(user).collapsed).to eq(true)
        end
      end

      context 'when there are preferences for user' do
        it 'updates user preferences' do
          list.update_preferences_for(user, collapsed: false)

          expect { list.update_preferences_for(user, collapsed: true) }.not_to change { ListUserPreference.count }
          expect(list.preferences_for(user).collapsed).to eq(true)
        end
      end

      context 'when user is nil' do
        it 'does not create user preferences' do
          expect { list.update_preferences_for(nil, collapsed: true) }.not_to change { ListUserPreference.count }
        end
      end
    end
  end

  describe '#preferences_for' do
    let(:user) { create(:user) }
    let(:list) { create(:list) }

    context 'when user is nil' do
      it 'returns not persisted preferences' do
        preferences = list.preferences_for(nil)

        expect(preferences.persisted?).to eq(false)
        expect(preferences.list_id).to eq(list.id)
        expect(preferences.user_id).to be_nil
      end
    end

    context 'when a user preference already exists' do
      before do
        list.update_preferences_for(user, collapsed: true)
      end

      it 'loads preference for user' do
        preferences = list.preferences_for(user)

        expect(preferences).to be_persisted
        expect(preferences.collapsed).to eq(true)
      end
    end

    context 'when preferences for user does not exist' do
      it 'returns not persisted preferences' do
        preferences = list.preferences_for(user)

        expect(preferences.persisted?).to eq(false)
        expect(preferences.user_id).to eq(user.id)
        expect(preferences.list_id).to eq(list.id)
      end
    end
  end
end