|
| 1 | +(function(){ |
| 2 | + |
| 3 | + var data = [ |
| 4 | + { |
| 5 | + "name" : "test1", |
| 6 | + "id" : 1 |
| 7 | + }, |
| 8 | + { |
| 9 | + "name" : "test2", |
| 10 | + "id" : 2 |
| 11 | + }, |
| 12 | + { |
| 13 | + "name" : "test3", |
| 14 | + "id" : 3 |
| 15 | + } |
| 16 | + ]; |
| 17 | + |
| 18 | + //data formatters |
| 19 | + //single data formatter |
| 20 | + $.addTemplateFormatter("singleFormatter", |
| 21 | + function(value, template) { |
| 22 | + $(this).addClass('red'); |
| 23 | + return value.toUpperCase(); |
| 24 | + } |
| 25 | + ); |
| 26 | + |
| 27 | + $.addTemplateFormatter("singleFormatter2", |
| 28 | + function(value, template) { |
| 29 | + return 99; |
| 30 | + } |
| 31 | + ); |
| 32 | + |
| 33 | + //multiple data formatters |
| 34 | + $.addTemplateFormatter({ |
| 35 | + UpperCaseFormatter : function(value, template) { |
| 36 | + $(this).addClass('red'); |
| 37 | + return value.toUpperCase(); |
| 38 | + }, |
| 39 | + idFormatter : function(value, template) { |
| 40 | + if (value === 2){ |
| 41 | + $(this).addClass('yellow'); |
| 42 | + } |
| 43 | + return 'xx'; |
| 44 | + }, |
| 45 | + SameCaseFormatter : function(value, template) { |
| 46 | + if(template == "upper") { |
| 47 | + return value.toUpperCase(); |
| 48 | + } else { |
| 49 | + return value.toLowerCase(); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + /** |
| 55 | + * general Formatter test |
| 56 | + */ |
| 57 | + function general(assert){ |
| 58 | + return { |
| 59 | + name: "Templates Formatter", |
| 60 | + setup: function(){ |
| 61 | + $("#render").loadTemplate("#template2", data); |
| 62 | + }, |
| 63 | + test: function () { |
| 64 | + $('#render div.name').each(function(i){ |
| 65 | + var val = $(this).text(); |
| 66 | + |
| 67 | + //all names must be in uppercases |
| 68 | + var match = data[i].name.toUpperCase(); |
| 69 | + assert("match " + val + " !== " + match, val === match); |
| 70 | + |
| 71 | + //upperCaseFormatter manipulate element by adding a red class |
| 72 | + //check if we got that correctly |
| 73 | + var red = $(this).hasClass('red'); |
| 74 | + assert("has class red", red); |
| 75 | + |
| 76 | + //id also has been overriden to 'xx' |
| 77 | + var id = $(this).next().text(); |
| 78 | + assert("xx id", "xx" === id); |
| 79 | + }); |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * This test make sure that all added formatters will be available |
| 86 | + * even if we mix by adding single function formatter or multiple key |
| 87 | + * value style |
| 88 | + */ |
| 89 | + function singleFormatter (assert){ |
| 90 | + return { |
| 91 | + name: "Single Formatter", |
| 92 | + setup: function(){ |
| 93 | + $("#render").loadTemplate("#template3", data); |
| 94 | + }, |
| 95 | + test: function () { |
| 96 | + $('#render div.name').each(function(i){ |
| 97 | + var val = $(this).text(); |
| 98 | + |
| 99 | + var match = data[i].name.toUpperCase(); |
| 100 | + assert("match " + val + " !== " + match, val === match); |
| 101 | + |
| 102 | + //upperCaseFormatter manipulate element by adding a red class |
| 103 | + //check if we got that correctly |
| 104 | + var red = $(this).hasClass('red'); |
| 105 | + assert("has class red", red); |
| 106 | + |
| 107 | + //id also has been overriden to 'xx' |
| 108 | + var id = $(this).next().text(); |
| 109 | + assert("id xx !== " + id, "xx" === id); |
| 110 | + |
| 111 | + //hidden value manipulated by singleFormatter2 |
| 112 | + //using data-format-target to target value attr |
| 113 | + var hidVal = $(this).nextAll('.hidden').val(); |
| 114 | + assert( "Hidden Value " + hidVal +" !== 99", hidVal == 99); |
| 115 | + |
| 116 | + }); |
| 117 | + } |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + //TODO |
| 122 | + function SameCaseFormatter (assert){ |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + addTests(general,singleFormatter); |
| 127 | + |
| 128 | +})(); |
0 commit comments