test-formhandler.html 49.1 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
  <div class="hide_col3" data-src="/test/formhandler_csv.json"></div>
  <div class="hide_col2" data-src="/test/formhandler_csv.json"></div>
  <div class="hide_col" data-src="/test/formhandler_csv.json"></div>
43

44
45
  <div class="cell_template1" data-src="/test/formhandler_csv.json"></div>
  <div class="cell_template2" data-src="/test/formhandler_csv.json"></div>
46

47
48
49
  <div class="test_star_overrride" data-src="/test/formhandler_csv.json"></div>
  <div class="test-star-without-title" data-src="/test/formhandler_csv.json"></div>
  <div class="test-star-with-title" data-src="/test/formhandler_csv.json"></div>
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
  <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
  <div class="fh1" data-src="formhandler_csv.json"></div>
89
90
91
  <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>
92
93
94
95
96
  <div class="fh5" data-src="formhandler_csv.json"></div>
  <div class="fh6" data-src="formhandler_csv.json"></div>
  <div class="fh7" data-src="formhandler_csv.json"></div>
  <div class="fh8" data-src="formhandler_csv.json"></div>
  <div class="fh9" data-src="formhandler_csv.json"></div>
97
  <div class="fh10"></div>
98
99
100
101
102
103
104
105
  <div class="fh11" data-src="formhandler_csv.json"></div>
  <div class="fh12" data-src="formhandler_csv.json"></div>
  <div class="formhandler" data-src="formhandler_csv.json" data-page-size="10"></div>
  <div class="fhgrid1" data-table="grid" data-src="formhandler_csv.json"></div>
  <div class="fhgrid2" data-table="grid" data-src="formhandler_csv.json" data-icon="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTZ1bqTcxkaGSSIl5meU4JYO1dvUVbqP71z0hH8M6f6Yh9JpZg8"></div>
  <div class="fhgrid3" data-table="grid" data-src="formhandler_csv.json" data-classes="formhandler-grid"></div>
  <div class="fhgrid4" data-table="grid" data-src="formhandler_csv.json"></div>
  <div class="fhgrid5" data-table="grid" data-src="formhandler_csv.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
123
124
        .on('load', function (args) {
          // formhandler-small.json contains only two rows
          t.notEqual($('.fh1 tbody tr').length, 2)
125
126
127
          t.end()
        })
    })
128
    tape('$().formhandler() hide options doesnot display the column', function(t) {
129
130
      var class_count = 2
      t.plan(1 * class_count)
131
132
133
134
135
136
137
138
139
140
141
142
143
144
      $('.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) {
145
146
147
      var class_count = 2
      var current_class_count = 0
      t.plan(2 * class_count)
148
149
150
151
152
153
154
155
156
157
158
159
160
161
      $('.hide_col2')
        .formhandler({
          columns: [
            {
              name: 'Continent',
              hide: true
            },
            {
              name: 'c1'
            }
          ],
          pageSize: 3
        })
        .on('load', function () {
162
163
          current_class_count += 1
          t.equals($("body > div.hide_col2 > div.position-relative > div.formhandler > div.table > table > thead > tr").length, current_class_count)
164
165
166
167
          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) {
168
169
170
      var class_count = 1
      var current_class_count = 0
      t.plan(2* class_count)
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
      $('.hide_col3')
        .formhandler({
          columns: [
            {
              name: '*'
            },
            {
              name: 'Continent',
              hide: true
            },
            {
              name: 'c1'
            }
          ],
          pageSize: 3
        })
        .on('load', function () {
188
189
190
191
192
          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)
          }
193
194
        })
    })
195
    tape('$().formhandler() uses option.src if no data-src exists', function (t) {
196
197
198
199
200
201
202
      $('.fh2')
        .formhandler({ src: 'formhandler-small.json' })
        .on('load', function () {
          t.equal($('.fh2 tbody tr').length, 2)
          t.end()
        })
    })
203
    tape('$().formhandler() shows default controls if meta is present', function (t) {
204
205
206
      $('.fh3')
        .formhandler({
          src: 'formhandler-small.json',
207
          transform: function (obj) {
208
209
210
            obj.meta.count = obj.meta.limit = obj.data.length
          }
        })
211
        .on('load', function () {
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
          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()
        })
    })

231
    tape('$().formhandler() disables controls via data-attributes', function (t) {
232
233
234
      $('.fh4')
        .formhandler({
          src: 'formhandler-small.json',
235
          transform: function (obj) {
236
237
238
            obj.meta.count = obj.meta.limit = obj.data.length
          }
        })
239
        .on('load', function () {
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
          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
276
          t.equal($('.fh6 thead tr th').length, 18)
277
278
279
280
281
282
283
284
285
          // 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()
        })
    })
286
    tape('$().formhandler() allows updating column info only for specific columns override test', function (t) {
287
288
289
      var class_count = 2
      var current_class_count = 0
      t.plan(6 * class_count)
290
291
292
293
294
295
296
297
      $('.test_star_overrride')
        .formhandler({
          columns: [
            { name: 'Continent', title: 'CustomTitle', sort: true },
            { name: '*', title: 'SameName' }
          ]
        })
        .on('load', function () {
298
          current_class_count += 1
299
          // no. of columns in meta data must be same as rendered columns
300
          t.equal($('.test_star_overrride thead tr th').length, 18 * current_class_count)
301
302
303
304
          // 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/))
305
          t.equal($('.test_star_overrride thead tr').text().match(/CustomTitle/).length, 1)
306
          // Rest of 16 columns must have title as 'SameName'
307
          t.equal($('.test_star_overrride thead tr').text().split('SameName').length - 1, 17 * current_class_count)
308
          // column is hideable by default
309
          t.equal($('.test_star_overrride [data-col="Continent"] div:nth-child(1) div a:last-of-type').text().trim(), "Hide".repeat(current_class_count))
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
    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: [
345
            { name: 'Continent', format: function (arg) { return arg.row['Continent'].toUpperCase() } }
346
347
348
349
350
351
352
353
          ]
        })
        .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()
        })
354
    })
