test-formhandler.html 49.2 KB
Newer Older
S Anand's avatar
S Anand committed
1
2
<!DOCTYPE html>
<html>
3

S Anand's avatar
S Anand committed
4
5
6
<head>
  <title>formhandler tests</title>
  <link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
7
8
  <link rel="stylesheet" href="../node_modules/font-awesome/css/font-awesome.min.css">
  <style>
9
10
11
12
13
14
15
16
17
18
19
20
21
    .position-relative {
      position: relative;
    }

    .pos-cc {
      position: absolute;
      top: 45%;
      left: 45%;
    }

    .d-none {
      display: none !important;
    }
22
  </style>
S Anand's avatar
S Anand committed
23
24
25
26
  <script src="../node_modules/jquery/dist/jquery.min.js"></script>
  <script src="../node_modules/popper.js/dist/umd/popper.min.js"></script>
  <script src="../node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
  <script src="../node_modules/lodash/lodash.min.js"></script>
27
28
  <script src="../node_modules/moment/min/moment.min.js"></script>
  <script src="../node_modules/numeral/min/numeral.min.js"></script>
S Anand's avatar
S Anand committed
29
30
31
  <script src="../node_modules/d3/build/d3.js"></script>
  <script src="../node_modules/d3-scale-chromatic/dist/d3-scale-chromatic.js"></script>
  <script src="../dist/g1.min.js"></script>
S Anand's avatar
S Anand committed
32
33
34
  <script src="tape.js"></script>

</head>
35

S Anand's avatar
S Anand committed
36
37
38
39
<body>
  <script>
    tape.onFinish(function () { window.renderComplete = true })
  </script>
40
41
42
43
44
45
46
47
48
49
50
51
  <div class="hide_col3" data-src="/test/formhandler.json"></div>
  <div class="hide_col2" data-src="/test/formhandler.json"></div>
  <div class="hide_col" data-src="/test/formhandler.json"></div>

  <div class="cell_template1" data-src="/test/formhandler.json"></div>
  <div class="cell_template2" data-src="/test/formhandler.json"></div>

  <div class="test_star_overrride" data-src="/test/formhandler.json"></div>
  <div class="test-star-without-title" data-src="/test/formhandler.json"></div>
  <div class="test-star-with-title" data-src="/test/formhandler.json"></div>


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  <div class="test_star_overrride" data-src="/formhandler-data"></div>
  <div class="test-star-without-title" data-src="/formhandler-data"></div>
  <div class="test-star-with-title" data-src="/formhandler-data"></div>
  <div class="add_row1" data-src="/formhandler-data"></div>

  <div class="row_template7" data-table="grid" data-src="/formhandler-data"></div>
  <div class="row_template6" data-src="/formhandler-data"></div>
  <div class="row_template5" data-src="/formhandler-data"></div>
  <div class="row_template4" data-src="/formhandler-data"></div>
  <div class="row_template3" data-src="/formhandler-data"></div>
  <div class="row_template2" data-src="/formhandler-data"></div>
  <div class="row_template1" data-src="/formhandler-data"></div>

  <div class="custom_buttons1" data-src="/formhandler-data"></div>
  <div class="custom_buttons2" data-src="/formhandler-data"></div>
  <div class="custom_buttons3" data-src="/formhandler-data"></div>
  <div class="custom_buttons4" data-src="/formhandler-data"></div>
  <div class="custom_buttons5" data-src="/formhandler-data"></div>

  <div class="fh17" data-src="/formhandler-data"></div>
  <div class="fh15" data-src="/formhandler-data"></div>
  <div class="fh16" data-src="/formhandler-data"></div>
  <div class="fh14" data-src="/formhandler-data"></div>
75
76
77
78
  <div class="hide_col3" data-src="/formhandler-data"></div>
  <div class="hide_col2" data-src="/formhandler-data"></div>
  <div class="hide_col" data-src="/formhandler-data"></div>

S Anand's avatar
S Anand committed
79
80
81
82
83
84
85
  <div class="cell_template1" data-src="/formhandler-data"></div>
  <div class="cell_template2" data-src="/formhandler-data"></div>
  <div class="cell_template3" data-src="/formhandler-data"></div>
  <div class="cell_template4" data-src="/formhandler-data"></div>
  <div class="cell_template5" data-src="/formhandler-data"></div>


S Anand's avatar
S Anand committed
86
  <div class="fh13" data-src="/formhandler-data"></div>
S Anand's avatar
S Anand committed
87

88
89
90
91
92
93
94
95
96
97
  <div class="fh1" data-src="formhandler.json"></div>
  <div class="fh2"></div>
  <div class="fh3"></div>
  <div class="fh4" data-table="false" data-count="false" data-page="false" data-size="false" data-export="false" data-filters="false"></div>
  <div class="fh5" data-src="formhandler.json"></div>
  <div class="fh6" data-src="formhandler.json"></div>
  <div class="fh7" data-src="formhandler.json"></div>
  <div class="fh8" data-src="formhandler.json"></div>
  <div class="fh9" data-src="formhandler.json"></div>
  <div class="fh10"></div>
98
99
  <div class="fh11" data-src="formhandler.json"></div>
  <div class="fh12" data-src="formhandler.json"></div>
100
  <div class="formhandler" data-src="formhandler.json" data-page-size="10"></div>
S Anand's avatar
S Anand committed
101
102
103
104
105
  <div class="fhgrid1" data-table="grid" data-src="formhandler.json"></div>
  <div class="fhgrid2" data-table="grid" data-src="formhandler.json" data-icon="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTZ1bqTcxkaGSSIl5meU4JYO1dvUVbqP71z0hH8M6f6Yh9JpZg8"></div>
  <div class="fhgrid3" data-table="grid" data-src="formhandler.json" data-classes="formhandler-grid"></div>
  <div class="fhgrid4" data-table="grid" data-src="formhandler.json"></div>
  <div class="fhgrid5" data-table="grid" data-src="formhandler.json"></div>
S Anand's avatar
S Anand committed
106
  <script>
107
    tape('$().formhandler() basic example works', function (t) {
S Anand's avatar
S Anand committed
108
      $('.formhandler').formhandler({
109
        pageSize: 20
110
      }).on('load', function (e) {
111
112
        t.equals(e.options.pageSize, 10)
        // TODO: Review: if _limit is given in url, e.args._limit is [10] not 10
S Anand's avatar
S Anand committed
113
        // t.equals(e.args._limit, 10)
114
115
116
117
118
        t.ok(e.formdata.length > 0)
        t.equals($('.formhandler table tbody tr').length, e.formdata.length)
        t.end()
      })
    })
