|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe 'Selectize inputs', type: :system do |
| 4 | + let(:authors) do |
| 5 | + 3.times.map do |i| |
| 6 | + Author.create!(email: "some_email_#{i}@example.com", name: "John #{i}", age: 30 + i * 3) |
| 7 | + end |
| 8 | + end |
| 9 | + let(:post) { Post.create!(title: 'Test', author: authors.last) } |
| 10 | + let(:tags) do |
| 11 | + 3.times.map do |i| |
| 12 | + Tag.create!(name: "A tag #{i}") |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + before do |
| 17 | + post.tags << tags.last |
| 18 | + end |
| 19 | + |
| 20 | + after do |
| 21 | + post.destroy |
| 22 | + authors.each(&:destroy) |
| 23 | + tags.each(&:destroy) |
| 24 | + end |
| 25 | + |
| 26 | + context 'with a single value selectize input' do |
| 27 | + let(:hidden_input) { '#post_author_input [data-selectize-input]' } |
| 28 | + let(:selectize_control) { '#post_author_input .selectize-control.single' } |
| 29 | + let(:selectize_input) { '#post_author_input .selectize-input' } |
| 30 | + |
| 31 | + it 'includes the hidden select and the selectize-input element' do |
| 32 | + visit "/admin/posts/#{post.id}/edit" |
| 33 | + |
| 34 | + expect(page).to have_select('post[author_id]', visible: false, selected: authors.last.name) |
| 35 | + expect(page).to have_css(selectize_input) |
| 36 | + end |
| 37 | + |
| 38 | + it 'updates the entity association' do |
| 39 | + visit "/admin/posts/#{post.id}/edit" |
| 40 | + |
| 41 | + find(selectize_input).click |
| 42 | + find("#{selectize_control} .selectize-dropdown-content", text: 'John 1').click |
| 43 | + find('[type="submit"]').click |
| 44 | + expect(page).to have_content('was successfully updated') |
| 45 | + expect(post.reload.author).to eq(authors.find { |item| item.name == 'John 1' }) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context 'with a multiple values selectize input' do |
| 50 | + let(:hidden_input) { '#post_tags_input [data-selectize-input]' } |
| 51 | + let(:selectize_control) { '#post_tags_input .selectize-control.multi' } |
| 52 | + let(:selectize_input) { '#post_tags_input .selectize-input' } |
| 53 | + |
| 54 | + it 'includes the hidden select and the selectize-input element' do |
| 55 | + visit "/admin/posts/#{post.id}/edit" |
| 56 | + |
| 57 | + expect(page).to have_select('post[tag_ids][]', visible: false, selected: tags.last.name) |
| 58 | + expect(page).to have_css(selectize_input) |
| 59 | + end |
| 60 | + |
| 61 | + it 'updates the entity association' do |
| 62 | + visit "/admin/posts/#{post.id}/edit" |
| 63 | + |
| 64 | + find(selectize_input).click |
| 65 | + find('#post_tags_input .option', text: 'A tag 1').click |
| 66 | + scroll_to(find('#post_submit_action')) |
| 67 | + find('[type="submit"]').click |
| 68 | + expect(page).to have_content('was successfully updated') |
| 69 | + expect(post.reload.tags).to include(tags.find { |item| item.name == 'A tag 1' }) |
| 70 | + end |
| 71 | + end |
| 72 | +end |
0 commit comments