2021-09-06 11:25:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative '../../plugin_helper'
|
|
|
|
|
|
|
|
describe CustomWizard::SubmissionSerializer do
|
2023-03-15 13:33:07 +01:00
|
|
|
fab!(:user1) { Fabricate(:user) }
|
|
|
|
fab!(:user2) { Fabricate(:user) }
|
2021-09-06 11:25:08 +02:00
|
|
|
|
|
|
|
let(:template_json) {
|
|
|
|
JSON.parse(File.open(
|
|
|
|
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
|
|
|
|
).read)
|
|
|
|
}
|
|
|
|
|
|
|
|
before do
|
|
|
|
CustomWizard::Template.save(template_json, skip_jobs: true)
|
2023-03-15 13:33:07 +01:00
|
|
|
wizard = CustomWizard::Wizard.create(template_json["id"], user1)
|
2021-09-06 11:25:08 +02:00
|
|
|
CustomWizard::Submission.new(wizard,
|
|
|
|
step_1_field_1: "I am user submission",
|
|
|
|
submitted_at: Time.now.iso8601
|
|
|
|
).save
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return submission attributes' do
|
2023-03-15 13:33:07 +01:00
|
|
|
wizard = CustomWizard::Wizard.create(template_json["id"])
|
|
|
|
list = CustomWizard::Submission.list(wizard, page: 0)
|
|
|
|
|
2021-09-06 11:25:08 +02:00
|
|
|
json_array = ActiveModel::ArraySerializer.new(
|
2023-03-15 13:33:07 +01:00
|
|
|
list.submissions,
|
2021-09-06 11:25:08 +02:00
|
|
|
each_serializer: described_class
|
|
|
|
).as_json
|
|
|
|
|
|
|
|
expect(json_array.length).to eq(1)
|
|
|
|
expect(json_array[0][:id].present?).to eq(true)
|
2023-03-15 13:33:07 +01:00
|
|
|
expect(json_array[0][:user]).to eq(
|
|
|
|
BasicUserSerializer.new(user1, root: false).as_json
|
|
|
|
)
|
2021-09-06 11:25:08 +02:00
|
|
|
expect(json_array[0][:submitted_at].present?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return field values, types and labels" do
|
2023-03-15 13:33:07 +01:00
|
|
|
wizard = CustomWizard::Wizard.create(template_json["id"])
|
|
|
|
list = CustomWizard::Submission.list(wizard, page: 0)
|
|
|
|
|
2021-09-06 11:25:08 +02:00
|
|
|
json_array = ActiveModel::ArraySerializer.new(
|
2023-03-15 13:33:07 +01:00
|
|
|
list.submissions,
|
2021-09-06 11:25:08 +02:00
|
|
|
each_serializer: described_class
|
|
|
|
).as_json
|
|
|
|
|
|
|
|
expect(json_array.length).to eq(1)
|
|
|
|
expect(json_array[0][:fields].as_json).to eq({
|
|
|
|
"step_1_field_1": {
|
|
|
|
"value": "I am user submission",
|
|
|
|
"type": "text",
|
|
|
|
"label": "Text"
|
|
|
|
}
|
|
|
|
}.as_json)
|
|
|
|
end
|
|
|
|
end
|