0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-23 18:00:29 +01:00
discourse-custom-wizard/spec/components/custom_wizard/submission_spec.rb

116 Zeilen
4,5 KiB
Ruby

# frozen_string_literal: true
describe CustomWizard::Submission do
fab!(:user) { Fabricate(:user) }
fab!(:user2) { Fabricate(:user) }
2021-09-07 14:06:13 +02:00
let(:template_json) { get_wizard_fixture("wizard") }
2023-01-18 19:53:36 +01:00
let(:guest_id) { CustomWizard::Wizard.generate_guest_id }
before do
CustomWizard::Template.save(template_json, skip_jobs: true)
@wizard = CustomWizard::Wizard.create(template_json["id"], user)
2021-09-01 05:10:49 +02:00
described_class.new(@wizard, step_1_field_1: "I am user submission").save
end
it "saves a user's submission" do
expect(
2023-01-18 19:53:36 +01:00
described_class.get(@wizard).fields["step_1_field_1"]
2021-09-01 05:10:49 +02:00
).to eq("I am user submission")
end
2023-01-18 19:53:36 +01:00
it "saves a guest's submission" do
CustomWizard::Template.save(template_json, skip_jobs: true)
@wizard = CustomWizard::Wizard.create(template_json["id"], nil, guest_id)
described_class.new(@wizard, step_1_field_1: "I am guest submission").save
expect(
described_class.get(@wizard).fields["step_1_field_1"]
).to eq("I am guest submission")
end
describe "#list" do
2021-09-01 05:10:49 +02:00
before do
freeze_time Time.now
2021-09-01 05:10:49 +02:00
template_json_2 = template_json.dup
template_json_2["id"] = "super_mega_fun_wizard_2"
CustomWizard::Template.save(template_json_2, skip_jobs: true)
2021-09-01 05:10:49 +02:00
@wizard2 = CustomWizard::Wizard.create(template_json["id"], user2)
@wizard3 = CustomWizard::Wizard.create(template_json_2["id"], user)
@count = CustomWizard::Submission::PAGE_LIMIT + 20
@count.times do |index|
described_class.new(@wizard, step_1_field_1: "I am user submission #{index + 1}", submitted_at: Time.now + (index + 1).minutes).save
2021-09-01 05:10:49 +02:00
end
described_class.new(@wizard2, step_1_field_1: "I am another user's submission").save
described_class.new(@wizard3, step_1_field_1: "I am a user submission on another wizard").save
end
it "list submissions by wizard" do
2023-01-18 19:53:36 +01:00
@wizard.user = nil
2021-09-01 05:10:49 +02:00
expect(described_class.list(@wizard).total).to eq(@count + 2)
end
2021-07-14 08:04:19 +02:00
2021-09-01 05:10:49 +02:00
it "list submissions by wizard and user" do
2023-01-18 19:53:36 +01:00
@wizard.user = user
expect(described_class.list(@wizard).total).to eq(@count + 1)
2021-09-01 05:10:49 +02:00
end
it "paginates submission lists" do
2023-01-18 19:53:36 +01:00
@wizard.user = nil
2021-09-01 05:10:49 +02:00
expect(described_class.list(@wizard, page: 1).submissions.size).to eq((@count + 2) - CustomWizard::Submission::PAGE_LIMIT)
end
it "orders submissions by submitted_at" do
expect(described_class.list(@wizard).submissions.first.submitted_at.to_datetime.change(usec: 0)).to eq((Time.now + @count.minutes).change(usec: 0))
end
end
2022-07-28 21:37:10 +02:00
describe "#cleanup_incomplete_submissions" do
it "cleans up redundant incomplete submissions on each build" do
freeze_time Time.now + 1
described_class.new(@wizard, step_1_field_1: "I am the second submission").save
builder = CustomWizard::Builder.new(@wizard.id, @wizard.user)
builder.build
2023-01-18 19:53:36 +01:00
submissions = described_class.list(@wizard).submissions
2021-09-01 05:10:49 +02:00
expect(submissions.length).to eq(1)
expect(submissions.first.fields["step_1_field_1"]).to eq("I am the second submission")
end
it "handles submissions without 'updated_at' field correctly" do
described_class.new(@wizard, step_1_field_1: "I am the second submission").save
described_class.new(@wizard, step_1_field_1: "I am the third submission").save
sub_data = PluginStore.get("#{@wizard.id}_submissions", @wizard.user.id)
sub_data.each do |sub|
sub['updated_at'] = nil
end
PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, sub_data)
builder = CustomWizard::Builder.new(@wizard.id, @wizard.user)
builder.build
2023-01-18 19:53:36 +01:00
submissions = described_class.list(@wizard).submissions
2021-09-01 05:10:49 +02:00
expect(submissions.length).to eq(1)
2022-10-05 17:29:21 +02:00
expect(submissions.first.fields["step_1_field_1"]).to eq("I am the second submission")
end
it "handles submissions with and without 'updated_at' field correctly" do
freeze_time Time.now + 1
described_class.new(@wizard, step_1_field_1: "I am the second submission").save
freeze_time Time.now + 2
described_class.new(@wizard, step_1_field_1: "I am the third submission").save
sub_data = PluginStore.get("#{@wizard.id}_submissions", @wizard.user.id)
sub_data[0]['updated_at'] = nil
PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, sub_data)
builder = CustomWizard::Builder.new(@wizard.id, @wizard.user)
builder.build
2023-01-18 19:53:36 +01:00
submissions = described_class.list(@wizard).submissions
2021-09-01 05:10:49 +02:00
expect(submissions.length).to eq(1)
expect(submissions.first.fields["step_1_field_1"]).to eq("I am the third submission")
end
end
2021-06-23 08:15:17 +02:00
end