discourse/plugins/chat/spec/models/category_spec.rb
Joffrey JAFFEUX 12a18d4d55
DEV: properly namespace chat (#20690)
This commit main goal was to comply with Zeitwerk and properly rely on autoloading. To achieve this, most resources have been namespaced under the `Chat` module.

- Given all models are now namespaced with `Chat::` and would change the stored types in DB when using polymorphism or STI (single table inheritance), this commit uses various Rails methods to ensure proper class is loaded and the stored name in DB is unchanged, eg: `Chat::Message` model will be stored as `"ChatMessage"`, and `"ChatMessage"` will correctly load `Chat::Message` model.
- Jobs are now using constants only, eg: `Jobs::Chat::Foo` and should only be enqueued this way

Notes:
- This commit also used this opportunity to limit the number of registered css files in plugin.rb
- `discourse_dev` support has been removed within this commit and will be reintroduced later

<!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
2023-03-17 14:24:38 +01:00

55 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require "rails_helper"
RSpec.describe Category do
it_behaves_like "a chatable model" do
fab!(:chatable) { Fabricate(:category) }
let(:channel_class) { Chat::CategoryChannel }
end
it { is_expected.to have_one(:category_channel).dependent(:destroy) }
describe "#cannot_delete_reason" do
subject(:reason) { category.cannot_delete_reason }
context "when a chat channel is present" do
let(:channel) { Fabricate(:category_channel) }
let(:category) { channel.chatable }
it "returns a message" do
expect(reason).to match I18n.t("category.cannot_delete.has_chat_channels")
end
end
end
describe "#deletable_for_chat?" do
subject(:category) { Fabricate.build(:category) }
context "when no category channel is present" do
it "returns true" do
expect(category).to be_deletable_for_chat
end
end
context "when a category channel is present" do
let(:channel) { Fabricate(:category_channel) }
let(:category) { channel.chatable }
context "when it has chat messages" do
before { Fabricate(:chat_message, chat_channel: channel) }
it "returns false" do
expect(category).not_to be_deletable_for_chat
end
end
context "when it has no chat messages" do
it "returns true" do
expect(category).to be_deletable_for_chat
end
end
end
end
end