0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-26 19:10:29 +01:00
discourse-custom-wizard/spec/serializers/custom_wizard/wizard_serializer_spec.rb

104 Zeilen
3,8 KiB
Ruby

2020-11-03 01:24:20 +01:00
# frozen_string_literal: true
describe CustomWizard::WizardSerializer do
fab!(:user) { Fabricate(:user) }
fab!(:category) { Fabricate(:category) }
2021-09-07 14:06:13 +02:00
let(:template) { get_wizard_fixture("wizard") }
let(:similar_topics_validation) { get_wizard_fixture("field/validation/similar_topics") }
2022-03-25 17:08:24 +01:00
let(:advanced_fields) { get_wizard_fixture("field/advanced_types") }
2020-11-03 01:24:20 +01:00
before do
2021-09-07 14:06:13 +02:00
CustomWizard::Template.save(template, skip_jobs: true)
2020-11-03 01:24:20 +01:00
@template = CustomWizard::Template.find('super_mega_fun_wizard')
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it 'should return the wizard attributes' do
json = CustomWizard::WizardSerializer.new(
CustomWizard::Builder.new(@template[:id], user).build,
scope: Guardian.new(user)
).as_json
expect(json[:wizard][:id]).to eq("super_mega_fun_wizard")
expect(json[:wizard][:name]).to eq("Super Mega Fun Wizard")
expect(json[:wizard][:background]).to eq("#333333")
expect(json[:wizard][:required]).to eq(false)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it 'should return the wizard steps' do
json = CustomWizard::WizardSerializer.new(
CustomWizard::Builder.new(@template[:id], user).build,
scope: Guardian.new(user)
).as_json
expect(json[:wizard][:steps].length).to eq(3)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "should return the wizard user attributes" do
json = CustomWizard::WizardSerializer.new(
CustomWizard::Builder.new(@template[:id], user).build,
scope: Guardian.new(user)
).as_json
expect(
json[:wizard][:user]
).to eq(BasicUserSerializer.new(user, root: false).as_json)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "should not return categories if there are no category fields" do
@template[:steps][2][:fields].delete_at(2)
CustomWizard::Template.save(@template)
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
json = CustomWizard::WizardSerializer.new(
CustomWizard::Builder.new(@template[:id], user).build,
scope: Guardian.new(user)
).as_json
expect(json[:wizard][:categories].present?).to eq(false)
expect(json[:wizard][:uncategorized_category_id].present?).to eq(false)
2021-03-11 07:30:15 +01:00
end
2022-03-25 17:08:24 +01:00
context "advanced fields" do
before do
enable_subscription("standard")
@template[:steps][0][:fields].push(*advanced_fields['fields'])
CustomWizard::Template.save(@template, skip_jobs: true)
end
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
it "should return categories if there is a category selector field" do
json = CustomWizard::WizardSerializer.new(
CustomWizard::Builder.new(@template[:id], user).build,
scope: Guardian.new(user)
).as_json
expect(json[:wizard][:categories].present?).to eq(true)
expect(json[:wizard][:uncategorized_category_id].present?).to eq(true)
end
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
it "should return categories if there is a similar topics validation scoped to category(s)" do
@template[:steps][0][:fields][0][:validations] = similar_topics_validation[:validations]
CustomWizard::Template.save(@template)
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
json = CustomWizard::WizardSerializer.new(
CustomWizard::Builder.new(@template[:id], user).build,
scope: Guardian.new(user)
).as_json
expect(json[:wizard][:categories].present?).to eq(true)
expect(json[:wizard][:uncategorized_category_id].present?).to eq(true)
end
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
it 'should return groups if there is a group selector field' do
json = CustomWizard::WizardSerializer.new(
CustomWizard::Builder.new(@template[:id], user).build,
scope: Guardian.new(user)
).as_json
expect(json[:wizard][:groups].length).to eq(8)
end
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
it 'should not return groups if there is not a group selector field' do
@template[:steps][0][:fields].reject! { |f| f["type"] === "group" }
CustomWizard::Template.save(@template)
json = CustomWizard::WizardSerializer.new(
CustomWizard::Builder.new(@template[:id], user).build,
scope: Guardian.new(user)
).as_json
expect(json[:wizard][:groups].present?).to eq(false)
end
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
end