discourse/spec/system/admin_badges_spec.rb
chapoi 2ca06ba236
DEV: form-kit
This PR introduces FormKit, a component-based form library designed to simplify form creation and management. This library provides a single `Form` component, various field components, controls, validation mechanisms, and customization options. Additionally, it includes helpers to facilitate testing and writing specifications for forms.

1. **Form Component**:
   - The main component that encapsulates form logic and structure.
   - Yields various utilities like `Field`, `Submit`, `Alert`, etc.

   **Example Usage**:
   ```gjs
   import Form from "discourse/form";

   <template>
     <Form as |form|>
       <form.Field
         @name="username"
         @title="Username"
         @validation="required"
         as |field|
       >
         <field.Input />
       </form.Field>

       <form.Field @name="age" @title="Age" as |field|>
         <field.Input @type="number" />
       </form.Field>

       <form.Submit />
     </Form>
   </template>
   ```

2. **Validation**:
   - Built-in validation rules such as `required`, `number`, `length`, and `url`.
   - Custom validation callbacks for more complex validation logic.

   **Example Usage**:
   ```javascript
   validateUsername(name, value, data, { addError }) {
     if (data.bar / 2 === value) {
       addError(name, "That's not how maths work.");
     }
   }
   ```

   ```hbs
   <form.Field @name="username" @validate={{this.validateUsername}} />
   ```

3. **Customization**:
   - Plugin outlets for extending form functionality.
   - Styling capabilities through propagated attributes.
   - Custom controls with properties provided by `form` and `field`.

   **Example Usage**:
   ```hbs
   <Form class="my-form" as |form|>
     <form.Field class="my-field" as |field|>
       <MyCustomControl id={{field.id}} @onChange={{field.set}} />
     </form.Field>
   </Form>
   ```

4. **Helpers for Testing**:
   - Test assertions for form and field validation.

   **Example usage**:
   ```javascript
   assert.form().hasErrors("the form shows errors");
   assert.form().field("foo").hasValue("bar", "user has set the value");
   ```

   - Helper for interacting with he form

   **Example usage**:
   ```javascript
   await formKit().field("foo").fillIn("bar");
   ```

5. **Page Object for System Specs**:
   - Page objects for interacting with forms in system specs.
   - Methods for submitting forms, checking alerts, and interacting with fields.

   **Example Usage**:
   ```ruby
   form = PageObjects::Components::FormKit.new(".my-form")
   form.submit
   expect(form).to have_an_alert("message")
   ```

   **Field Interactions**:
   ```ruby
   field = form.field("foo")
   expect(field).to have_value("bar")
   field.fill_in("bar")
   ```


6. **Collections handling**:
   - A specific component to handle array of objects

   **Example Usage**:
   ```gjs
    <Form @data={{hash foo=(array (hash bar=1) (hash bar=2))}} as |form|>
      <form.Collection @name="foo" as |collection|>
        <collection.Field @name="bar" @title="Bar" as |field|>
          <field.Input />
        </collection.Field>
      </form.Collection>
    </Form>
   ```
2024-07-17 11:59:35 +02:00

79 lines
2.8 KiB
Ruby

# frozen_string_literal: true
describe "Admin Badges Page", type: :system do
before { SiteSetting.enable_badges = true }
fab!(:current_user) { Fabricate(:admin) }
let(:badges_page) { PageObjects::Pages::AdminBadges.new }
let(:form) { PageObjects::Components::FormKit.new("form") }
before { sign_in(current_user) }
context "with system badge" do
it "displays badge" do
badges_page.visit_page(Badge::Autobiographer)
badge = Badge.find(Badge::Autobiographer)
expect(form).to have_an_alert(I18n.t("admin_js.admin.badges.disable_system"))
expect(form.field("badge_type_id")).to be_disabled
expect(form.field("badge_type_id")).to have_value(BadgeType::Bronze.to_s)
expect(form.field("badge_grouping_id")).to be_disabled
expect(form.field("badge_grouping_id")).to have_value(BadgeGrouping::GettingStarted.to_s)
expect(form.field("allow_title")).to be_enabled
expect(form.field("allow_title")).to be_unchecked
expect(form.field("multiple_grant")).to be_disabled
expect(form.field("multiple_grant")).to be_unchecked
expect(form.field("listable")).to be_disabled
expect(form.field("listable")).to be_checked
expect(form.field("show_posts")).to be_disabled
expect(form.field("show_posts")).to be_unchecked
expect(form.field("icon")).to be_enabled
expect(form.field("icon")).to have_value("user-edit")
expect(find(".form-kit__container[data-name='name']")).to have_content(badge.name.strip)
expect(find(".form-kit__container[data-name='description']")).to have_content(
badge.description.strip,
)
expect(find(".form-kit__container[data-name='long_description']")).to have_content(
badge.long_description.strip,
)
end
end
context "when creating a badge" do
it "creates a badge" do
badges_page.new_page
form.field("enabled").accept
form.field("name").fill_in("a name")
form.field("badge_type_id").select(BadgeType::Bronze)
form.field("icon").select("ambulance")
form.field("description").fill_in("a description")
form.field("long_description").fill_in("a long_description")
form.field("badge_grouping_id").select(BadgeGrouping::GettingStarted)
form.field("allow_title").toggle
form.field("multiple_grant").toggle
form.field("listable").toggle
form.field("show_posts").toggle
form.submit
expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.saved"))
expect(badges_page).to have_badge("a name")
end
end
context "with enable_badge_sql" do
before { SiteSetting.enable_badge_sql = true }
it "shows the sql section" do
badges_page.new_page
form.field("query").fill_in("a query")
expect(form.field("auto_revoke")).to be_unchecked
expect(form.field("target_posts")).to be_unchecked
end
end
end