119
    tape('$().formhandler() renders data from data-src, overriding options.src', function (t) {
120
121
      $('.fh1')
        .formhandler({ src: 'formhandler-small.json' })
122
        .on('load', function () {
123
124
125
126
          t.equals($('.fh1 tbody tr').length, 50)
          t.end()
        })
    })
127
    tape('$().formhandler() hide options doesnot display the column', function(t) {
128
129
      var class_count = 2
      t.plan(1 * class_count)
130
131
132
133
134
135
136
137
138
139
140
141
142
143
      $('.hide_col')
        .formhandler({
          columns: [
            {
              name: 'Continent',
              hide: true
            }
          ]
        })
        .on('load', function () {
          t.equals($('body > div.hide_col > div.position-relative > div.formhandler > div.table > table > thead').text().trim(), "")
        })
    })
    tape('$().formhandler() hide options doesnot display the column (using multiple columns)', function(t) {
144
145
146
      var class_count = 2
      var current_class_count = 0
      t.plan(2 * class_count)
147
148
149
150
151
152
153
154
155
156
157
158
159
160
      $('.hide_col2')
        .formhandler({
          columns: [
            {
              name: 'Continent',
              hide: true
            },
            {
              name: 'c1'
            }
          ],
          pageSize: 3
        })
        .on('load', function () {
161
162
          current_class_count += 1
          t.equals($("body > div.hide_col2 > div.position-relative > div.formhandler > div.table > table > thead > tr").length, current_class_count)
163
164
165
166
          t.ok($("body > div.hide_col2 > div.position-relative > div.formhandler > div.table > table > thead > tr").text().startsWith(" c1"))
        })
    })
    tape('$().formhandler() hide options doesnot display the column (using * all columns)', function(t) {
167
168
169
      var class_count = 1
      var current_class_count = 0
      t.plan(2* class_count)
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
      $('.hide_col3')
        .formhandler({
          columns: [
            {
              name: '*'
            },
            {
              name: 'Continent',
              hide: true
            },
            {
              name: 'c1'
            }
          ],
          pageSize: 3
        })
        .on('load', function () {
187
188
189
190
191
          current_class_count += 1
          if(current_class_count == 2) {
            t.equals($('[data-col="Continent"]', $("body > div.hide_col3 > div.position-relative > div.formhandler > div.table > table > thead > tr")).length, 0)
            t.ok($('[data-col]', $("body > div.hide_col3 > div.position-relative > div.formhandler > div.table > table > thead > tr")).length > 0)
          }
192
193
        })
    })
194
    tape('$().formhandler() uses option.src if no data-src exists', function (t) {
195
196
197
198
199
200
201
      $('.fh2')
        .formhandler({ src: 'formhandler-small.json' })
        .on('load', function () {
          t.equal($('.fh2 tbody tr').length, 2)
          t.end()
        })
    })
202
    tape('$().formhandler() shows default controls if meta is present', function (t) {
203
204
205
      $('.fh3')
        .formhandler({
          src: 'formhandler-small.json',
206
          transform: function (obj) {
207
208
209
            obj.meta.count = obj.meta.limit = obj.data.length
          }
        })
210
        .on('load', function () {
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
          t.equal($('.fh3 tbody tr').length, 2)
          t.equal($('.fh3 .count').length, 1)
          t.ok($('.fh3 .count').text().match(/2 rows/))

          t.equal($('.fh3 .page').length, 1)
          t.ok($('.fh3 .page').text().match(/Previous.*Next/))

          t.equal($('.fh3 .size').length, 1)
          t.ok($('.fh3 .size').text().match(/2 rows/))

          t.equal($('.fh3 .filters').length, 1)

          t.equal($('.fh3 .export').length, 1)
          t.ok($('.fh3 .export').text().match(/Export as/))

          t.end()
        })
    })

230
    tape('$().formhandler() disables controls via data-attributes', function (t) {
231
232
233
      $('.fh4')
        .formhandler({
          src: 'formhandler-small.json',
234
          transform: function (obj) {
235
236
237
            obj.meta.count = obj.meta.limit = obj.data.length
          }
        })
238
        .on('load', function () {
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
          t.equal($('.fh4 .table').html(), '')
          t.equal($('.fh4 .count').html(), '')
          t.equal($('.fh4 .page').html(), '')
          t.equal($('.fh4 .size').html(), '')
          t.equal($('.fh4 .filters').html(), '')
          t.equal($('.fh4 .export').html(), '')
          t.end()
        })
    })
    tape('$().formhandler() applies {sort: true, hideable: false} option mentioned in options.columns', function (t) {
      $('.fh5')
        .formhandler({
          columns: [
            { name: 'Continent', sort: true, hideable: false }
          ]
        })
        .on('load', function () {
          // display only Continent column
          t.equal($('.fh5 thead tr th').length, 1)
          t.equal($('.fh5 [data-col="Continent"] div:nth-child(1) div a:nth-child(1)').text().trim(), "Sort ascending")
          t.equal($('.fh5 [data-col="Continent"] div:nth-child(1) div a:nth-child(2)').text().trim(), "Sort descending")
          t.notEqual($('.fh5 [data-col="Continent"] div:nth-child(1) div a:last-of-type').text().trim(), "Hide")

          t.end()
        })
    })
    tape('$().formhandler() allows updating column info only for specific columns', function (t) {
      $('.fh6')
        .formhandler({
          columns: [
            { name: 'Continent', title: 'CustomTitle', sort: true },
            { name: '*' }
          ]
        })
        .on('load', function () {
          // no. of columns in meta data must be same as rendered columns
          t.equal($('.fh6 thead tr th').length, 17)
          // there should not be a column with name 'Continent'
          t.notOk($('.fh6 thead tr').text().match(/Continent/))
          // there shoudl be a column with name 'CustomTitle'
          t.ok($('.fh6 thead tr').text().match(/CustomTitle/))
          // column is hideable by default
          t.equal($('.fh6 [data-col="Continent"] div:nth-child(1) div a:last-of-type').text().trim(), "Hide")
          t.end()
        })
    })
