0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-30 04:30:29 +01:00
discourse-custom-wizard/spec/extensions/custom_field_extensions_spec.rb

124 Zeilen
3,4 KiB
Ruby

# frozen_string_literal: true
describe "custom field extensions" do
fab!(:topic) { Fabricate(:topic) }
fab!(:post) { Fabricate(:post) }
fab!(:category) { Fabricate(:category) }
fab!(:group) { Fabricate(:group) }
fab!(:user) { Fabricate(:user) }
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
let(:custom_field_json) { get_wizard_fixture("custom_field/custom_fields") }
2021-09-24 11:58:42 +02:00
let(:subscription_custom_field_json) { get_wizard_fixture("custom_field/subscription_custom_fields") }
2021-03-11 07:30:15 +01:00
before do
custom_field_json['custom_fields'].each do |field_json|
custom_field = CustomWizard::CustomField.new(nil, field_json)
custom_field.save
end
end
2021-03-11 07:30:15 +01:00
context "topic" do
it "registers topic custom fields" do
topic
expect(Topic.get_custom_field_descriptor("topic_field_1").type).to eq(:boolean)
end
2021-03-11 07:30:15 +01:00
it "adds topic custom fields to the topic_view serializer" do
topic.custom_fields["topic_field_1"] = true
topic.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
serializer = TopicViewSerializer.new(
TopicView.new(topic.id, user),
scope: Guardian.new(user),
root: false
).as_json
2021-03-11 07:30:15 +01:00
expect(serializer[:topic_field_1]).to eq(true)
end
2021-03-11 07:30:15 +01:00
it "adds topic custom fields to the topic_list_item serializer" do
topic.custom_fields["topic_field_1"] = true
topic.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
serializer = TopicListItemSerializer.new(
topic,
scope: Guardian.new(user),
root: false
).as_json
2021-03-11 07:30:15 +01:00
expect(serializer[:topic_field_1]).to eq(true)
end
end
2021-03-11 07:30:15 +01:00
context "post" do
it "registers post custom fields" do
post
expect(Post.get_custom_field_descriptor("post_field_1").type).to eq(:integer)
end
2021-03-11 07:30:15 +01:00
it "adds post custom fields to the post serializer" do
post.custom_fields["post_field_1"] = 7
post.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
serializer = PostSerializer.new(
post,
scope: Guardian.new(user),
root: false
).as_json
2021-03-11 07:30:15 +01:00
expect(serializer[:post_field_1]).to eq(7)
end
end
2021-03-11 07:30:15 +01:00
2021-09-24 11:58:42 +02:00
context "subscription custom fields" do
2021-09-07 14:06:13 +02:00
before do
enable_subscription("business")
2021-09-07 14:06:13 +02:00
2021-09-24 11:58:42 +02:00
subscription_custom_field_json['custom_fields'].each do |field_json|
2021-09-07 14:06:13 +02:00
custom_field = CustomWizard::CustomField.new(nil, field_json)
custom_field.save
end
end
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
context "category" do
it "registers" do
category
expect(Category.get_custom_field_descriptor("category_field_1").type).to eq(:json)
2021-09-07 14:06:13 +02:00
end
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
it "adds custom fields to the basic category serializer" do
category.custom_fields["category_field_1"] = { a: 1, b: 2 }.to_json
category.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
serializer = BasicCategorySerializer.new(
category,
scope: Guardian.new(user),
root: false
).as_json
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
expect(serializer[:category_field_1]).to eq({ a: 1, b: 2 }.to_json)
end
end
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
context "group" do
it "registers" do
group
expect(Group.get_custom_field_descriptor("group_field_1").type).to eq(:string)
2021-09-07 14:06:13 +02:00
end
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
it "adds custom fields to the basic group serializer" do
group.custom_fields["group_field_1"] = "Hello"
group.save_custom_fields(true)
serializer = BasicGroupSerializer.new(
group,
scope: Guardian.new(user),
root: false
).as_json
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
expect(serializer[:group_field_1]).to eq("Hello")
end
end
end
2021-03-11 07:30:15 +01:00
end