Skip to content

Commit 6d8a7aa

Browse files
committed
feat: add test case fir custom link
1 parent a514b3f commit 6d8a7aa

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

spec/requests/cutom_links_spec.rb

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe CustomLinksController, type: :request do
4+
let(:user) { create(:user) }
5+
let(:admin) { create(:casa_admin, casa_org: user.casa_org) }
6+
let(:custom_link) { create(:custom_link, casa_org_id: user.casa_org_id) }
7+
let(:valid_attributes) { { text: 'Link Text', url: 'http://example.com', active: true, casa_org_id: user.casa_org_id } }
8+
let(:invalid_attributes) { { text: '', url: 'invalid', active: nil } }
9+
10+
before do
11+
sign_in user
12+
allow_any_instance_of(CustomLinkPolicy).to receive(:new?).and_return(true)
13+
allow_any_instance_of(CustomLinkPolicy).to receive(:edit?).and_return(true)
14+
allow_any_instance_of(CustomLinkPolicy).to receive(:create?).and_return(true)
15+
allow_any_instance_of(CustomLinkPolicy).to receive(:update?).and_return(true)
16+
allow_any_instance_of(CustomLinkPolicy).to receive(:destroy?).and_return(true)
17+
end
18+
19+
describe 'GET #new' do
20+
it 'authorizes the action' do
21+
expect_any_instance_of(CustomLinkPolicy).to receive(:new?).and_return(true)
22+
get new_custom_link_path
23+
end
24+
25+
it 'assigns a new CustomLink with the current user\'s casa_org_id to @custom_link' do
26+
get new_custom_link_path
27+
expect(assigns(:custom_link)).to be_a_new(CustomLink)
28+
expect(assigns(:custom_link).casa_org_id).to eq(user.casa_org_id)
29+
end
30+
31+
it 'renders the new template' do
32+
get new_custom_link_path
33+
expect(response).to render_template(:new)
34+
end
35+
end
36+
37+
describe 'GET #edit' do
38+
it 'assigns the requested custom_link as @custom_link' do
39+
get edit_custom_link_path(custom_link)
40+
expect(assigns(:custom_link)).to eq(custom_link)
41+
end
42+
43+
it 'authorizes the action' do
44+
expect_any_instance_of(CustomLinkPolicy).to receive(:edit?).and_return(true)
45+
get edit_custom_link_path(custom_link)
46+
end
47+
end
48+
49+
describe 'POST #create' do
50+
context 'with valid parameters' do
51+
it 'creates a new CustomLink' do
52+
expect do
53+
post custom_links_path, params: { custom_link: valid_attributes }
54+
end.to change(CustomLink, :count).by(1)
55+
end
56+
57+
it 'redirects to the edit_casa_org_path' do
58+
post custom_links_path, params: { custom_link: valid_attributes }
59+
expect(response).to redirect_to(edit_casa_org_path(user.casa_org))
60+
end
61+
62+
it 'sets a success notice' do
63+
post custom_links_path, params: { custom_link: valid_attributes }
64+
expect(flash[:notice]).to eq('Custom link was successfully created.')
65+
end
66+
67+
it 'authorizes the action' do
68+
expect_any_instance_of(CustomLinkPolicy).to receive(:create?).and_return(true)
69+
post custom_links_path, params: { custom_link: valid_attributes }
70+
end
71+
end
72+
73+
context 'with invalid parameters' do
74+
it 'does not create a new CustomLink' do
75+
expect do
76+
post custom_links_path, params: { custom_link: invalid_attributes }
77+
end.to change(CustomLink, :count).by(0)
78+
end
79+
80+
it 'renders the new template' do
81+
post custom_links_path, params: { custom_link: invalid_attributes }
82+
expect(response).to render_template(:new)
83+
end
84+
end
85+
end
86+
87+
describe 'PATCH/PUT #update' do
88+
context 'with valid parameters' do
89+
let(:new_attributes) { { text: 'Updated Text', url: 'http://updated.com' } }
90+
91+
it 'updates the requested custom_link' do
92+
patch custom_link_path(custom_link), params: { custom_link: new_attributes }
93+
custom_link.reload
94+
expect(custom_link.text).to eq('Updated Text')
95+
end
96+
97+
it 'redirects to the edit_casa_org_path' do
98+
patch custom_link_path(custom_link), params: { custom_link: new_attributes }
99+
expect(response).to redirect_to(edit_casa_org_path(user.casa_org))
100+
end
101+
102+
it 'sets a success notice' do
103+
patch custom_link_path(custom_link), params: { custom_link: new_attributes }
104+
expect(flash[:notice]).to eq('Custom link was successfully updated.')
105+
end
106+
107+
it 'authorizes the action' do
108+
expect_any_instance_of(CustomLinkPolicy).to receive(:update?).and_return(true)
109+
patch custom_link_path(custom_link), params: { custom_link: new_attributes }
110+
end
111+
end
112+
113+
context 'with invalid parameters' do
114+
it 'does not update the requested custom_link' do
115+
patch custom_link_path(custom_link), params: { custom_link: invalid_attributes }
116+
custom_link.reload
117+
expect(custom_link.text).not_to be_empty
118+
end
119+
120+
it 'renders the edit template' do
121+
patch custom_link_path(custom_link), params: { custom_link: invalid_attributes }
122+
expect(response).to render_template(:edit)
123+
end
124+
end
125+
end
126+
127+
describe 'DELETE #destroy' do
128+
it 'destroys the requested custom_link' do
129+
custom_link
130+
expect do
131+
delete custom_link_path(custom_link)
132+
end.to change(CustomLink, :count).by(-1)
133+
end
134+
135+
it 'redirects to the edit_casa_org_path' do
136+
delete custom_link_path(custom_link)
137+
expect(response).to redirect_to(edit_casa_org_path(user.casa_org))
138+
end
139+
140+
it 'sets a success notice' do
141+
delete custom_link_path(custom_link)
142+
expect(flash[:notice]).to eq('Custom link was successfully removed.')
143+
end
144+
145+
it 'authorizes the action' do
146+
expect_any_instance_of(CustomLinkPolicy).to receive(:destroy?).and_return(true)
147+
delete custom_link_path(custom_link)
148+
end
149+
end
150+
end

0 commit comments

Comments
 (0)