|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "dummy_rails_app" |
| 4 | +require "rails_helper" |
| 5 | + |
| 6 | +RSpec.describe TinyAdmin::Views::Components::FiltersForm do |
| 7 | + let(:component) { described_class.new } |
| 8 | + |
| 9 | + before do |
| 10 | + TinyAdmin::Settings.instance.load_settings |
| 11 | + component.update_attributes(section_path: "/admin/posts", filters: filters) |
| 12 | + end |
| 13 | + |
| 14 | + describe "with a boolean filter" do |
| 15 | + let(:field) { TinyAdmin::Field.new(name: "published", type: :boolean, title: "Published", options: {}) } |
| 16 | + let(:filters) { {field => {filter: {type: :boolean}, value: "1"}} } |
| 17 | + |
| 18 | + it "renders a select with true/false options" do |
| 19 | + html = component.call |
| 20 | + expect(html).to include("form-select") |
| 21 | + expect(html).to include("true") |
| 22 | + expect(html).to include("false") |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + describe "with a date filter" do |
| 27 | + let(:field) { TinyAdmin::Field.new(name: "created_at", type: :date, title: "Created At", options: {}) } |
| 28 | + let(:filters) { {field => {filter: {type: :date}, value: "2024-01-01"}} } |
| 29 | + |
| 30 | + it "renders a date input" do |
| 31 | + html = component.call |
| 32 | + expect(html).to include('type="date"') |
| 33 | + expect(html).to include("2024-01-01") |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe "with a datetime filter" do |
| 38 | + let(:field) { TinyAdmin::Field.new(name: "updated_at", type: :datetime, title: "Updated At", options: {}) } |
| 39 | + let(:filters) { {field => {filter: {type: :datetime}, value: "2024-01-01T12:00"}} } |
| 40 | + |
| 41 | + it "renders a datetime-local input" do |
| 42 | + html = component.call |
| 43 | + expect(html).to include('type="datetime-local"') |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe "with an integer filter" do |
| 48 | + let(:field) { TinyAdmin::Field.new(name: "age", type: :integer, title: "Age", options: {}) } |
| 49 | + let(:filters) { {field => {filter: {type: :integer}, value: "25"}} } |
| 50 | + |
| 51 | + it "renders a number input" do |
| 52 | + html = component.call |
| 53 | + expect(html).to include('type="number"') |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + describe "with a select filter" do |
| 58 | + let(:field) { TinyAdmin::Field.new(name: "state", type: :select, title: "State", options: {}) } |
| 59 | + let(:filters) { {field => {filter: {type: :select, values: %w[available unavailable]}, value: "available"}} } |
| 60 | + |
| 61 | + it "renders a select with the provided values" do |
| 62 | + html = component.call |
| 63 | + expect(html).to include("form-select") |
| 64 | + expect(html).to include("available") |
| 65 | + expect(html).to include("unavailable") |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + describe "with a text filter (default)" do |
| 70 | + let(:field) { TinyAdmin::Field.new(name: "title", type: :string, title: "Title", options: {}) } |
| 71 | + let(:filters) { {field => {filter: {}, value: "hello"}} } |
| 72 | + |
| 73 | + it "renders a text input" do |
| 74 | + html = component.call |
| 75 | + expect(html).to include('type="text"') |
| 76 | + expect(html).to include("hello") |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + describe "action buttons" do |
| 81 | + let(:field) { TinyAdmin::Field.new(name: "title", type: :string, title: "Title", options: {}) } |
| 82 | + let(:filters) { {field => {filter: {}, value: ""}} } |
| 83 | + |
| 84 | + it "renders Clear and Filter buttons" do |
| 85 | + html = component.call |
| 86 | + expect(html).to include("Clear") |
| 87 | + expect(html).to include("Filter") |
| 88 | + end |
| 89 | + end |
| 90 | +end |
0 commit comments