355
356
    tape('$().formhandler() applies format option (date type) option mentioned in options.columns', function (t) {
      $('.fh10')
357
358
359
360
361
362
363
364
365
366
        .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
367

368
369
370
371
          // 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')
372
373


374
375
376
377
378
379
380
381
382
          t.end()
        })
    })
    tape('$().formhandler() link attr will let user goto that link instead of applying filter', function (t) {
      $('.fh11')
        .formhandler({
          columns: [
            {
              name: 'Continent',
383
              link: 'https://en.wikipedia.org/wiki/<%= row["Continent"].split(" ").join(" ") %>'
384
385
386
387
388
            }
          ]
        })
        .on('load', function () {
          // no. of columns in meta data must be same as rendered columns
389
          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())
390
391
392
393
394
395
396
397
398
          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',
399
              link: function (arg) { return 'https://en.wikipedia.org/wiki/' + arg.row['Continent'].split(" ").join(" ") }
400
401
402
403
404
            }
          ]
        })
        .on('load', function () {
          // no. of columns in meta data must be same as rendered columns
405
          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
406
407
408
409
410
411
412
413
          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')
414
415
416
417
418
419
        .formhandler({
          name: 'fhname13',
          columns: [
            {
              name: 'Continent'
            }
S Anand's avatar
S Anand committed
420
421
422
423
424
          ]
        })
        .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(' '),
425
            'Europe')
S Anand's avatar
S Anand committed
426
427
428
429
430
431
432
433
434
          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({}, '', '#')
435
436
          t.end()
        })
S Anand's avatar
S Anand committed
437
    })
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
    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
457
          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")
458
          // check if Edit Button changes to Save Button when clicked
