# frozen_string_literal: true
require 'test_helper'
require 'ui_bibz/ui/ux/tables/components/store'
require 'ui_bibz/ui/ux/tables/extensions/paginable'
require 'ui_bibz/ui/ux/tables/extensions/searchable'
require 'ui_bibz/ui/ux/tables/extensions/sortable'
require 'ui_bibz/ui/ux/tables/extensions/actionable'
class TableTest < ActionView::TestCase
setup do
create_list(:user, 25)
params = ActionController::Parameters.new({
controller: 'users',
action: 'index',
sort: 'users.name_fr',
direction: 'asc',
search: 'Name fr',
per_page: 2,
page: 1,
only_path: true
})
@users = User.table_search_pagination(params, session)
@store = UiBibz::Ui::Ux::Tables::Store.new @users
end
test 'table search field' do
actual = UiBibz::Ui::Ux::Tables::TableSearchField.new({ store: @users }).render
expected = '
'
assert_equal expected, actual
end
test 'table pagination' do
actual = UiBibz::Ui::Ux::Tables::TablePagination.new({ store: @users }).render
expected = ''
assert_equal expected, actual
end
test 'table non sortable' do
options = { sortable: false }
actual = UiBibz::Ui::Ux::Tables::Sortable.new(@store, options).header(@store.columns.list.first)
expected = 'Id'
assert_equal expected, actual
end
test 'table sortable' do
options = { sortable: true }
actual = UiBibz::Ui::Ux::Tables::Sortable.new(@store, options).header(@store.columns.list.first)
expected = 'Id'
assert_equal expected, actual
end
test 'table non paginable' do
options = { paginable: false }
pagination = UiBibz::Ui::Ux::Tables::Paginable.new(@store, options)
actual = pagination.render if pagination.paginable?
assert_nil actual
end
test 'table paginable' do
options = { paginable: true }
pagination = UiBibz::Ui::Ux::Tables::Paginable.new(@store, options)
actual = pagination.render if pagination.paginable?
expected = "
"
assert_equal expected, actual
end
test 'table non searchable' do
options = { searchable: false }
actual = UiBibz::Ui::Ux::Tables::Searchable.new(@store, options).render
expected = ''
assert_equal expected, actual
end
test 'table non searchable with a title and glyph' do
options = { searchable: false, glyph: 'state', title: 'Title list' }
actual = UiBibz::Ui::Ux::Tables::Searchable.new(@store, options).render
expected = ''
assert_equal expected, actual
end
test 'table searchable' do
options = { searchable: true }
actual = UiBibz::Ui::Ux::Tables::Searchable.new(@store, options).render
expected = ''
assert_equal expected, actual
end
test 'table actionable header' do
options = { actionable: true }
action = UiBibz::Ui::Ux::Tables::Actionable.new(@store, options)
actual = action.header []
expected = [' | ']
assert_equal expected, actual
end
test 'table actionable body' do
options = { actionable: true }
action = UiBibz::Ui::Ux::Tables::Actionable.new(@store, options)
actual = action.body @store.records.first, []
expected = [' | ']
assert_equal expected, actual
end
test 'table actionable inject_url' do
options = { actionable: true }
action = UiBibz::Ui::Ux::Tables::Actionable.new(@store, options)
actual = action.inject_url 'http://localhost/users/id/test', @store.records.first
expected = 'http://localhost/users/1/test'
assert_equal expected, actual
end
test 'table non actionable header' do
options = { actionable: false }
action = UiBibz::Ui::Ux::Tables::Actionable.new(@store, options)
actual = action.header []
expected = []
assert_equal expected, actual
end
test 'table non actionable body' do
options = { actionable: false }
action = UiBibz::Ui::Ux::Tables::Actionable.new(@store, options)
actual = action.body @store.records.first, []
expected = []
assert_equal expected, actual
end
test 'simple table_card' do
actual = UiBibz::Ui::Ux::Tables::TableCard.new(store: @users, tap: true).render
end
test 'complex table_card' do
actual = UiBibz::Ui::Ux::Tables::TableCard.new({ store: @users, tap: true }, { class: 'state' }).tap do |pane|
pane.header 'Test header'
pane.body class: 'ui' do
'Test body'
end
end.render
end
test 'complex table_card with custom actions' do
actual = UiBibz::Ui::Ux::Tables::TableCard.new({ store: @users, tap: true }, { class: 'state' }).tap do |pane|
pane.header 'Test header'
pane.body cls: 'ui' do
'Test body'
end
pane.columns do |c|
c.column(:id, { name: '#' })
c.column(:name_fr, { name: 'Name fr', link: edit_user_path(:id), order: 2 })
c.column(:name_en, { name: 'Name en', order: 1 })
c.column(:name_en, { name: 'Name en', format: ->(_records, record) { "name #{record.id}" } })
end
pane.actions do |a|
a.link 'state', url: users_path(:id), glyph: 'eye'
a.divider
a.link 'momo', url: users_path(:id), glyph: 'home'
end
end.render
end
test 'format action' do
actual = UiBibz::Ui::Ux::Tables::TableCard.new(store: @users, tap: true).tap do |pane|
pane.columns do |cls|
cls.column :id, name: '#'
end
pane.actions do |acs|
acs.format do |record|
acs.action 'Action statique', url: '#'
acs.action "Action dynamique #{record.id}", url: '#'
end
end
end
end
test 'table visual options' do
table = UiBibz::Ui::Ux::Tables::Table.new(store: @users, striped: true, status: :inverse, responsive: true, bordered: true, size: :sm, hoverable: true, reflow: true).render
actual = Nokogiri::HTML(table).xpath('//table')[0].attributes['class'].value
expected = 'table-inverse table table-striped table-bordered table-hoverable table-sm table-responsive table-reflow'
assert_equal expected, actual
end
test 'table thead visual options' do
table = UiBibz::Ui::Ux::Tables::Table.new(store: @users, thead: { status: :default }).render
actual = Nokogiri::HTML(table).xpath('//thead')[0].attributes['class'].value
expected = 'thead-default'
assert_equal expected, actual
end
end