0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-26 02:50:28 +01:00
discourse-custom-wizard/spec/components/custom_wizard/template_spec.rb

163 Zeilen
5,8 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2020-11-03 01:24:20 +01:00
describe CustomWizard::Template do
2024-10-16 13:52:03 +02:00
fab!(:user)
2021-09-07 14:06:13 +02:00
let(:template_json) { get_wizard_fixture("wizard") }
let(:permitted_json) { get_wizard_fixture("wizard/permitted") }
2024-10-16 13:52:03 +02:00
fab!(:upload)
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
before { CustomWizard::Template.save(template_json, skip_jobs: true) }
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "saves wizard templates" do
expect(
2024-10-16 13:52:03 +02:00
PluginStoreRow.exists?(plugin_name: "custom_wizard", key: "super_mega_fun_wizard"),
2020-11-03 01:24:20 +01:00
).to eq(true)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "finds wizard templates" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Template.find("super_mega_fun_wizard")["id"]).to eq(
"super_mega_fun_wizard",
)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "removes wizard templates" do
2024-10-16 13:52:03 +02:00
CustomWizard::Template.remove("super_mega_fun_wizard")
expect(CustomWizard::Template.find("super_mega_fun_wizard")).to eq(nil)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
it "removes user wizard redirects if template is removed" do
2024-10-16 13:52:03 +02:00
user.custom_fields["redirect_to_wizard"] = "super_mega_fun_wizard"
user.save_custom_fields(true)
2024-10-16 13:52:03 +02:00
CustomWizard::Template.remove("super_mega_fun_wizard")
expect(user.reload.custom_fields["redirect_to_wizard"]).to eq(nil)
end
2020-11-03 01:24:20 +01:00
it "checks for wizard template existence" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Template.exists?("super_mega_fun_wizard")).to eq(true)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
context "upload references" do
it "are added if a wizard has a step banner" do
2024-10-16 13:52:03 +02:00
template_json["steps"][0]["banner"] = upload.url
template_json["steps"][0]["banner_upload_id"] = upload.id
CustomWizard::Template.save(template_json, skip_jobs: true)
wizard_record = CustomWizard::Template.find_record(template_json["id"])
expect(
UploadReference.exists?(
upload_id: upload.id,
target_type: "PluginStoreRow",
2024-10-16 13:52:03 +02:00
target_id: wizard_record.id,
),
).to eq(true)
end
it "are added if a wizard has a field image" do
2024-10-16 13:52:03 +02:00
template_json["steps"][0]["fields"][0]["image"] = upload.url
template_json["steps"][0]["fields"][0]["image_upload_id"] = upload.id
CustomWizard::Template.save(template_json, skip_jobs: true)
wizard_record = CustomWizard::Template.find_record(template_json["id"])
expect(
UploadReference.exists?(
upload_id: upload.id,
target_type: "PluginStoreRow",
2024-10-16 13:52:03 +02:00
target_id: wizard_record.id,
),
).to eq(true)
end
it "are removed if a wizard step banner is removed" do
2024-10-16 13:52:03 +02:00
template_json["steps"][0]["banner"] = upload.url
template_json["steps"][0]["banner_upload_id"] = upload.id
CustomWizard::Template.save(template_json, skip_jobs: true)
2024-10-16 13:52:03 +02:00
template_json["steps"][0]["banner"] = nil
template_json["steps"][0]["banner_upload_id"] = nil
CustomWizard::Template.save(template_json, skip_jobs: true)
wizard_record = CustomWizard::Template.find_record(template_json["id"])
2024-10-16 13:52:03 +02:00
expect(UploadReference.exists?(target_type: "PluginStoreRow")).to eq(false)
end
it "are removed if a wizard field image is removed" do
2024-10-16 13:52:03 +02:00
template_json["steps"][0]["fields"][0]["image"] = upload.url
template_json["steps"][0]["fields"][0]["image_upload_id"] = upload.id
CustomWizard::Template.save(template_json, skip_jobs: true)
2024-10-16 13:52:03 +02:00
template_json["steps"][0]["fields"][0]["image"] = nil
template_json["steps"][0]["fields"][0]["image_upload_id"] = nil
CustomWizard::Template.save(template_json, skip_jobs: true)
wizard_record = CustomWizard::Template.find_record(template_json["id"])
2024-10-16 13:52:03 +02:00
expect(UploadReference.exists?(target_type: "PluginStoreRow")).to eq(false)
end
it "are removed if a wizard is removed" do
2024-10-16 13:52:03 +02:00
template_json["steps"][0]["fields"][0]["image"] = upload.url
template_json["steps"][0]["fields"][0]["image_upload_id"] = upload.id
CustomWizard::Template.save(template_json, skip_jobs: true)
CustomWizard::Template.remove(template_json["id"])
2024-10-16 13:52:03 +02:00
expect(UploadReference.exists?(target_type: "PluginStoreRow")).to eq(false)
end
end
2020-11-03 01:24:20 +01:00
context "wizard template list" do
before do
2024-10-16 13:52:03 +02:00
enable_subscription("standard")
2022-03-25 17:08:24 +01:00
2020-11-03 01:24:20 +01:00
template_json_2 = template_json.dup
2024-10-16 13:52:03 +02:00
template_json_2["id"] = "super_mega_fun_wizard_2"
template_json_2["permitted"] = permitted_json["permitted"]
2020-11-03 01:24:20 +01:00
CustomWizard::Template.save(template_json_2, skip_jobs: true)
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
template_json_3 = template_json.dup
2024-10-16 13:52:03 +02:00
template_json_3["id"] = "super_mega_fun_wizard_3"
2020-11-03 01:24:20 +01:00
template_json_3["after_signup"] = true
CustomWizard::Template.save(template_json_3, skip_jobs: true)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "works" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Template.list.length).to eq(3)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "can be filtered by wizard settings" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Template.list(setting: "after_signup").length).to eq(1)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "can be ordered" do
expect(
2024-10-16 13:52:03 +02:00
CustomWizard::Template.list(order: "(value::json ->> 'permitted') IS NOT NULL DESC").first[
"id"
],
).to eq("super_mega_fun_wizard_2")
2020-11-03 01:24:20 +01:00
end
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
context "after time setting" do
before do
freeze_time Time.now
2020-11-23 01:40:46 +01:00
@scheduled_time = (Time.now + 3.hours).iso8601
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
@after_time_template = template_json.dup
2020-11-23 01:40:46 +01:00
@after_time_template["after_time"] = true
@after_time_template["after_time_scheduled"] = @scheduled_time
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
it "if enabled queues jobs after wizard is saved" do
2020-11-23 01:40:46 +01:00
expect_enqueued_with(job: :set_after_time_wizard, at: Time.parse(@scheduled_time).utc) do
2020-11-03 01:24:20 +01:00
CustomWizard::Template.save(@after_time_template)
end
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
it "if disabled clears jobs after wizard is saved" do
2020-11-03 01:24:20 +01:00
CustomWizard::Template.save(@after_time_template)
2024-10-16 13:52:03 +02:00
@after_time_template["after_time"] = false
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
expect_not_enqueued_with(job: :set_after_time_wizard) do
CustomWizard::Template.save(@after_time_template)
end
2024-10-17 16:15:25 +02:00
expect(
UserCustomField.exists?(name: "redirect_to_wizard", value: @after_time_template[:id]),
).to eq(false)
2020-11-03 01:24:20 +01:00
end
end
2021-03-11 07:30:15 +01:00
end