459
460
          $('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")
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
          // 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
534
          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")
535
          // check if Edit Button changes to Save Button when clicked
536
537
          $('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")
S Anand's avatar
S Anand committed
538
539
          var td_elms = $("body > div.fh17 > div.position-relative > div.formhandler > div.table > table > tbody > tr > td ")
          var input_elms = $("body > div.fh17 > div.position-relative > div.formhandler > div.table > table > tbody > tr > td > input")
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
          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
561
562
563
564
565
    // 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 () {
566
          t.equals($('.fhgrid1 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow .thumbnail').length, 100)
S Anand's avatar
S Anand committed
567
568
569
570
571
572
573
574
          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 () {
575
          t.equals($('.fhgrid2 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow .thumbnail').length, 100)
S Anand's avatar
S Anand committed
576
577
578
579
580
581
582
583
584
585
          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)
586
        t.equals($('.fhgrid3 .formhandler-grid .formhandler-grid .thumbnail').length, 100)
S Anand's avatar
S Anand committed
587
588
589
590
591
592
          t.end()
        })
    })

    tape('$().formhandler() renders grid from data-src and overriding default options.icon with the jsoptions.icon', function (t) {
      $('.fhgrid4')
593
        .formhandler({ src: 'formhandler-small.json', icon: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSck4Qkjt376rXmEUqCj1MVPsgF4ftoSg4ok2EXdansLiF9VddD' })
S Anand's avatar
S Anand committed
594
        .on('load', function () {
595
          t.equals($('.fhgrid4 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow .thumbnail').length, 100)
S Anand's avatar
S Anand committed
596
597
598
599
600
601
602
          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')
603
        .formhandler({ src: 'formhandler-small.json', classes: 'formhandler-grid' })
S Anand's avatar
S Anand committed
604
605
        .on('load', function () {
          t.equals($('.fhgrid5 .formhandler-grid .formhandler-grid-cell.d-inline-block.p-3.box-shadow .thumbnail').length, 0)
606
          t.equals($('.fhgrid5 .formhandler-grid .formhandler-grid .thumbnail').length, 100)
S Anand's avatar
S Anand committed
607
608
609
610
          t.end()
        })
    })

611
    tape('$().formhandler() renders template with formatted cell contents', function (t) {
612
613
614
      var class_count = 2
      var current_class_count = 0
      t.plan(3 * class_count)
S Anand's avatar
S Anand committed
615
616
617
618
      $('.cell_template1').formhandler({
        columns: [
          {
            name: 'ID',
619
620
621
622
            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
623
624
625
626
627
628
629
            }
          },
          { name: 'Name' },
          { name: 'Contitent' }
        ],
        pageSize: 3
      }).on('load', function (e) {
630
631
632
633
        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
634
635
636
637
      })
    })

    tape('$().formhandler() renders template option with unusual html tags', function (t) {
638
639
640
      var class_count = 2
      var current_class_count = 0
      t.plan(3 * class_count)
S Anand's avatar
S Anand committed
641
642
643
644
645
      // span, div, p, i,
      $('.cell_template2').formhandler({
        columns: [
          {
            name: 'ID',
646
647
648
649
            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
650
651
652
653
654
655
656
            }
          },
          { name: 'Name' },
          { name: 'Contitent' }
        ],
        pageSize: 3
      }).on('load', function () {
657
658
659
660
        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
661
662
663
664
665
666
667
668
      })
    })

    tape('$().formhandler() renders template with svg elements', function (t) {
      $('.cell_template3').formhandler({
        columns: [
          {
            name: 'ID',
669
670
671
            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
672
673
674
          },
          {
            name: 'c1',
675
            template: function (arg) {
S Anand's avatar
S Anand committed
676
677
678
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
679
              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
680
681
682
683
            }
          },
          {
            name: 'c2',
684
            format: function (arg) {
S Anand's avatar
S Anand committed
685
686
687
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
688
              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
689
690
            }
          },
691
692
          { 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
693
        ],
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
        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
711
      }).on('load', function () {
712
        t.equal($('.cell_template3 tr:first-of-type td:nth-of-type(1)').text().trim(), 'and')
S Anand's avatar
S Anand committed
713
714
715
        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')
716
        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
717
718
        t.equal($('.cell_template3 tr:first-of-type td:nth-of-type(3) svg circle').length, 1)

719
        t.equal($('.cell_template3 tr:nth-of-type(2) td:nth-of-type(1)').text().trim(), 'are')
S Anand's avatar
S Anand committed
720
721
722
        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')
723
        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
724
725
        t.equal($('.cell_template3 tr:nth-of-type(2) td:nth-of-type(3) svg circle').length, 1)

726
        t.equal($('.cell_template3 tr:nth-of-type(3) td:nth-of-type(1)').text().trim(), 'afg')
S Anand's avatar
S Anand committed
727
728
729
        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')
730
        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
731
732
733
734
735
736
737
738
739
740
        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',
741
742
            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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
            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',
762
763
764
765
            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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
            }
          },
          { 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',
785
            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
786
            link: 'https://en.wikipedia.org/wiki/<%= ID %>',
787
788
            template: function (arg) {
              return "<td>ID " + arg.format + "</td>"
S Anand's avatar
S Anand committed
789
790
791
792
            }
          },
          {
            name: 'c1',
793
            template: function (arg) {
S Anand's avatar
S Anand committed
794
795
796
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
797
              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
798
799
800
801
            }
          },
          {
            name: 'c2',
802
            format: function (arg) {
S Anand's avatar
S Anand committed
803
804
805
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
806
              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
807
808
809
810
            }
          },
          { name: 'Continent' }
        ],
811
812
813
814
815
        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
816
817
818
        },
        pageSize: 3
      }).on('load', function () {
819
820
821
822
823
        $.getJSON($(this).data('src'), function(result){
            var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
            for(var index = 0; index < 3; index++) {
S Anand's avatar
S Anand committed
824
825
826
827
828
              t.equals($('body > div.row_template1 > div.position-relative > div.formhandler > div.table > 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 > 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 > 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 > 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 > tbody > tr:nth-child('+(index+1)+') > td:nth-child(4)').text(), ''+result[index]['Continent'])
829
830
831
            }
            t.end()
        });
S Anand's avatar
S Anand committed
832
833
834
835
836
837
838
839
      })
    })

    tape('$().formhandler() renders rowTemplate as string', function (t) {
      $('.row_template2').formhandler({
        columns: [
          {
            name: 'ID',
840
841
842
843
            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
844
845
846
847
            }
          },
          {
            name: 'c1',
848
            template: function (arg) {
S Anand's avatar
S Anand committed
849
850
851
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
852
              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
853
854
855
856
            }
          },
          {
            name: 'c2',
857
            format: function (arg) {
S Anand's avatar
S Anand committed
858
859
860
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
861
              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
862
863
864
865
            }
          },
          { name: 'Continent' }
        ],
866
867
868
869
870
871
872
        // 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
873
874
        pageSize: 3
      }).on('load', function () {
875
876
877
878
879
880
881
882
        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++) {
S Anand's avatar
S Anand committed
883
884
885
886
            t.equals($('body > div.row_template2 > div.position-relative > div.formhandler > div.table > 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 > 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 > 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 > tbody > tr:nth-child('+(index+1)+') > td:nth-child(4)').text(), ''+result[index]['Continent'])
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
          }
          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++) {
S Anand's avatar
S Anand committed
933
934
          t.equals($('body > div.row_template3 > div.position-relative > div.formhandler > div.table > 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 > tbody > tr:nth-child('+(index+1)+') > td:nth-child(3) > svg > circle').length, 0)
935
        }
S Anand's avatar
S Anand committed
936
937
938
939
        t.end()
      })
    })

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
    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++) {
S Anand's avatar
S Anand committed
965
966
            t.equals($('body > div.row_template4 > div.position-relative > div.formhandler > div.table > 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 > tbody > tr:nth-child('+(index+1)+') > td:nth-child(3)').text(), result[index]['Continent'], 'Column `Continent` is rendered with the original value')
967
968
969
970
971
972
          }
          t.end()
        });
      })
    })

