89 |
135 |
136 |
--------------------------------------------------------------------------------
/spec/small.html:
--------------------------------------------------------------------------------
1 |
{{item.content}}
2 |
--------------------------------------------------------------------------------
/spec/spec.js:
--------------------------------------------------------------------------------
1 | var tmpl, data, expectedOutput, output;
2 | var ht = require('../index.js');
3 |
4 | describe("ht", () => {
5 |
6 | it("expressions", () => {
7 | /*******************************************************
8 | * `{{}}` expression test
9 | *******************************************************/
10 | expect(ht("{{foo}}", { foo: 1 })).toEqual("1");
11 | expect(ht("{{x.foo}}", { x: { foo: 1 } })).toEqual("1");
12 | expect(ht("{{x.foo}} {{ x.bar }}", { x: { foo: 1, bar: 2 } })).toEqual("1 2");
13 | });
14 |
15 | describe("pipes", () => {
16 |
17 | it('lowercase/uppercase', () => {
18 | expect(ht("{{x.foo | lowercase }} {{ x.bar | lowercase }}", { x: { foo: 'A', bar: 'B' } })).toEqual("a b");
19 | });
20 |
21 | it('limitTo', () => {
22 | expect(ht("{{value | limitTo:2 }}", { value: "abcd" })).toEqual("ab");
23 | expect(ht("{{value | limitTo:2:1 }}", { value: "abcd" })).toEqual("bc");
24 | expect(ht("{{value | limitTo }}", { value: "abcd" })).toEqual("abcd");
25 | expect(ht("{{[1, 2, 3, 4] | limitTo:2:2 | json:0 }}", {})).toEqual("[3,4]");
26 | });
27 |
28 | it('number', () => {
29 | expect(ht("{{num | number:2 }}", { num: 51.2534 })).toEqual("51.25");
30 | });
31 |
32 | it('currency', () => {
33 | expect(ht("{{num | currency:'':2 }}", { num: 51.2534 })).toEqual("51.25");
34 | expect(ht("{{num | currency }}", { num: 51.2534 })).toEqual("$51.25");
35 | expect(ht("{{num | currency:undefined:0 }}", { num: 51.2534 })).toEqual("$51");
36 | });
37 |
38 | it('date', () => {
39 | expect(ht("{{1288323623006 | date:'medium':'+0200' }}", {})).toEqual("Oct 29, 2010 5:40:23 AM");
40 | expect(ht("{{1288323623006 | date:\"MM/dd/yyyy 'at' h:mma\":'+0200' }}", {})).toEqual("10/29/2010 at 5:40AM");
41 | expect(ht("{{1288323623006 | date:'MM/dd/yyyy \\'a:t\\' h:mma':'+0200' }}", {})).toEqual("10/29/2010 a:t 5:40AM");
42 | expect(ht("{{value | date:\"dd-MM-yyyy '|' h:mma\":'+0200' }}", { value: new Date("2017-09-25T11:00:50.691Z") })).toEqual("25-09-2017 | 1:00PM");
43 | expect(ht("{{'2018-01-14T22:26:59.680Z' | date:'MM/dd/yyyy \\'a:t\\' h:mma':'+0000' }}", {})).toEqual("01/14/2018 a:t 10:26PM");
44 | });
45 |
46 | it('json', () => {
47 | expect(ht("{{value | json:0 }}", { value: { a: 1 } })).toEqual('{"a":1}');
48 | });
49 |
50 | it('custom', () => {
51 | ht.pipes.or = function (options, value, param) {
52 | return value ? value : param;
53 | };
54 | expect(ht("{{value | or: '12|4|b' | uppercase }}", { value: false })).toEqual("12|4|B");
55 | });
56 | });
57 |
58 | it("advanced expressions", () => {
59 | /*******************************************************
60 | * advanced expressions test
61 | *******************************************************/
62 | expect(ht("
YES
", { x: { foo: 10, bar: 11 } })).toEqual("");
63 | expect(ht("
YES
", { x: { foo: true } })).toEqual("
YES
");
64 | expect(ht("
NO
", { x: { foo: 'foo', bar: 'bar' } })).toEqual("
NO
");
65 | });
66 |
67 | it("if", () => {
68 | /*******************************************************
69 | * `ht-if` expression test
70 | *******************************************************/
71 | expect(ht("
YES
", { x: { foo: true } })).toEqual("
YES
");
72 | expect(ht("
YES
", { x: { foo: true } })).toEqual("");
73 | expect(ht("
NO
", { x: { foo: true } })).toEqual("
NO
");
74 | });
75 |
76 | it("show", () => {
77 | /*******************************************************
78 | * `ht-show` expression test
79 | *******************************************************/
80 | expect(ht("
YES
", { x: { foo: true } })).toEqual("
YES
");
81 | expect(ht("
YES
", { x: { foo: true } })).toEqual("");
82 | expect(ht("
NO
", { x: { foo: true } })).toEqual("
NO
");
83 | });
84 |
85 |
86 | it("hide", () => {
87 | /*******************************************************
88 | * `ht-hide` expression test
89 | *******************************************************/
90 | expect(ht("
YES
", { x: { foo: true } })).toEqual("");
91 | expect(ht("
YES
", { x: { foo: true } })).toEqual("
YES
");
92 | expect(ht("
NO
", { x: { foo: true } })).toEqual("");
93 | });
94 |
95 | it("repeat", () => {
96 | /*******************************************************
97 | * `ht-repeat` expression test
98 | *******************************************************/
99 | expect(ht("
{{el}}", {})).toEqual("
123");
100 | expect(ht("
{{el}}", { list: [1, 2, 3] })).toEqual("
123");
101 | expect(ht(
102 | "
{{v}}",
103 | { list: { a: 1, b: 2, c: 3 } }
104 | )).toEqual("
123");
105 |
106 | expect(ht(
107 | "
{{v}}",
108 | { list: { a: 1, b: 2, c: 3 } }
109 | )).toEqual("
123");
110 |
111 | expect(ht(
112 | "
{{k}}{{v}}",
113 | { list: { a: 1, b: 2, c: 3 } }
114 | )).toEqual("
a1b2c3");
115 |
116 | expect(ht(
117 | "
{{i}}",
118 | { list: [0, 1, 2, 3] }
119 | )).toEqual("
123");
120 |
121 | expect(ht(
122 | "
{{i}}",
123 | { list: [0, 1, 2, 3] }
124 | )).toEqual("
12");
125 |
126 | expect(ht(
127 | "
{{i}}",
128 | { list: [0, 1, 2, 3], filterFn: (v) => v > 0 }
129 | )).toEqual("
123");
130 |
131 | expect(ht(
132 | "
{{i}}",
133 | { list: [0, 1, 2, 3], filterFn: (v) => v > 0 }
134 | )).toEqual("
123");
135 |
136 | expect(ht(
137 | "
{{i}}",
138 | { list: [0, 1, 2, 3], filterFn: (v) => v > 0 }
139 | )).toEqual("
23");
140 | });
141 |
142 | it('class', () => {
143 | /*******************************************************
144 | * `ht-class` expression test
145 | *******************************************************/
146 | expect(ht("
YES
", { item: { classes: 'highlight' } })).toEqual("
YES
");
147 | expect(ht("
YES
", { item: { classes: { highlight: true } } })).toEqual("
YES
");
148 | expect(ht("
YES
", { item: { classes: { highlight: true, odd: true } } })).toEqual("
YES
");
149 | expect(ht("
YES
", { item: { classes: ['odd', { highlight: true }] } })).toEqual("
YES
");
150 | expect(ht("
YES
", { item: { classes: ['odd', { highlight: true }] } })).toEqual("
YES
");
151 | expect(ht("
YES
", { item: { classes: ['odd', { highlight: false }] } })).toEqual("
YES
");
152 | expect(ht("
YES
", { item: { highlight: true } })).toEqual("
YES
");
153 |
154 | });
155 |
156 | it('bind', () => {
157 | /*******************************************************
158 | * `ht-bind` expression test
159 | *******************************************************/
160 | expect(ht("
", { title: 'YES' })).toEqual("
YES
");
161 | expect(ht("
", { title: '
YES' })).toEqual("
YES
");
162 | });
163 |
164 | it('style', () => {
165 |
166 | expect(ht("
YES
", { styles: { color: 'red' } })).toEqual("
YES
");
167 | expect(ht("
YES
", { styles: { 'font-size': '12px' } })).toEqual("
YES
");
168 | expect(ht("
YES
", { styles: { 'font-size': '12px', width: '45px' } })).toEqual("
YES
");
169 | expect(ht("
YES
", { fontSize: '12px' })).toEqual("
YES
");
170 | });
171 |
172 | it("include", () => {
173 | /*******************************************************************
174 | * `ht-include` expression test, passed as non existing property for backwards compatibility
175 | * file does not exist, so it will print out as html, the file name
176 | *******************************************************************/
177 | expect(ht("
", {})).toMatch(/
.*file1.html<\/div>/);
178 |
179 | /*******************************************************************
180 | * `ht-include` expression test, passed as string
181 | * file does not exist, so it will print out as html, the file name
182 | *******************************************************************/
183 | expect(ht("
", {})).toMatch(/
.*file1.html<\/div>/);
184 |
185 | /*******************************************************************
186 | * `ht-include` expression test, passed as property
187 | * file does not exist, so it will print out as html, the file name
188 | *******************************************************************/
189 | expect(ht("
", { item: { template: 'file2.html' } })).toMatch(/
.*file2.html<\/div>/);
190 |
191 | /*******************************************************************
192 | * `ht-include` expression test, passed as property in a repeat
193 | * file does not exist, so it will print out as html, the file name
194 | *******************************************************************/
195 | var exampleResult = ht("
", { items: [{ template: 'file3.html' }, { content: 'foo', template: 'spec/small.html' }] });
196 | expect(exampleResult).toMatch(/
.*file3.html<\/div>/);
197 | expect(exampleResult).toMatch(/
foo<\/span>/);
198 |
199 | /*******************************************************************
200 | * `ht-include` expression test, passed as property in a nested repeat with key value
201 | * file does not exist, so it will print out as html, the file name
202 | *******************************************************************/
203 | var exampleResult2 = ht("", { items: [{ items: [{ template: 'file3.html' }] }, { items: [{ content: 'foo', template: 'spec/small.html' }] }] });
204 | expect(exampleResult2).toMatch(/
.*file3.html<\/div>/);
205 | expect(exampleResult2).toMatch(/
foo<\/span>/);
206 |
207 | });
208 |
209 | it("directory", () => {
210 | /*******************************************************************
211 | * * includeDirs test
212 | * *******************************************************************/
213 | expect(ht("", { item: { content: 'test1' } }, { prefix: 'ng', includeDirs: [__dirname, __dirname + '/includes'] })).toEqual('test1
');
214 | expect(ht("", { item: { content: 'test1' } }, { prefix: 'ng', includeDirs: [__dirname + '/includes', __dirname] })).toEqual('');
215 | expect(ht("", { item: { content: 'test1' } }, { prefix: 'ng', includeDirs: [__dirname + '/shared', __dirname + '/includes', __dirname] })).toEqual('');
216 |
217 | });
218 |
219 | it("include context", () => {
220 | expect(ht("", { foo: { content: 'test1' } }, { prefix: 'ng', includeDirs: [__dirname] })).toEqual('');
221 | expect(ht("", { items: [{ foo: { content: 'test1' } }] }, { prefix: 'ng', includeDirs: [__dirname] })).toEqual('');
222 | });
223 |
224 | it("jsdoc template", () => {
225 | /*******************************************************************
226 | * jsdoc template test
227 | *******************************************************************/
228 | expect(function () {
229 | ht("spec/layout.html",
230 | { nav: [], children: [{ members: [], functions: [] }] },
231 | { jsMode: false, prefix: 'ng' });
232 | }).not.toThrow();
233 | });
234 |
235 | it("cache and preprocess", () => {
236 | /*******************************************************************
237 | * cache and preprocess test
238 | *******************************************************************/
239 |
240 | var exampleResult3 = ht("", { item: { content: 'foo' } }, {
241 | prefix: 'ng', cache: 'test', preprocess: function (tpl) {
242 | tpl = tpl.replace(/span/g, 'div');
243 | return tpl;
244 | }
245 | });
246 | expect(exampleResult3).toMatch(/foo<\/div>/);
247 | expect(ht.cache.get('test')).toMatch(/spec\/small\.html/);
248 | expect(ht.cache.get('test$$spec/small.html')).toMatch(/item\.content/);
249 |
250 | expect(ht("
", { item: { content: 'foo' } }, {
251 | prefix: 'ng', cache: 'test', preprocess: function (tpl) {
252 | tpl = tpl.replace(/span/g, 'div');
253 | return tpl;
254 | }
255 | })).toMatch(/
foo<\/div>/);
256 |
257 | ht.cache.remove('test');
258 | expect(ht.cache.get('test')).toBeUndefined();
259 | expect(ht.cache.get('test$$spec/small.html')).toBeUndefined();
260 | });
261 | });
--------------------------------------------------------------------------------
/spec/support/jasmine.json:
--------------------------------------------------------------------------------
1 | {
2 | "spec_dir": "spec",
3 | "spec_files": [
4 | "**/*[sS]pec.js"
5 | ],
6 | "helpers": [
7 | "helpers/**/*.js"
8 | ],
9 | "stopSpecOnExpectationFailure": false,
10 | "random": false
11 | }
12 |
--------------------------------------------------------------------------------