285
    tape('$().formhandler() allows updating column info only for specific columns override test', function (t) {
286
287
288
      var class_count = 2
      var current_class_count = 0
      t.plan(6 * class_count)
289
290
291
292
293
294
295
296
      $('.test_star_overrride')
        .formhandler({
          columns: [
            { name: 'Continent', title: 'CustomTitle', sort: true },
            { name: '*', title: 'SameName' }
          ]
        })
        .on('load', function () {
297
          current_class_count += 1
298
          // no. of columns in meta data must be same as rendered columns
299
          t.equal($('.test_star_overrride thead tr th').length, 17 * current_class_count)
300
301
302
303
          // there should not be a column with name 'Continent'
          t.notOk($('.test_star_overrride thead tr').text().match(/Continent/))
          // there shoudl be a column with name 'CustomTitle'
          t.ok($('.test_star_overrride thead tr').text().match(/CustomTitle/))
304
          t.equal($('.test_star_overrride thead tr').text().match(/CustomTitle/).length, 1)
305
          // Rest of 16 columns must have title as 'SameName'
306
          t.equal($('.test_star_overrride thead tr').text().split('SameName').length - 1, 16 * current_class_count)
307
          // column is hideable by default
308
          t.equal($('.test_star_overrride [data-col="Continent"] div:nth-child(1) div a:last-of-type').text().trim(), "Hide".repeat(current_class_count))
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
    tape('$().formhandler() applies {sort: false} option mentioned in options.columns', function (t) {
      $('.fh7')
        .formhandler({
          columns: [
            { name: 'Continent', sort: false }
          ]
        })
        .on('load', function () {
          // no. of columns in meta data must be same as rendered columns
          t.equal($('.fh7 thead tr th').length, 1)
          t.notEqual($('.fh7 [data-col="Continent"] div:nth-child(1) div a:nth-child(1)').text().trim(), "Sort ascending")
          t.notEqual($('.fh7 [data-col="Continent"] div:nth-child(1) div a:nth-child(2)').text().trim(), "Sort descending")
          t.end()
        })
    })
    tape('$().formhandler() ---- applies custom sorting names option mentioned in options.columns', function (t) {
      $('.fh8')
        .formhandler({
          columns: [
            { name: 'Continent', sort: { '': 'Sort by Alphabet', '-': 'Sort reverse alphabet' } },
          ]
        })
        .on('load', function () {
          // check if custom sort names are displayed
          t.equal($('.fh8 [data-col="Continent"] div:nth-child(1) div a:nth-child(1)').text().trim(), "Sort by Alphabet")
          t.equal($('.fh8 [data-col="Continent"] div:nth-child(1) div a:nth-child(2)').text().trim(), "Sort reverse alphabet")
          t.end()
        })
    })
    tape('$().formhandler() applies format option (function, text, number) option mentioned in options.columns', function (t) {
      $('.fh9')
        .formhandler({
          columns: [
344
            { name: 'Continent', format: function (arg) { return arg.row['Continent'].toUpperCase() } }
345
346
347
348
349
350
351
352
          ]
        })
        .on('load', function () {
          // check if by default text type is applied
          // check if function is applied as format
          t.equal($('.fh9 tbody tr:first-of-type td:first-of-type a').text().trim(), "EUROPE")
          t.end()
        })
353
    })
354
355
    tape('$().formhandler() applies format option (date type) option mentioned in options.columns', function (t) {
      $('.fh10')
356
357
358
359
360
361
362
363
364
365
        .formhandler({
          src: 'formhandler-types.json',
          columns: [
            { name: 'Amount', type: 'number', format: '$0,0.00' },
            { name: 'Date', type: 'date', format: 'DD-MM-YYYY' }
          ]
        })
        .on('load', function () {
          // check if number is given, applied filters display name must be Greater than, Lesser than
          // check if date is give, applied filters display name must be Before, After
366

367
368
369
370
          // check if format is applied to number type using numeraljs
          t.equal($('.fh10 tbody tr:first-of-type td:nth-of-type(1) a').text().trim(), '$3,500.00')
          // check if format is applied to date type using momentjs
          t.equal($('.fh10 tbody tr:first-of-type td:nth-of-type(2) a').text().trim(), '08-02-2013')
371
372


373
374
375
376
377
378
379
380
381
          t.end()
        })
    })
    tape('$().formhandler() link attr will let user goto that link instead of applying filter', function (t) {
      $('.fh11')
        .formhandler({
          columns: [
            {
              name: 'Continent',
382
              link: 'https://en.wikipedia.org/wiki/<%= row["Continent"].split(" ").join(" ") %>'
383
384
385
386
387
            }
          ]
        })
        .on('load', function () {
          // no. of columns in meta data must be same as rendered columns
388
          t.equal($('.fh11 tbody tr:first-of-type td:nth-of-type(1) a').attr('href'), 'https://en.wikipedia.org/wiki/' + $('.fh11 tbody tr:first-of-type td:nth-of-type(1) a').text().split(" ").join(" ").trim())
389
390
391
392
393
394
395
396
397
          t.end()
        })
    })
    tape('$().formhandler() link attr will let user goto that link instead of applying filter using function', function (t) {
      $('.fh12')
        .formhandler({
          columns: [
            {
              name: 'Continent',
398
              link: function (arg) { return 'https://en.wikipedia.org/wiki/' + arg.row['Continent'].split(" ").join(" ") }
399
400
401
402
403
            }
          ]
        })
        .on('load', function () {
          // no. of columns in meta data must be same as rendered columns
404
          t.equal($('.fh12 tbody tr:first-of-type td:nth-of-type(1) a').attr('href'), 'https://en.wikipedia.org/wiki/' + $('.fh11 tbody tr:first-of-type td:nth-of-type(1) a').text().split(" ").join(" ").trim())
S Anand's avatar
S Anand committed
405
406
407
408
409
410
411
412
          t.end()
        })
    })
    tape('$().formhandler() if namespace is given for urlfilter, apply that ', function (t) {
      window.history.pushState({}, '', '#')
      window.history.pushState({}, '', g1.url.parse(location.href).toString() + '#' + g1.url.parse(location.hash).update({ 'fhname13:Continent': 'Europe', _limit: 5 }).toString())

      $('.fh13')
413
414
415
416
417
418
        .formhandler({
          name: 'fhname13',
          columns: [
            {
              name: 'Continent'
            }
S Anand's avatar
S Anand committed
419
420
421
422
423
          ]
        })
        .on('load', function () {
          // no. of columns in meta data must be same as rendered columns
          t.equal($('body > div.fh13 > div.position-relative > div.formhandler > div.table > table > tbody > tr:nth-child(1) > td > a').text().trim(' '),
424
            'Europe')
S Anand's avatar
S Anand committed
425
426
427
428
429
430
431
432
433
          t.equal($('body > div.fh13 > div.position-relative > div.formhandler > div.table > table > tbody > tr:nth-child(2) > td > a').text().trim(' '),
            'Europe')
          t.equal($('body > div.fh13 > div.position-relative > div.formhandler > div.table > table > tbody > tr:nth-child(3) > td > a').text().trim(' '),
            'Europe')
          t.equal($('body > div.fh13 > div.position-relative > div.formhandler > div.table > table > tbody > tr:nth-child(4) > td > a').text().trim(' '),
            'Europe')
          t.equal($('body > div.fh13 > div.position-relative > div.formhandler > div.table > table > tbody > tr:nth-child(5) > td > a').text().trim(' '),
            'Europe')
          window.history.pushState({}, '', '#')
434
435
          t.end()
        })
S Anand's avatar
S Anand committed
436
    })
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
    tape('$().formhandler() test edit option functionality for single link column', function (t) {
      $('.fh14')
        .formhandler({
          columns: [
            {
              name: 'ID',
              link: function (arg) { return 'https://en.wikipedia.org/wiki/' + arg.row.ID.split(" ").join(" ") },
              editable: false
            },
            {
              name: 'c1',
              editable: true
            }
          ],
          pageSize: 5,
          edit: true
        })
        .on('load', function () {
          // check if Edit button is rendered when edit:true option is given
456
          t.deepEquals($('body > div.fh14 > div.position-relative > div.formhandler > div.formhandler-table-header.d-flex.justify-content-between.mb-2 > div.d-flex.flex-wrap > div.edit > button').text(), "Edit")
457
          // check if Edit Button changes to Save Button when clicked
458
459
          $('body > div.fh14 > div.position-relative > div.formhandler > div.formhandler-table-header.d-flex.justify-content-between.mb-2 > div.d-flex.flex-wrap > div.edit > button').click()
          t.deepEquals($('body > div.fh14 > div.position-relative > div.formhandler > div.formhandler-table-header.d-flex.justify-content-between.mb-2 > div.d-flex.flex-wrap > div.edit > button').text(), "Save")
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
          // check if when Save Button is visiblle, all the rows in table are rerendered which can be editable
          t.deepEquals()
          // check if when Save Button is clicked, the Edit Button changes to Edit Button
          // check if when Save Button is clicked, all the rows are filterable cells - deepEquals(initialTemplate)

          // check if when save button is clicked,
          // check if each row has edit and save buttons

          // test if when save is clicked, only the current formhandler table rows got updated and not other formhandler rows in the page

          // test if a bootstrap success message is visible if rows got updated successfully
          t.end()
        })
    })
    tape('$().formhandler() test edit option functionality for single column', function (t) {
      $('.fh15')
        .formhandler({
          columns: [
            {
              name: 'Continent',
              editable: true
            },
            {
              name: 'ID',
              editable: false
            }
          ],
          pageSize: 5,
          edit: true
        })
        .on('load', function () {
          // check if each row has edit and save buttons
          t.end()
        })
    })
    // tape('$().formhandler() test edit option exists for (all) columns when * is mentioned, edit is true', function (t) {
    //   $('.fh16')
    //     .formhandler({
    //       columns: [
    //         {
    //           name: '*',
    //           editable: true
    //         }
    //       ],
    //       pageSize: 5,
    //       edit: true
    //     })
    //     .on('load', function () {
    //       // check if each row has edit and save buttons
    //       t.end()
    //     })
    // })
    tape('$().formhandler() test edit option exists for (all) columns when name * and override of other cols work', function (t) {
      $('.fh17')
        .formhandler({
          columns: [
            {
              name: '*'
            },
            {
              name: 'ID',
              link: function (arg) { return 'https://en.wikipedia.org/wiki/' + arg.row.ID.split(" ").join(" ") },
            },
            {
              name: 'Continent',
            }
          ],
          pageSize: 5,
          edit: true
        })
        .on('load', function () {
          // check if each row has edit and save buttons
          // check if Edit button is rendered when edit:true option is given
533
          t.deepEquals($('body > div.fh17 > div.position-relative > div.formhandler > div.formhandler-table-header.d-flex.justify-content-between.mb-2 > div.d-flex.flex-wrap > div.edit > button').text(), "Edit")
534
          // check if Edit Button changes to Save Button when clicked
535
536
          $('body > div.fh17 > div.position-relative > div.formhandler > div.formhandler-table-header.d-flex.justify-content-between.mb-2 > div.d-flex.flex-wrap > div.edit > button').click()
          t.deepEquals($('body > div.fh17 > div.position-relative > div.formhandler > div.formhandler-table-header.d-flex.justify-content-between.mb-2 > div.d-flex.flex-wrap > div.edit > button').text(), "Save")
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
          var td_elms = $("body > div.fh17 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr > td ")
          var input_elms = $("body > div.fh17 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr > td > input")
          t.equals(td_elms.length, input_elms.length)
          t.end()
        })
    })
    // tape('$().formhandler() add new rows', function (t) {
    //   $('.fh_add1')
    //     .formhandler({
    //       columns: [
    //         {
    //           name: 'Continent',
    //           link: 'https://en.wikipedia.org/wiki/<%= row.Continent.split(" ").join(" ") %>'
    //         }
    //       ],
    //       add: true
    //     })
    //     .on('load', function () {
    //       // no. of columns in meta data must be same as rendered columns
    //       // t.equal($('.fh11 tbody tr:first-of-type td:nth-of-type(1) a').attr('href'), 'https://en.wikipedia.org/wiki/' + $('.fh11 tbody tr:first-of-type td:nth-of-type(1) a').text().split(" ").join(" "))
    //       t.end()
    //     })
    // })
S Anand's avatar
S Anand committed
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
    // TODO: formatted_content if not encoded url args work
    tape('$().formhandler() renders grid from data-src', function (t) {
      $('.fhgrid1')
        .formhandler({ src: 'formhandler-small.json' })
        .on('load', function () {
          t.equals($('.fhgrid1 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow .thumbnail').length, 50)
          t.end()
        })
    })

    tape('$().formhandler() renders grid from data-src and overriding default options.icon', function (t) {
      $('.fhgrid2')
        .formhandler({ src: 'formhandler-small.json' })
        .on('load', function () {
          t.equals($('.fhgrid2 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow .thumbnail').length, 50)
          t.equals($('.fhgrid2 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow:first-child .thumbnail img').attr('src'), 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTZ1bqTcxkaGSSIl5meU4JYO1dvUVbqP71z0hH8M6f6Yh9JpZg8')
          t.end()
        })
    })

    tape('$().formhandler() renders grid from data-src and overriding default options.classes', function (t) {
      $('.fhgrid3')
        .formhandler({ src: 'formhandler-small.json' })
        .on('load', function () {
          t.equals($('.fhgrid3 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow .thumbnail').length, 0)
          t.equals($('.fhgrid3 .formhandler-grid .formhandler-grid .thumbnail').length, 50)
          t.end()
        })
    })

    tape('$().formhandler() renders grid from data-src and overriding default options.icon with the jsoptions.icon', function (t) {
      $('.fhgrid4')
592
        .formhandler({ src: 'formhandler-small.json', icon: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSck4Qkjt376rXmEUqCj1MVPsgF4ftoSg4ok2EXdansLiF9VddD' })
S Anand's avatar
S Anand committed
593
594
595
596
597
598
599
600
601
        .on('load', function () {
          t.equals($('.fhgrid4 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow .thumbnail').length, 50)
          t.equals($('.fhgrid4 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow:first-child .thumbnail img').attr('src'), 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSck4Qkjt376rXmEUqCj1MVPsgF4ftoSg4ok2EXdansLiF9VddD')
          t.end()
        })
    })

    tape('$().formhandler() renders grid from data-src and overriding default options.classes with jsoptions.classes', function (t) {
      $('.fhgrid5')
602
        .formhandler({ src: 'formhandler-small.json', classes: 'formhandler-grid' })
S Anand's avatar
S Anand committed
603
604
605
606
607
608
609
        .on('load', function () {
          t.equals($('.fhgrid5 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow .thumbnail').length, 0)
          t.equals($('.fhgrid5 .formhandler-grid .formhandler-grid .thumbnail').length, 50)
          t.end()
        })
    })

610
    tape('$().formhandler() renders template with formatted cell contents', function (t) {
611
612
613
      var class_count = 2
      var current_class_count = 0
      t.plan(3 * class_count)
S Anand's avatar
S Anand committed
614
615
616
617
      $('.cell_template1').formhandler({
        columns: [
          {
            name: 'ID',
618
619
620
621
            format: function (arg) { return arg.row['ID'].toLowerCase() }, // TODO: REVIEW whether to pass row number as second argument?
            link: 'https://en.wikipedia.org/wiki/<%= row.ID %>', // Tejesh: Link is not working if template is applied
            template: function (arg) {
              return "<td>ID " + arg.format + "</td>"
S Anand's avatar
S Anand committed
622
623
624
625
626
627
628
            }
          },
          { name: 'Name' },
          { name: 'Contitent' }
        ],
        pageSize: 3
      }).on('load', function (e) {
629
630
631
632
        current_class_count += 1
        t.equal($('.cell_template1 tr:first-of-type td:nth-of-type(1)').text().trim(), 'ID and'.repeat(current_class_count))
        t.equal($('.cell_template1 tr:nth-of-type(2) td:nth-of-type(1)').text().trim(), 'ID are'.repeat(current_class_count))
        t.equal($('.cell_template1 tr:nth-of-type(3) td:nth-of-type(1)').text().trim(), 'ID afg'.repeat(current_class_count))
S Anand's avatar
S Anand committed
633
634
635
636
      })
    })

    tape('$().formhandler() renders template option with unusual html tags', function (t) {
637
638
639
      var class_count = 2
      var current_class_count = 0
      t.plan(3 * class_count)
S Anand's avatar
S Anand committed
640
641
642
643
644
      // span, div, p, i,
      $('.cell_template2').formhandler({
        columns: [
          {
            name: 'ID',
645
646
647
648
            format: function (arg) { return arg.row['ID'].toLowerCase() }, // TODO: REVIEW whether to pass row number as second argument?
            link: 'https://en.wikipedia.org/wiki/<%= row.ID %>',
            template: function (arg) {
              return "<span>ID " + arg.format + "</span>"
S Anand's avatar
S Anand committed
649
650
651
652
653
654
655
            }
          },
          { name: 'Name' },
          { name: 'Contitent' }
        ],
        pageSize: 3
      }).on('load', function () {
656
657
658
659
        current_class_count += 1
        t.equal($('.cell_template2 span:nth-of-type(1):not(".sr-only, .btn")').text().trim(), 'ID and'.repeat(current_class_count))
        t.equal($('.cell_template2 span:nth-of-type(2):not(".sr-only, .btn")').text().trim(), 'ID are'.repeat(current_class_count))
        t.equal($('.cell_template2 span:nth-of-type(3):not(".sr-only, .btn")').text().trim(), 'ID afg'.repeat(current_class_count))
S Anand's avatar
S Anand committed
660
661
662
663
664
665
666
667
      })
    })

    tape('$().formhandler() renders template with svg elements', function (t) {
      $('.cell_template3').formhandler({
        columns: [
          {
            name: 'ID',
668
669
670
            format: function (arg) { return arg.row['ID'].toLowerCase() }, // TODO: REVIEW whether to pass row number as second argument?
            link: 'https://en.wikipedia.org/wiki/<%= row.ID %>',
            template: function (arg) { return '<td><button type="text" data-action="testaction">'+arg.format+'</button></td>' }
S Anand's avatar
S Anand committed
671
672
673
          },
          {
            name: 'c1',
674
            template: function (arg) {
S Anand's avatar
S Anand committed
675
676
677
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
678
              return '<td>' + arg.value + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="' + f(arg.value) + '"></circle></svg></td>'
S Anand's avatar
S Anand committed
679
680
681
682
            }
          },
          {
            name: 'c2',
683
            format: function (arg) {
S Anand's avatar
S Anand committed
684
685
686
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
687
              return arg.row['c2'] + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="' + f(arg.row['c2']) + '"></circle></svg>'
S Anand's avatar
S Anand committed
688
689
            }
          },
690
691
          { name: 'Continent' },
          { name: 'testaction', template: function(arg) { return '<td><button type="text" data-action="testaction">Test action</button></td>'}}
S Anand's avatar
S Anand committed
692
        ],
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
        pageSize: 3,
        actions: {
          'testaction': function(arg) { // arg {row: row, data: data, value: value}
            $.ajax(options.src, {
              method: 'DELETE',
              dataType: 'json',
              data: data
            }).done(function (data, status, xhr) {
              // TODO: show a bootstrap 4 success/failure message
              $('.loader', $this).addClass('d-none')
            }).fail(function (xhr, status, message) {
              $this.html(template['error']({
                message: message
              }))
            })
          }
        }
S Anand's avatar
S Anand committed
710
      }).on('load', function () {
711
        t.equal($('.cell_template3 tr:first-of-type td:nth-of-type(1)').text().trim(), 'and')
S Anand's avatar
S Anand committed
712
713
714
        t.equal($('.cell_template3 tr:first-of-type td:nth-of-type(2)').text().trim(), '35')
        t.equal($('.cell_template3 tr:first-of-type td:nth-of-type(2) svg circle').length, 1)
        t.equal($('.cell_template3 tr:first-of-type td:nth-of-type(3) a').text().trim(), '1')
715
        t.equal($('.cell_template3 tr:first-of-type td:nth-of-type(3) a').attr('href').trim(), '?c2=' + $('.cell_template3 tr:first-of-type td:nth-of-type(3) a').text().trim() + '&_offset=')
S Anand's avatar
S Anand committed
716
717
        t.equal($('.cell_template3 tr:first-of-type td:nth-of-type(3) svg circle').length, 1)

718
        t.equal($('.cell_template3 tr:nth-of-type(2) td:nth-of-type(1)').text().trim(), 'are')
S Anand's avatar
S Anand committed
719
720
721
        t.equal($('.cell_template3 tr:nth-of-type(2) td:nth-of-type(2)').text().trim(), '24')
        t.equal($('.cell_template3 tr:nth-of-type(2) td:nth-of-type(2) svg circle').length, 1)
        t.equal($('.cell_template3 tr:nth-of-type(2) td:nth-of-type(3) a').text().trim(), '0')
722
        t.equal($('.cell_template3 tr:nth-of-type(2) td:nth-of-type(3) a').attr('href').trim(), '?c2=' + $('.cell_template3 tr:nth-of-type(2) td:nth-of-type(3) a').text().trim() + '&_offset=')
S Anand's avatar
S Anand committed
723
724
        t.equal($('.cell_template3 tr:nth-of-type(2) td:nth-of-type(3) svg circle').length, 1)

725
        t.equal($('.cell_template3 tr:nth-of-type(3) td:nth-of-type(1)').text().trim(), 'afg')
S Anand's avatar
S Anand committed
726
727
728
        t.equal($('.cell_template3 tr:nth-of-type(3) td:nth-of-type(2)').text().trim(), '28')
        t.equal($('.cell_template3 tr:nth-of-type(3) td:nth-of-type(2) svg circle').length, 1)
        t.equal($('.cell_template3 tr:nth-of-type(3) td:nth-of-type(3) a').text().trim(), '1')
729
        t.equal($('.cell_template3 tr:nth-of-type(3) td:nth-of-type(3) a').attr('href').trim(), '?c2=' + $('.cell_template3 tr:nth-of-type(3) td:nth-of-type(3) a').text().trim() + '&_offset=')
S Anand's avatar
S Anand committed
730
731
732
733
734
735
736
737
738
739
        t.equal($('.cell_template3 tr:nth-of-type(3) td:nth-of-type(3) svg circle').length, 1)
        t.end()
      })
    })

    tape('$().formhandler() renders template when it is a string', function (t) {
      $('.cell_template4').formhandler({
        columns: [
          {
            name: 'ID',
740
741
            format: function (arg) { return arg.row['ID'].toLowerCase() }, // TODO: REVIEW whether to pass row number as second argument?
            link: 'https://en.wikipedia.org/wiki/<%= row.ID %>', // Tejesh: Link is not working if template is applied
S Anand's avatar
S Anand committed
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
            template: "<td>ID</td>"
          },
          { name: 'Name' },
          { name: 'Contitent' }
        ],
        pageSize: 3
      }).on('load', function (e) {
        t.equal($('.cell_template4 tr:first-of-type td:nth-of-type(1)').text().trim(), 'ID')
        t.equal($('.cell_template4 tr:nth-of-type(2) td:nth-of-type(1)').text().trim(), 'ID')
        t.equal($('.cell_template4 tr:nth-of-type(3) td:nth-of-type(1)').text().trim(), 'ID')
        t.end()
      })
    })

    tape('$().formhandler() renders template when it is a function', function (t) {
      $('.cell_template5').formhandler({
        columns: [
          {
            name: 'ID',
761
762
763
764
            format: function (arg) { return arg.row['ID'].toLowerCase() }, // TODO: REVIEW whether to pass row number as second argument?
            link: 'https://en.wikipedia.org/wiki/<%= row.ID %>', // Tejesh: Link is not working if template is applied
            template: function (arg) {
              return "<td>ID " + arg.format + "</td>"
S Anand's avatar
S Anand committed
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
            }
          },
          { name: 'Name' },
          { name: 'Contitent' }
        ],
        pageSize: 3
      }).on('load', function (e) {
        t.equal($('.cell_template5 tr:first-of-type td:nth-of-type(1)').text().trim(), 'ID and')
        t.equal($('.cell_template5 tr:nth-of-type(2) td:nth-of-type(1)').text().trim(), 'ID are')
        t.equal($('.cell_template5 tr:nth-of-type(3) td:nth-of-type(1)').text().trim(), 'ID afg')
        t.end()
      })
    })

    tape('$().formhandler() renders rowTemplate as function', function (t) {
      $('.row_template1').formhandler({
        columns: [
          {
            name: 'ID',
784
            format: function (arg) { return arg.row['ID'].toLowerCase() }, // TODO: REVIEW whether to pass row number as second argument?
S Anand's avatar
S Anand committed
785
            link: 'https://en.wikipedia.org/wiki/<%= ID %>',
786
787
            template: function (arg) {
              return "<td>ID " + arg.format + "</td>"
S Anand's avatar
S Anand committed
788
789
790
791
            }
          },
          {
            name: 'c1',
792
            template: function (arg) {
S Anand's avatar
S Anand committed
793
794
795
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
796
              return '<td>' + arg.value + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="' + f(value) + '"></circle></svg></td>'
S Anand's avatar
S Anand committed
797
798
799
800
            }
          },
          {
            name: 'c2',
801
            format: function (arg) {
S Anand's avatar
S Anand committed
802
803
804
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
805
              return arg.row['c2'] + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="' + f(row['c2']) + '"></circle></svg>'
S Anand's avatar
S Anand committed
806
807
808
809
            }
          },
          { name: 'Continent' }
        ],
810
811
812
813
814
        rowTemplate: function (arg) {
          var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
          return '<tr><td>'+ arg.row['ID'] +'</td><td>' + arg.row['c2'] + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="' + f(arg.row['c1']) + '"></circle></svg></td><td>'+ arg.row['c2'] +'</td><td>'+ arg.row['Continent'] +'</td></tr>'
S Anand's avatar
S Anand committed
815
816
817
        },
        pageSize: 3
      }).on('load', function () {
818
819
820
821
822
823
824
825
826
827
828
829
830
        $.getJSON($(this).data('src'), function(result){
            var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
            for(var index = 0; index < 3; index++) {
              t.equals($('body > div.row_template1 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(1)').text(), result[index]['ID'])
              t.equals($('body > div.row_template1 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(2)').text(), ''+result[index]['c2'])
              t.equals($('body > div.row_template1 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(2) > svg > circle').attr('fill'), f(result[index]['c1']))
              t.equals($('body > div.row_template1 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(3)').text(), ''+result[index]['c2'])
              t.equals($('body > div.row_template1 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(4)').text(), ''+result[index]['Continent'])
            }
            t.end()
        });
S Anand's avatar
S Anand committed
831
832
833
834
835
836
837
838
      })
    })

    tape('$().formhandler() renders rowTemplate as string', function (t) {
      $('.row_template2').formhandler({
        columns: [
          {
            name: 'ID',
839
840
841
842
            format: function (arg) { return arg.row['ID'].toLowerCase() }, // TODO: REVIEW whether to pass row number as second argument?
            link: 'https://en.wikipedia.org/wiki/<%= row.ID %>',
            template: function (arg) {
              return "<td>ID " + arg.format + "</td>"
S Anand's avatar
S Anand committed
843
844
845
846
            }
          },
          {
            name: 'c1',
847
            template: function (arg) {
S Anand's avatar
S Anand committed
848
849
850
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
851
              return '<td>' + arg.value + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="' + f(arg.value) + '"></circle></svg></td>'
S Anand's avatar
S Anand committed
852
853
854
855
            }
          },
          {
            name: 'c2',
856
            format: function (arg) {
S Anand's avatar
S Anand committed
857
858
859
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
860
              return arg.row['c2'] + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="' + f(arg.row['c2']) + '"></circle></svg>'
S Anand's avatar
S Anand committed
861
862
863
864
            }
          },
          { name: 'Continent' }
        ],
865
866
867
868
869
870
871
        // TODO: Abinesh to use lodash template for `rowTemplate` instead of hardcoded string
        rowTemplate: `<tr>
                      <td><%- row.ID%></td>
                      <td><%- row.c1%></td>
                      <td><%- row.c2%></td>
                      <td><%- row.Continent%></td>
                    </tr>`,
S Anand's avatar
S Anand committed
872
873
        pageSize: 3
      }).on('load', function () {
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
        var f = d3.scaleLinear()
            .domain([0, 100])
            .range(['white', 'green'])
        $.getJSON($(this).data('src'), function(result){
          var f = d3.scaleLinear()
              .domain([0, 100])
              .range(['white', 'green'])
          for(var index = 0; index < 3; index++) {
            t.equals($('body > div.row_template2 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(1)').text(), result[index]['ID'])
            t.equals($('body > div.row_template2 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(2)').text(), ''+result[index]['c1'])
            t.equals($('body > div.row_template2 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(3)').text(), ''+result[index]['c2'])
            t.equals($('body > div.row_template2 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(4)').text(), ''+result[index]['Continent'])
          }
          t.end()
        });
      })
    })

    tape('$().formhandler() renders rowTemplate and overides the cellTempalte and format options', function (t) {
      $('.row_template3').formhandler({
        columns: [
          {
            name: 'ID',
            format: function (arg) { return arg.row['ID'].toLowerCase() }, // TODO: REVIEW whether to pass row number as second argument?
            link: 'https://en.wikipedia.org/wiki/<%= row["ID"] %>',
            template: function (arg) {
              return "<td>ID " + arg.format + "</td>"
            }
          },
          {
            name: 'c1',
            template: function (row) {
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
              return '<td>' + row.value + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="' + f(row.value) + '"></circle></svg></td>'
            }
          },
          {
            name: 'c2',
            format: function (arg) {
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
              return arg.row.c2 + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="' + f(arg.row.c2) + '"></circle></svg>'
            }
          },
          { name: 'Continent' }
        ],
        rowTemplate: function (arg) {
          var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
          return '<tr><td>'+ arg.row['ID'] +'</td><td>' + arg.row['c1'] + '</td><td>'+ arg.row['c2'] +'</td><td>'+ arg.row['Continent'] +'</td></tr>'
        },
        pageSize: 3
      }).on('load', function () {
        for(var index = 0; index < 3; index++) {
          t.equals($('body > div.row_template3 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(2) > svg > circle').length, 0)
          t.equals($('body > div.row_template3 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(3) > svg > circle').length, 0)
        }
S Anand's avatar
S Anand committed
935
936
937
938
        t.end()
      })
    })

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
    tape('$().formhandler() renders cellTemplate with the format value and with original value', function (t) {
      $('.row_template4').formhandler({
        columns: [
          {
            name: 'ID',
            format: function (arg) { return arg.row['ID'].toLowerCase() }, // TODO: REVIEW whether to pass row number as second argument?
            link: 'https://en.wikipedia.org/wiki/<%= row["ID"] %>',
            template: function (arg) {
              return "<td>ID " + arg.format + "</td>"
            }
          },
          { name: 'c1' },
          {
            name: 'Continent',
            format: function (arg) { return (arg.row['Continent']) ? arg.row['Continent'].toLowerCase() : '' }, // TODO: REVIEW whether to pass row number as second argument?
            template: function (arg) {
              var value = (arg.value) ? arg.value : ""
              return "<td>" + value + "</td>"
            }
          },
        ],
        pageSize: 3
      }).on('load', function () {
        $.getJSON($(this).data('src'), function(result){
          for(var index = 0; index < 3; index++) {
            t.equals($('body > div.row_template4 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(1)').text(), 'ID '+result[index]['ID'].toLowerCase(), 'Column `ID` is rendered with the formated value')
            t.equals($('body > div.row_template4 > div.position-relative > div.formhandler > div.table.table-responsive > table > tbody > tr:nth-child('+(index+1)+') > td:nth-child(3)').text(), result[index]['Continent'], 'Column `Continent` is rendered with the original value')
          }
          t.end()
        });
      })
    })

S Anand's avatar
S Anand committed
972
    tape('$().formhandler() renders grid with rowTemplate as function', function (t) {
973
      $('.row_template7').formhandler({
S Anand's avatar
S Anand committed
974
975
976
977
978
        columns: [
          {
            name: 'ID',
            format: function(row) {return row['ID'].toLowerCase() }, // TODO: REVIEW whether to pass row number as second argument?
            link: 'https://en.wikipedia.org/wiki/<%= ID %>',
979
980
            template: function (arg) {
              return "<td>ID " + arg.format + "</td>"
S Anand's avatar
S Anand committed
981
982
983
984
            }
          },
          {
            name: 'c1',
985
            template: function (arg) {
S Anand's avatar
S Anand committed
986
987
988
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
989
              return '<td>' + arg.value + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="'+  f(arg.value) +'"></circle></svg></td>'
S Anand's avatar
S Anand committed
990
991
992
993
            }
          },
          {
            name: 'c2',
994
            format: function (arg) {
S Anand's avatar
S Anand committed
995
996
997
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
998
              return  arg.row['c2'] + '<svg height="20" width="20"><circle r="10" cx="10" cy="10" stroke="black" stroke-width="3" fill="' + f(arg.row['c2']) + '"></circle></svg>'
S Anand's avatar
S Anand committed
999
1000
1001
1002
            }
          },
          { name: 'Continent' }
        ],
1003
1004
        rowTemplate: function(arg) {
          return `<div class="col-sm-3 formhandler-grid-cell d-inline-block p-3 box-shadow">
S Anand's avatar
S Anand committed
1005
1006
1007
1008
              <div class="thumbnail">
                <img class="img img-responsive" src="http://icons.iconarchive.com/icons/mazenl77/NX11/256/Folder-Default-icon.png"/>
                <div class="caption">
                  <div>
1009
                    <strong>ID</strong>:`+arg.row['ID']+`
S Anand's avatar
S Anand committed
1010
1011
                  </div>
                  <div>
1012
                    <strong>c1</strong>:`+arg.row['c1']+`
S Anand's avatar
S Anand committed
1013
1014
                  </div>
                  <div>
1015
                    <strong>Continent</strong>:`+arg.row['Continent']+`
S Anand's avatar
S Anand committed
1016
1017
1018
1019
1020
1021
1022
                  </div>
                </div>
              </div>
            </div>`
        },
        pageSize: 3
      }).on('load', function () {
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
        t.equals($('.row_template7 .formhandler-grid-cell').length, 3)

        t.equals($('.row_template7 .formhandler-grid-cell:first-child img').attr('src'), 'http://icons.iconarchive.com/icons/mazenl77/NX11/256/Folder-Default-icon.png')
        t.equals($('.row_template7 .formhandler-grid-cell:nth-child(2) img').attr('src'), 'http://icons.iconarchive.com/icons/mazenl77/NX11/256/Folder-Default-icon.png')
        t.equals($('.row_template7 .formhandler-grid-cell:nth-child(3) img').attr('src'), 'http://icons.iconarchive.com/icons/mazenl77/NX11/256/Folder-Default-icon.png')

        t.equals($('.row_template7 .formhandler-grid-cell:first-child .caption div:first-child').text().trim(), 'ID:AND')
        t.equals($('.row_template7 .formhandler-grid-cell:first-child .caption div:nth-child(2)').text().trim(), 'c1:35')
        t.equals($('.row_template7 .formhandler-grid-cell:first-child .caption div:nth-child(3)').text().trim(), 'Continent:Europe')

        t.equals($('.row_template7 .formhandler-grid-cell:nth-child(2) .caption div:first-child').text().trim(), 'ID:ARE')
        t.equals($('.row_template7 .formhandler-grid-cell:nth-child(2) .caption div:nth-child(2)').text().trim(), 'c1:24')
        t.equals($('.row_template7 .formhandler-grid-cell:nth-child(2) .caption div:nth-child(3)').text().trim(), 'Continent:Asia')

        t.equals($('.row_template7 .formhandler-grid-cell:nth-child(3) .caption div:first-child').text().trim(), 'ID:AFG')
        t.equals($('.row_template7 .formhandler-grid-cell:nth-child(3) .caption div:nth-child(2)').text().trim(), 'c1:28')
        t.equals($('.row_template7 .formhandler-grid-cell:nth-child(3) .caption div:nth-child(3)').text().trim(), 'Continent:Asia')
        t.end()
      })
    })


    tape('$().formhandler() test the star column with out title option', function (t) {
1046
1047
1048
      var class_count = 2
      var init_class_count = 0
      t.plan(1 * class_count)
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
      $('.test-star-without-title').formhandler({
        columns: [
          {
            name: '*'
          },
          {
            name: 'ID'
          },
          {
            name: 'c1'
          }
        ],
        pageSize: 3
      })
      .on('load', function () {
1064
1065
        init_class_count += 1
        t.ok($('.test-star-without-title table thead th a.dropdown-toggle').length == 17 * init_class_count)
1066
1067
1068
      })
    })

S Anand's avatar
S Anand committed
1069
1070
  </script>
</body>
1071

S Anand's avatar
S Anand committed
1072
</html>