S Anand's avatar
S Anand committed
973
    tape('$().formhandler() renders grid with rowTemplate as function', function (t) {
974
      $('.row_template7').formhandler({
S Anand's avatar
S Anand committed
975
976
977
978
979
        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 %>',
980
981
            template: function (arg) {
              return "<td>ID " + arg.format + "</td>"
S Anand's avatar
S Anand committed
982
983
984
985
            }
          },
          {
            name: 'c1',
986
            template: function (arg) {
S Anand's avatar
S Anand committed
987
988
989
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
990
              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
991
992
993
994
            }
          },
          {
            name: 'c2',
995
            format: function (arg) {
S Anand's avatar
S Anand committed
996
997
998
              var f = d3.scaleLinear()
                .domain([0, 100])
                .range(['white', 'green'])
999
              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
1000
1001
1002
1003
            }
          },
          { name: 'Continent' }
        ],
1004
1005
        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
1006
1007
1008
1009
              <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>
1010
                    <strong>ID</strong>:`+arg.row['ID']+`
S Anand's avatar
S Anand committed
1011
1012
                  </div>
                  <div>
1013
                    <strong>c1</strong>:`+arg.row['c1']+`
S Anand's avatar
S Anand committed
1014
1015
                  </div>
                  <div>
1016
                    <strong>Continent</strong>:`+arg.row['Continent']+`
S Anand's avatar
S Anand committed
1017
1018
1019
1020
1021
1022
1023
                  </div>
                </div>
              </div>
            </div>`
        },
        pageSize: 3
      }).on('load', function () {
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
        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) {
1047
1048
1049
      var class_count = 2
      var init_class_count = 0
      t.plan(1 * class_count)
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
      $('.test-star-without-title').formhandler({
        columns: [
          {
            name: '*'
          },
          {
            name: 'ID'
          },
          {
            name: 'c1'
          }
        ],
        pageSize: 3
      })
      .on('load', function () {
1065
        init_class_count += 1
1066
        t.ok($('.test-star-without-title table thead th a.dropdown-toggle').length == 18 * init_class_count)
1067
1068
1069
      })
    })

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

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