From a916fe44701b4d3a56678e51f21dfb2eb8be1fa4 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Tue, 10 Jun 2025 11:18:33 +0200 Subject: [PATCH] Add tests --- .../collections/card_previews_controller.rb | 1 + .../card_previews/index.turbo_stream.erb | 2 +- .../card_previews_controller_test.rb | 44 +++++++++++++++ .../public/collections_controller_test.rb | 23 ++++++++ test/models/collection/publishable_test.rb | 55 +++++++++++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 test/controllers/public/collections/card_previews_controller_test.rb create mode 100644 test/controllers/public/collections_controller_test.rb create mode 100644 test/models/collection/publishable_test.rb diff --git a/app/controllers/public/collections/card_previews_controller.rb b/app/controllers/public/collections/card_previews_controller.rb index 753422ab5..370f099ba 100644 --- a/app/controllers/public/collections/card_previews_controller.rb +++ b/app/controllers/public/collections/card_previews_controller.rb @@ -18,6 +18,7 @@ class Public::Collections::CardPreviewsController < ApplicationController @collection.cards.closed.recently_closed_first else head :bad_request + Card.none end end end diff --git a/app/views/public/collections/card_previews/index.turbo_stream.erb b/app/views/public/collections/card_previews/index.turbo_stream.erb index 1877fd968..18a546ddc 100644 --- a/app/views/public/collections/card_previews/index.turbo_stream.erb +++ b/app/views/public/collections/card_previews/index.turbo_stream.erb @@ -1,7 +1,7 @@ <%= turbo_stream.remove "#{params[:target]}-load-page-#{@page.number}" %> <%= turbo_stream.append params[:target] do %> - <%= render partial: "cards/display/preview", + <%= render partial: "cards/display/public_preview", collection: @page.records, as: :card, cached: true %> <% unless @page.last? %> diff --git a/test/controllers/public/collections/card_previews_controller_test.rb b/test/controllers/public/collections/card_previews_controller_test.rb new file mode 100644 index 000000000..fd969e267 --- /dev/null +++ b/test/controllers/public/collections/card_previews_controller_test.rb @@ -0,0 +1,44 @@ +require "test_helper" + +class Public::Collections::CardPreviewsControllerTest < ActionDispatch::IntegrationTest + setup do + sign_in_as :kevin + + collections(:writebook).publish + end + + test "render considering cards" do + assert cards(:text).considering? + assert_not cards(:logo).considering? + + get public_collection_card_previews_path(collections(:writebook).publication.key, target: "considering-cards", format: :turbo_stream) + + assert_select ".card", text: /#{cards(:text).title}/ + assert_select ".card", text: cards(:logo).title, count: 0 + end + + test "render doing cards" do + assert cards(:logo).doing? + assert_not cards(:text).doing? + + get public_collection_card_previews_path(collections(:writebook).publication.key, target: "doing-cards", format: :turbo_stream) + + assert_select ".card", text: /#{cards(:logo).title}/ + assert_select ".card", text: cards(:text).title, count: 0 + end + + test "render closed cards" do + assert cards(:shipping).closed? + assert_not cards(:text).doing? + + get public_collection_card_previews_path(collections(:writebook).publication.key, target: "closed-cards", format: :turbo_stream) + + assert_select ".card", text: /#{cards(:shipping).title}/ + assert_select ".card", text: cards(:text).title, count: 0 + end + + test "bad response for unknown target" do + get public_collection_card_previews_path(collections(:writebook).publication.key, format: :turbo_stream) + assert_response :bad_request + end +end diff --git a/test/controllers/public/collections_controller_test.rb b/test/controllers/public/collections_controller_test.rb new file mode 100644 index 000000000..0cb691c89 --- /dev/null +++ b/test/controllers/public/collections_controller_test.rb @@ -0,0 +1,23 @@ +require "test_helper" + +class Public::CollectionsControllerTest < ActionDispatch::IntegrationTest + setup do + sign_in_as :kevin + + collections(:writebook).publish + end + + test "show" do + get published_collection_path(collections(:writebook)) + assert_response :success + end + + test "not found if the collection is not published" do + key = collections(:writebook).publication.key + + collections(:writebook).unpublish + get public_collection_path(key) + + assert_response :not_found + end +end diff --git a/test/models/collection/publishable_test.rb b/test/models/collection/publishable_test.rb new file mode 100644 index 000000000..ff6424104 --- /dev/null +++ b/test/models/collection/publishable_test.rb @@ -0,0 +1,55 @@ +require "test_helper" + +class Collection::PublishableTest < ActiveSupport::TestCase + setup do + Current.session = sessions(:david) + end + + test "published scope" do + collections(:writebook).publish + assert_includes Collection.published, collections(:writebook) + assert_not_includes Collection.published, collections(:private) + end + + test "published?" do + assert_not collections(:writebook).published? + collections(:writebook).publish + assert collections(:writebook).published? + end + + test "publish and unpublish" do + assert_not collections(:writebook).published? + + assert_difference -> { Collection::Publication.count }, +1 do + collections(:writebook).publish + end + + assert collections(:writebook).published? + + assert_difference -> { Collection::Publication.count }, -1 do + collections(:writebook).unpublish + end + + assert_not collections(:writebook).reload.published? + end + + test "find collection by publication key" do + collections(:writebook).publish + assert_equal collections(:writebook), Collection.find_by_published_key(collections(:writebook).publication.key) + + assert_raise ActiveRecord::RecordNotFound do + Collection.find_by_published_key("invalid") + end + end + + test "publish doesn't create duplicate publications" do + collections(:writebook).publish + original_publication = collections(:writebook).publication + + assert_no_difference -> { Collection::Publication.count } do + collections(:writebook).publish + end + + assert_equal original_publication, collections(:writebook).reload.publication + end +end