Skip to content

Commit

Permalink
add command specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ElviaBth committed Apr 8, 2024
1 parent 8cebf58 commit f7f32f7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 13 deletions.
26 changes: 13 additions & 13 deletions app/views/decidim/anonymous_codes/admin/code_groups/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
</tr>
</thead>
<tbody>
<% groups.each do |group| %>
<tr>
<td><%= translated_attribute(group.title) %></td>
<td><%= l(group.expires_at, format: :decidim_short) %></td>
<td><%= t("booleans.#{group.active}") %></td>
<td><%= group.tokens.count %></td>
<td><%= group.max_reuses %></td>
<td class="table-list__actions">
<%= icon_link_to "circle-x", code_group_path(group.id), t("actions.destroy", scope: "decidim.anonymous_codes.admin"), method: :delete, class: "action-icon--remove", data: { confirm: t("actions.confirm_destroy", scope: "decidim.anonymous_codes.admin") } %>
<%= icon_link_to "pencil", edit_code_group_path(group.id), t("actions.edit", scope: "decidim.anonymous_codes.admin"), class: "action-icon--edit" %>
<%= icon_link_to "eye", resource_path(group), t("actions.preview", scope: "decidim.anonymous_codes.admin"), class: "action-icon--preview#{group.resource} ? '':' invisible'", target: :_blank %>
</td>
</tr>
<% groups.each do |group| %>
<tr>
<td><%= translated_attribute(group.title) %></td>
<td><%= l(group.expires_at, format: :decidim_short) %></td>
<td><%= t("booleans.#{group.active}") %></td>
<td><%= group.tokens.count %></td>
<td><%= group.max_reuses %></td>
<td class="table-list__actions">
<%= icon_link_to "circle-x", code_group_path(group.id), t("actions.destroy", scope: "decidim.anonymous_codes.admin"), method: :delete, class: "action-icon--remove", data: { confirm: t("actions.confirm_destroy", scope: "decidim.anonymous_codes.admin") } %>
<%= icon_link_to "pencil", edit_code_group_path(group.id), t("actions.edit", scope: "decidim.anonymous_codes.admin"), class: "action-icon--edit" %>
<%= icon_link_to "eye", resource_path(group), t("actions.preview", scope: "decidim.anonymous_codes.admin"), class: "action-icon--preview#{group.resource} ? '':' invisible'", target: :_blank %>
</td>
</tr>
<% end %>
</tbody>
</table>
Expand Down
25 changes: 25 additions & 0 deletions spec/commands/admin/create_code_group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim
module AnonymousCodes
describe Group, type: :model do
let(:current_organization) { create(:organization) }

describe "#create" do
it "creates a code group successfully" do
code_group = Decidim::AnonymousCodes::Group.create(
title: "Sample Code Group",
expires_at: 10.days.from_now,
active: true,
max_reuses: 10,
organization: current_organization
)

expect(code_group).to be_valid
end
end
end
end
end
71 changes: 71 additions & 0 deletions spec/commands/admin/update_code_group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim
module AnonymousCodes
module Admin
describe UpdateCodeGroup do
let(:current_organization) { create(:organization) }
let(:current_user) { create(:user, :confirmed, :admin, organization: current_organization) }
let(:code_group) do
Decidim::AnonymousCodes::Group.create(
title: "Sample Code Group",
expires_at: 10.days.from_now,
active: true,
max_reuses: 10,
organization: current_organization
)
end
let(:form) do
CodeGroupForm.new(
title: "New Title",
expires_at: code_group.expires_at,
active: code_group.active,
max_reuses: code_group.max_reuses
)
end

subject { described_class.new(form, code_group) }

describe "#call" do
context "when the form is valid" do
it "updates the code group" do
expect(Decidim.traceability).to receive(:update!).with(
code_group,
current_user,
title: form.title,
expires_at: form.expires_at,
active: form.active,
max_reuses: form.max_reuses
)

subject.call
end

it "broadcasts :ok with the updated code group" do
expect(subject).to receive(:broadcast).with(:ok, code_group)
subject.call
end
end

context "when the form is invalid" do
before do
allow(form).to receive(:invalid?).and_return(true)
end

it "does not update the code group" do
expect(Decidim.traceability).not_to receive(:update!)
subject.call
end

it "broadcasts :invalid" do
expect(subject).to receive(:broadcast).with(:invalid)
subject.call
end
end
end
end
end
end
end

0 comments on commit f7f32f7

Please sign in to comment.