-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathindex_spec.rb
1280 lines (1148 loc) · 40.6 KB
/
index_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Index action', type: :request do
subject { page }
describe 'page' do
it 'shows "List of Models", should show filters and should show column headers' do
RailsAdmin.config.default_items_per_page = 1
2.times { FactoryBot.create :player } # two pages of players
visit index_path(model_name: 'player')
is_expected.to have_content('List of Players')
is_expected.to have_content('Created at')
is_expected.to have_content('Updated at')
# it "shows the show, edit and delete links" do
is_expected.to have_selector("li[title='Show'] a")
is_expected.to have_selector("li[title='Edit'] a")
is_expected.to have_selector("li[title='Delete'] a")
# it "has the search box with some prompt text" do
is_expected.to have_selector("input[placeholder='Filter']")
# https://github.com/railsadminteam/rails_admin/issues/362
# test that no link uses the "wildcard route" with the main
# controller and list method
# it "does not use the 'wildcard route'" do
is_expected.to have_selector("a[href*='all=true']") # make sure we're fully testing pagination
is_expected.to have_no_selector("a[href^='/rails_admin/main/list']")
end
end
describe 'css hooks' do
it 'is present' do
RailsAdmin.config Team do
list do
field :name
end
end
FactoryBot.create :team
visit index_path(model_name: 'team')
is_expected.to have_selector('th.header.string_type.name_field')
is_expected.to have_selector('td.string_type.name_field')
end
end
describe 'with querying and filtering' do
before do
@teams = Array.new(2) do
FactoryBot.create(:team)
end
@players = [
FactoryBot.create(:player, retired: true, injured: true, team: @teams[0]),
FactoryBot.create(:player, retired: true, injured: false, team: @teams[0]),
FactoryBot.create(:player, retired: false, injured: true, team: @teams[1]),
FactoryBot.create(:player, retired: false, injured: false, team: @teams[1]),
]
@comment = FactoryBot.create(:comment, commentable: @players[2])
end
it 'allows to query on any attribute' do
RailsAdmin.config Player do
list do
field :name
field :team
field :injured
field :retired
end
end
visit index_path(model_name: 'player', query: @players[0].name)
is_expected.to have_content(@players[0].name)
(1..3).each do |i|
is_expected.to have_no_content(@players[i].name)
end
end
it 'allows to clear the search query box', js: true do
visit index_path(model_name: 'player', query: @players[0].name)
is_expected.not_to have_content(@players[1].name)
find_button('Reset filters').click
is_expected.to have_content(@players[1].name)
end
it 'allows to filter on one attribute' do
RailsAdmin.config Player do
list do
field :name
field :team
field :injured
field :retired
end
end
visit index_path(model_name: 'player', f: {injured: {'1' => {v: 'true'}}})
is_expected.to have_content(@players[0].name)
is_expected.to have_no_content(@players[1].name)
is_expected.to have_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
end
it 'allows to combine filters on two different attributes' do
RailsAdmin.config Player do
list do
field :name
field :team
field :injured
field :retired
end
end
visit index_path(model_name: 'player', f: {retired: {'1' => {v: 'true'}}, injured: {'1' => {v: 'true'}}})
is_expected.to have_content(@players[0].name)
(1..3).each do |i|
is_expected.to have_no_content(@players[i].name)
end
end
it 'allows to filter on belongs_to relationships' do
RailsAdmin.config Player do
list do
field :name
field :team
field :injured
field :retired
end
end
visit index_path(model_name: 'player', f: {team: {'1' => {v: @teams[0].name}}})
is_expected.to have_content(@players[0].name)
is_expected.to have_content(@players[1].name)
is_expected.to have_no_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
end
it 'allows to filter on has_one relationships' do
@draft = FactoryBot.create(:draft, player: @players[1], college: 'University of Alabama')
RailsAdmin.config Player do
list do
field :name
field :draft do
searchable :college
end
end
end
visit index_path(model_name: 'player', f: {draft: {'1' => {v: 'Alabama'}}})
is_expected.to have_content(@players[1].name)
is_expected.to have_css('tbody .name_field', count: 1)
end
it 'allows to disable search on attributes' do
RailsAdmin.config Player do
list do
field :position
field :name do
searchable false
end
end
end
visit index_path(model_name: 'player', query: @players[0].name)
is_expected.to have_no_content(@players[0].name)
end
it 'allows to search a belongs_to attribute over the base table' do
RailsAdmin.config Player do
list do
field PK_COLUMN
field :name
field :team do
searchable Player => :team_id
end
end
end
visit index_path(model_name: 'player', f: {team: {'1' => {v: @teams.first.id}}})
is_expected.to have_content(@players[0].name)
is_expected.to have_content(@players[1].name)
is_expected.to have_no_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
end
it 'allows to search a belongs_to attribute over the target table' do
RailsAdmin.config Player do
list do
field PK_COLUMN
field :name
field :team do
searchable Team => :name
end
end
end
visit index_path(model_name: 'player', f: {team: {'1' => {v: @teams.first.name}}})
is_expected.to have_content(@players[0].name)
is_expected.to have_content(@players[1].name)
is_expected.to have_no_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
end
it 'allows to search a belongs_to attribute over the target table with a table name specified as a hash' do
RailsAdmin.config Player do
list do
field PK_COLUMN
field :name
field :team do
searchable teams: :name
end
end
end
visit index_path(model_name: 'player', f: {team: {'1' => {v: @teams.first.name}}})
is_expected.to have_content(@players[0].name)
is_expected.to have_content(@players[1].name)
is_expected.to have_no_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
end
it 'allows to search a belongs_to attribute over the target table with a table name specified as a string' do
RailsAdmin.config Player do
list do
field PK_COLUMN
field :name
field :team do
searchable 'teams.name'
end
end
end
visit index_path(model_name: 'player', f: {team: {'1' => {v: @teams.first.name}}})
is_expected.to have_content(@players[0].name)
is_expected.to have_content(@players[1].name)
is_expected.to have_no_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
end
it 'allows to search a belongs_to attribute over the label method by default' do
RailsAdmin.config Player do
list do
field PK_COLUMN
field :name
field :team
end
end
visit index_path(model_name: 'player', f: {team: {'1' => {v: @teams.first.name}}})
is_expected.to have_content(@players[0].name)
is_expected.to have_content(@players[1].name)
is_expected.to have_no_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
end
it 'allows to search a belongs_to attribute over the target table when an attribute is specified' do
RailsAdmin.config Player do
list do
field PK_COLUMN
field :name
field :team do
searchable :name
end
end
end
visit index_path(model_name: 'player', f: {team: {'1' => {v: @teams.first.name}}})
is_expected.to have_content(@players[0].name)
is_expected.to have_content(@players[1].name)
is_expected.to have_no_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
end
it 'allows to search over more than one attribute' do
RailsAdmin.config Player do
list do
field PK_COLUMN
field :name
field :team do
searchable [:name, {Player => :team_id}]
end
end
end
visit index_path(model_name: 'player', f: {team: {'1' => {v: @teams.first.name}, '2' => {v: @teams.first.id, o: 'is'}}})
is_expected.to have_content(@players[0].name)
is_expected.to have_content(@players[1].name)
is_expected.to have_no_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
# same with a different id
visit index_path(model_name: 'player', f: {team: {'1' => {v: @teams.first.name}, '2' => {v: @teams.last.id, o: 'is'}}})
is_expected.to have_no_content(@players[0].name)
is_expected.to have_no_content(@players[1].name)
is_expected.to have_no_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
end
it 'allows to search a has_many attribute over the target table' do
RailsAdmin.config Player do
list do
field PK_COLUMN
field :name
field :comments do
searchable :content
end
end
end
visit index_path(model_name: 'player', f: {comments: {'1' => {v: @comment.content}}})
is_expected.to have_no_content(@players[0].name)
is_expected.to have_no_content(@players[1].name)
is_expected.to have_content(@players[2].name)
is_expected.to have_no_content(@players[3].name)
end
it 'displays base filters when no filters are present in the params' do
RailsAdmin.config Player do
list { filters(%i[name team]) }
field :name do
default_filter_operator 'is'
end
field :team do
filterable true
end
end
visit index_path(model_name: 'player')
expect(JSON.parse(find('#filters_box')['data-options']).map(&:symbolize_keys)).to match_array [
{
index: 1,
label: 'Name',
name: 'name',
type: 'string',
value: '',
operator: 'is',
operators: %w[_discard like not_like is starts_with ends_with],
},
{
index: 2,
label: 'Team',
name: 'team',
type: 'belongs_to_association',
value: '',
operator: nil,
operators: %w[_discard like not_like is starts_with ends_with _separator _present _blank],
},
]
end
it 'shows the help text below the search box' do
RailsAdmin.config Player do
list do
search_help 'Use this box to search!'
end
end
visit index_path(model_name: 'player')
is_expected.to have_css('.form-text', text: /Use this box/)
end
end
describe 'fields' do
before do
if defined?(ActiveRecord) && ActiveRecord.gem_version >= Gem::Version.new('7.1') || defined?(CompositePrimaryKeys)
RailsAdmin.config Fan do
configure(:fanships) { hide }
configure(:fanship) { hide }
end
end
end
it 'shows all by default' do
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to match_array ['Id', 'Created at', 'Updated at', 'Their Name', 'Teams']
end
it 'hides some fields on demand with a block' do
RailsAdmin.config Fan do
list do
exclude_fields_if do
type == :datetime
end
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to match_array ['Id', 'Their Name', 'Teams']
end
it 'hides some fields on demand with fields list' do
RailsAdmin.config Fan do
list do
exclude_fields :created_at, :updated_at
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to match_array ['Id', 'Their Name', 'Teams']
end
it 'adds some fields on demand with a block' do
RailsAdmin.config Fan do
list do
include_fields_if do
type != :datetime
end
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to match_array ['Id', 'Their Name', 'Teams']
end
it 'shows some fields on demand with fields list, respect ordering and configure them' do
RailsAdmin.config Fan do
list do
fields :name, PK_COLUMN do
label do
"Modified #{label}"
end
end
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to match_array ['Modified Id', 'Modified Their Name']
end
it 'shows all fields if asked' do
RailsAdmin.config Fan do
list do
include_all_fields
field PK_COLUMN
field :name
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to match_array ['Id', 'Created at', 'Updated at', 'Their Name', 'Teams']
end
it 'appears in order defined' do
RailsAdmin.config Fan do
list do
field :updated_at
field :name
field PK_COLUMN
field :created_at
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to eq(['Updated at', 'Their Name', 'Id', 'Created at'])
end
it 'only lists the defined fields if some fields are defined' do
RailsAdmin.config Fan do
list do
field PK_COLUMN
field :name
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to eq(['Id', 'Their Name'])
is_expected.to have_no_selector('th:nth-child(4).header')
end
it 'delegates the label option to the ActiveModel API' do
RailsAdmin.config Fan do
list do
field :name
end
end
visit index_path(model_name: 'fan')
expect(find('th:nth-child(2)')).to have_content('Their Name')
end
it 'is renameable' do
RailsAdmin.config Fan do
list do
field PK_COLUMN do
label 'Identifier'
end
field :name
end
end
visit index_path(model_name: 'fan')
expect(find('th:nth-child(2)')).to have_content('Identifier')
expect(find('th:nth-child(3)')).to have_content('Their Name')
end
it 'is renameable by type' do
RailsAdmin.config Fan do
list do
fields_of_type :datetime do
label { "#{label} (datetime)" }
end
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to match_array ['Id', 'Created at (datetime)', 'Updated at (datetime)', 'Their Name', 'Teams']
end
it 'is globally renameable by type' do
RailsAdmin.config Fan do
list do
fields_of_type :datetime do
label { "#{label} (datetime)" }
end
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to match_array ['Id', 'Created at (datetime)', 'Updated at (datetime)', 'Their Name', 'Teams']
end
it 'is sortable by default' do
visit index_path(model_name: 'fan')
is_expected.to have_selector('th:nth-child(2).header')
is_expected.to have_selector('th:nth-child(3).header')
is_expected.to have_selector('th:nth-child(4).header')
is_expected.to have_selector('th:nth-child(5).header')
end
it 'has option to disable sortability' do
RailsAdmin.config Fan do
list do
field PK_COLUMN do
sortable false
end
field :name
end
end
visit index_path(model_name: 'fan')
is_expected.to have_no_selector('th:nth-child(2).header')
is_expected.to have_selector('th:nth-child(3).header')
end
it 'has option to disable sortability by type' do
RailsAdmin.config Fan do
list do
fields_of_type :datetime do
sortable false
end
field PK_COLUMN
field :name
field :created_at
field :updated_at
end
end
visit index_path(model_name: 'fan')
is_expected.to have_selector('th:nth-child(2).header')
is_expected.to have_selector('th:nth-child(3).header')
is_expected.to have_no_selector('th:nth-child(4).header')
is_expected.to have_no_selector('th:nth-child(5).header')
end
it 'has option to disable sortability by type globally' do
RailsAdmin.config Fan do
list do
fields_of_type :datetime do
sortable false
end
field PK_COLUMN
field :name
field :created_at
field :updated_at
end
end
visit index_path(model_name: 'fan')
is_expected.to have_selector('th:nth-child(2).header')
is_expected.to have_selector('th:nth-child(3).header')
is_expected.to have_no_selector('th:nth-child(4).header')
is_expected.to have_no_selector('th:nth-child(5).header')
end
it 'has option to hide fields by type' do
RailsAdmin.config Fan do
list do
fields_of_type :datetime do
hide
end
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to match_array ['Id', 'Their Name', 'Teams']
end
it 'has option to hide fields by type globally' do
RailsAdmin.config Fan do
list do
fields_of_type :datetime do
hide
end
end
end
visit index_path(model_name: 'fan')
expect(all('th').collect(&:text).delete_if { |t| /^\n*$/ =~ t }).
to match_array ['Id', 'Their Name', 'Teams']
end
it 'has option to customize column width' do
RailsAdmin.config Fan do
list do
field PK_COLUMN do
column_width 200
end
field :name
field :created_at
field :updated_at
end
end
@fans = FactoryBot.create_list(:fan, 2)
visit index_path(model_name: 'fan')
# NOTE: Capybara really doesn't want us to look at invisible text. This test
# could break at any moment.
expect(find('style').native.text).to include("#list th.#{PK_COLUMN}_field")
expect(find('style').native.text).to include("#list td.#{PK_COLUMN}_field")
end
it 'has option to customize output formatting' do
RailsAdmin.config Fan do
list do
field PK_COLUMN
field :name do
formatted_value do
value.to_s.upcase
end
end
field :created_at
field :updated_at
end
end
@fans = FactoryBot.create_list(:fan, 2).sort_by(&:id)
visit index_path(model_name: 'fan')
expect(find('tbody tr:nth-child(1) td:nth-child(3)')).to have_content(@fans[1].name.upcase)
expect(find('tbody tr:nth-child(2) td:nth-child(3)')).to have_content(@fans[0].name.upcase)
end
it 'has a simple option to customize output formatting of date fields' do
RailsAdmin.config Fan do
list do
field PK_COLUMN
field :name
field :created_at do
date_format :short
end
field :updated_at
end
end
@fans = FactoryBot.create_list(:fan, 2)
visit index_path(model_name: 'fan')
is_expected.to have_selector('tbody tr:nth-child(1) td:nth-child(4)', text: /\d{2} \w{3} \d{1,2}:\d{1,2}/)
end
it 'has option to customize output formatting of date fields' do
RailsAdmin.config Fan do
list do
field PK_COLUMN
field :name
field :created_at do
strftime_format '%Y-%m-%d'
end
field :updated_at
end
end
@fans = FactoryBot.create_list(:fan, 2)
visit index_path(model_name: 'fan')
is_expected.to have_selector('tbody tr:nth-child(1) td:nth-child(4)', text: /\d{4}-\d{2}-\d{2}/)
end
it 'allows addition of virtual fields (object methods)' do
RailsAdmin.config Team do
list do
field PK_COLUMN
field :name
field :player_names_truncated
end
end
@team = FactoryBot.create :team
@players = FactoryBot.create_list :player, 2, team: @team
visit index_path(model_name: 'team')
expect(find('tbody tr:nth-child(1) td:nth-child(4)')).to have_content(@players.sort_by(&:id).collect(&:name).join(', '))
end
describe 'with title attribute' do
it 'does not allow XSS' do
RailsAdmin.config Team do
list do
field :name
end
end
@team = FactoryBot.create :team, name: '" onclick="alert()" "'
visit index_path(model_name: 'team')
expect(find('tbody tr:nth-child(1) td:nth-child(2)')['onclick']).to be_nil
expect(find('tbody tr:nth-child(1) td:nth-child(2)')['title']).to eq '" onclick="alert()" "'
end
it 'does not break values with HTML tags' do
RailsAdmin.config Player do
list do
field :team
end
end
@player = FactoryBot.create :player, team: FactoryBot.create(:team)
visit index_path(model_name: 'player')
expect(find('tbody tr:nth-child(1) td:nth-child(2)')['title']).to eq @player.team.name
end
end
end
context 'when no record exists' do
before do
visit index_path(model_name: 'player')
end
it 'shows "No records found" message' do
is_expected.to have_content('No records found')
end
end
context 'without pagination' do
before do
@players = FactoryBot.create_list(:player, 2)
visit index_path(model_name: 'player')
end
it 'shows "2 results"' do
is_expected.to have_content('2 players')
end
end
context 'with pagination' do
def visit_page(page)
visit index_path(model_name: 'player', page: page)
end
before do
FactoryBot.create_list :player, 3
end
describe 'with limited_pagination=false' do
before { RailsAdmin.config.default_items_per_page = 1 }
it 'page 1' do
visit_page(1)
within('ul.pagination') do
expect(find('li:first')).to have_content('« Prev')
expect(find('li:last')).to have_content('Next »')
expect(find('li.active')).to have_content('1')
end
end
it 'page 2' do
visit_page(2)
within('ul.pagination') do
expect(find('li:first')).to have_content('« Prev')
expect(find('li:last')).to have_content('Next »')
expect(find('li.active')).to have_content('2')
end
end
it 'page 3' do
visit_page(3)
within('ul.pagination') do
expect(find('li:first')).to have_content('« Prev')
expect(find('li:last')).to have_content('Next »')
expect(find('li.active')).to have_content('3')
end
end
end
context 'with limited_pagination=true' do
before do
RailsAdmin.config.default_items_per_page = 1
allow(RailsAdmin::AbstractModel.new(Player).config.list).
to receive(:limited_pagination).
and_return(true)
end
it 'page 1' do
visit_page(1)
within('ul.pagination') do
expect(find('li:first')).not_to have_content('« Prev')
expect(find('li:last')).to have_content('Next »')
end
end
it 'page 2' do
visit_page(2)
within('ul.pagination') do
expect(find('li:first')).to have_content('« Prev')
expect(find('li:last')).to have_content('Next »')
end
end
it 'page 3' do
visit_page(3)
within('ul.pagination') do
expect(find('li:first')).to have_content('« Prev')
expect(find('li:last')).to have_content('Next »')
end
end
end
describe 'number of items per page' do
before do
FactoryBot.create_list :league, 2
end
it 'is configurable per model' do
RailsAdmin.config League do
list do
items_per_page 1
end
end
visit index_path(model_name: 'league')
is_expected.to have_selector('tbody tr', count: 1)
visit index_path(model_name: 'player')
is_expected.to have_selector('tbody tr', count: 3)
end
end
end
context 'on showing all' do
it 'responds successfully with a single model' do
FactoryBot.create :player
visit index_path(model_name: 'player', all: true)
expect(find('div.total-count')).to have_content('1 player')
expect(find('div.total-count')).not_to have_content('1 players')
end
it 'responds successfully with multiple models' do
FactoryBot.create_list(:player, 2)
visit index_path(model_name: 'player', all: true)
expect(find('div.total-count')).to have_content('2 players')
end
end
context 'with pagination disabled by :associated_collection' do
it 'responds successfully' do
@team = FactoryBot.create :team
Array.new(2) { FactoryBot.create :player, team: @team }
visit index_path(model_name: 'player', associated_collection: 'players', compact: true, current_action: 'update', source_abstract_model: 'team', source_object_id: @team.id)
expect(find('div.total-count')).to have_content('2 players')
end
end
describe 'sorting' do
let(:today) { Date.today }
let(:players) do
[{name: 'Jackie Robinson', created_at: today, team_id: rand(99_999), number: 42},
{name: 'Deibinson Romero', created_at: (today - 2.days), team_id: rand(99_999), number: 13},
{name: 'Sandy Koufax', created_at: (today - 1.days), team_id: rand(99_999), number: 32}]
end
let(:leagues) do
[{name: 'American', created_at: (today - 1.day)},
{name: 'Florida State', created_at: (today - 2.days)},
{name: 'National', created_at: today}]
end
let(:player_names_by_date) { players.sort_by { |p| p[:created_at] }.collect { |p| p[:name] } }
let(:league_names_by_date) { leagues.sort_by { |l| l[:created_at] }.collect { |l| l[:name] } }
before { @players = players.collect { |h| Player.create(h) } }
it 'has reverse direction by default' do
RailsAdmin.config Player do
list do
sort_by :created_at
field :name
end
end
visit index_path(model_name: 'player')
player_names_by_date.reverse.each_with_index do |name, i|
expect(find("tbody tr:nth-child(#{i + 1})")).to have_content(name)
end
end
it 'allows change direction by using field configuration' do
RailsAdmin.config Player do
list do
sort_by :created_at
configure :created_at do
sort_reverse false
end
field :name
end
end
visit index_path(model_name: 'player')
player_names_by_date.each_with_index do |name, i|
expect(find("tbody tr:nth-child(#{i + 1})")).to have_content(name)
end
end
it 'can be activated by clicking the table header', js: true do
visit index_path(model_name: 'player')
find('th.header', text: 'Name').trigger('click')
is_expected.to have_css('th.name_field.headerSortDown')
expect(all('tbody td.name_field').map(&:text)).to eq @players.map(&:name).sort
end
end
context 'on listing as compact json' do
it 'has_content an array with 2 elements and contain an array of elements with keys id and label' do
FactoryBot.create_list(:player, 2)
get index_path(model_name: 'player', compact: true, format: :json)
expect(ActiveSupport::JSON.decode(response.body).length).to eq(2)
ActiveSupport::JSON.decode(response.body).each do |object|
expect(object).to have_key('id')
expect(object).to have_key('label')
end
end
end
describe 'with search operator' do
let(:player) { FactoryBot.create :player }
before do
expect(Player.count).to eq(0)
end
it 'finds the player if the query matches the default search operator' do
RailsAdmin.config do |config|
config.default_search_operator = 'ends_with'
config.model Player do
list { field :name }
end
end
visit index_path(model_name: 'player', query: player.name[2, -1])
is_expected.to have_content(player.name)
end
it 'does not find the player if the query does not match the default search operator' do
RailsAdmin.config do |config|
config.default_search_operator = 'ends_with'
config.model Player do
list { field :name }
end
end
visit index_path(model_name: 'player', query: player.name[0, 2])
is_expected.to have_no_content(player.name)
end
it 'finds the player if the query matches the specified search operator' do
RailsAdmin.config Player do
list do
field :name do
search_operator 'starts_with'
end
end
end
visit index_path(model_name: 'player', query: player.name[0, 2])
is_expected.to have_content(player.name)
end
it 'does not find the player if the query does not match the specified search operator' do
RailsAdmin.config Player do
list do
field :name do
search_operator 'starts_with'
end
end
end
visit index_path(model_name: 'player', query: player.name[1..])
is_expected.to have_no_content(player.name)
end
end
describe 'with custom search' do
before do
RailsAdmin.config do |config|
config.model Player do
list do
search_by :rails_admin_search
end
end
end
end
let!(:players) do
[FactoryBot.create(:player, name: 'Joe'),
FactoryBot.create(:player, name: 'George')]
end
it 'performs search using given scope' do
visit index_path(model_name: 'player', query: 'eoJ')
is_expected.to have_content(players[0].name)
is_expected.to have_no_content(players[1].name)
end
end
context 'with overridden to_param' do
before do
@ball = FactoryBot.create :ball
visit index_path(model_name: 'ball')
end
it 'shows the show, edit and delete links with valid url' do
is_expected.to have_selector("td a[href$='/admin/ball/#{@ball.id}']")
is_expected.to have_selector("td a[href$='/admin/ball/#{@ball.id}/edit']")
is_expected.to have_selector("td a[href$='/admin/ball/#{@ball.id}/delete']")
end