From 62c22f2453f71ff570025f94d5aa17a6a6502fb0 Mon Sep 17 00:00:00 2001
From: Christiaan Westerbeek
Date: Thu, 22 Jan 2015 16:20:42 +0100
Subject: [PATCH] Expands tests to match all those on Ext JS site
---
package.json | 2 +-
test/test.js | 272 ++++++++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 259 insertions(+), 15 deletions(-)
diff --git a/package.json b/package.json
index c8cb5d2..372840b 100644
--- a/package.json
+++ b/package.json
@@ -6,7 +6,7 @@
"extjs",
"template"
],
- "version": "0.2.7",
+ "version": "0.2.8",
"repository": {
"type" : "git",
"url": "https://github.com/devotis/node-extjs-custom"
diff --git a/test/test.js b/test/test.js
index bb65ecf..974ea69 100644
--- a/test/test.js
+++ b/test/test.js
@@ -8,11 +8,11 @@ var data = {
company: 'Sencha Inc.',
drinks: ['Coffee', 'Water', 'More Coffee'],
kids: [
- { name: 'Aubrey', age: 17 },
- { name: 'Joshua', age: 13 },
- { name: 'Cale', age: 10 },
- { name: 'Nikol', age: 5 },
- { name: 'Solomon', age: 0 }
+ { name: 'Aubrey', age: 17, created: new Date(2014, 0, 1, 11, 45, 56)},
+ { name: 'Joshua', age: 13, created: new Date(2014, 0, 2, 12, 45, 56)},
+ { name: 'Cale', age: 10, created: new Date(2014, 0, 3, 13, 45, 56)},
+ { name: 'Nikol', age: 5, created: new Date(2014, 0, 4, 14, 45, 56)},
+ { name: 'Solomon', age: 0, created: new Date(2014, 0, 5, 15, 45, 56)}
]
};
@@ -47,17 +47,15 @@ describe('Class instantiation and inheritance', function() {
});
});
-describe('Template', function() {
- it('basic compiling should work', function() {
+describe('Template vs XTemplate', function() {
+ it('Template basic compiling should work', function() {
var tpl = new Ext.Template('Name: {0}, Age: {1}');
var html = tpl.apply(['John', 25]);
html.should.be.a.String.and.be.equal('Name: John, Age: 25');
});
-});
-describe('XTemplate', function() {
- it('basic compiling should work', function() {
+ it('XTemplate engine should not use Template engine', function() {
var tpl = new Ext.XTemplate(
'Name: {name}
',
'Title: {title}
',
@@ -73,15 +71,261 @@ describe('XTemplate', function() {
html.should.be.a.String.and.not.be.equal('Name: Don Griffin
Title: Senior Technomage
Company: Sencha Inc.
Kids: Don Griffin
')
html.should.be.a.String.and.be.equal('Name: Don Griffin
Title: Senior Technomage
Company: Sencha Inc.
Kids:
Aubrey
Joshua
Cale
Nikol
Solomon
');
});
+});
+
+describe('All examples on http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.XTemplate', function() {
+ describe('Auto filling of arrays', function() {
+ it('example 1 should work', function() {
+ var tpl = new Ext.XTemplate(
+ 'Kids: ',
+ '', // process the data.kids node
+ '{#}. {name}
', // use current array index to autonumber
+ '
'
+ );
+
+ var html = tpl.apply(data.kids);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('Kids:
1. Aubrey
2. Joshua
3. Cale
4. Nikol
5. Solomon
');
+ });
+
+ it('example 2, illustrating the for property, should work', function() {
+ var tpl = new Ext.XTemplate(
+ 'Name: {name}
',
+ 'Title: {title}
',
+ 'Company: {company}
',
+ 'Kids: ',
+ '', // interrogate the kids property within the data
+ '{name}
',
+ '
'
+ );
+
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('Name: Don Griffin
Title: Senior Technomage
Company: Sencha Inc.
Kids:
Aubrey
Joshua
Cale
Nikol
Solomon
');
+ });
+
+ it('example 3, using the special {.} variable inside a loop, should work', function() {
+ var tpl = new Ext.XTemplate(
+ '{name}\'s favorite beverages:
',
+ '',
+ ' - {.}
',
+ ''
+ );
+
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('Don Griffin\'s favorite beverages:
- Coffee
- Water
- More Coffee
');
+ });
+
+ it('example 4, accessing the parent object, should work', function() {
+ var tpl = new Ext.XTemplate(
+ 'Name: {name}
',
+ 'Kids: ',
+ '',
+ '',
+ '{name}
',
+ 'Dad: {parent.name}
',
+ '',
+ '
'
+ );
+
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('Name: Don Griffin
Kids:
Aubrey
Dad: Don Griffin
Joshua
Dad: Don Griffin
Cale
Dad: Don Griffin
Nikol
Dad: Don Griffin
');
+ });
+
+ it('example 5, using the foreach operator, should work', function() {
+ var tpl = new Ext.XTemplate(
+ '',
+ '',
+ '- {$}
', // the special **`{$}`** variable contains the property name
+ '- {.}
', // within the loop, the **`{.}`** variable is set to the property value
+ '',
+ '
'
+ );
+
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('- name
- Don Griffin
- title
- Senior Technomage
- company
- Sencha Inc.
- drinks
- kids
');
+ });
+ });
+
+ describe('Conditional processing with basic comparison operators', function() {
+ it('example 1, using the tpl tag and the if operator, should work', function() {
+ var tpl = new Ext.XTemplate(
+ 'Name: {name}
',
+ 'Kids: ',
+ '',
+ '',
+ '{name}
',
+ '',
+ '
'
+ );
+
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('Name: Don Griffin
Kids:
Aubrey
Joshua
Cale
Nikol
');
+ });
+
+ it('example 2a, using advanced conditionals, should work', function() {
+ var tpl = new Ext.XTemplate(
+ 'Name: {name}
',
+ 'Kids: ',
+ '',
+ '{name} is a ',
+ '',
+ 'teenager
',
+ '',
+ 'kid
',
+ '',
+ 'baby
',
+ '',
+ '
'
+ );
+
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('Name: Don Griffin
Kids:
Aubrey is a
teenager
Joshua is a
teenager
Cale is a
kid
Nikol is a
kid
Solomon is a
baby
');
+ });
+
+
+ it('example 2b, using advanced conditionals, should work', function() {
+ var tpl = new Ext.XTemplate(
+ 'Name: {name}
',
+ 'Kids: ',
+ '',
+ '{name} is a ',
+ '',
+ '',
+ 'girl
',
+ '',
+ 'boy
',
+ '',
+ '
'
+ );
+
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('Name: Don Griffin
Kids:
Aubrey is a
girl
Joshua is a
boy
Cale is a
boy
Nikol is a
girl
Solomon is a
boy
');
+ });
+
+ });
+
+ describe('Basic math support', function() {
+ it('example 1 should work', function() {
+ var tpl = new Ext.XTemplate(
+ 'Name: {name}
',
+ 'Kids: ',
+ '',
+ '', // <-- Note that the > is encoded
+ '{#}: {name}
', // <-- Auto-number each item
+ 'In 5 Years: {age+5}
', // <-- Basic math
+ 'Dad: {parent.name}
',
+ '',
+ '
'
+ );
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('Name: Don Griffin
Kids:
1: Aubrey
In 5 Years: 22
Dad: Don Griffin
2: Joshua
In 5 Years: 18
Dad: Don Griffin
3: Cale
In 5 Years: 15
Dad: Don Griffin
4: Nikol
In 5 Years: 10
Dad: Don Griffin
');
+ });
+
+ });
+
+ describe('Execute arbitrary inline code with special built-in template variables', function() {
+ it('example 1, using values and xindex, should work', function() {
+ var tpl = new Ext.XTemplate(
+ 'Name: {name}
',
+ 'Company: {[values.company.toUpperCase() + ", " + values.title]}
',
+ 'Kids: ',
+ '',
+ '',
+ '{name}',
+ '
',
+ '
'
+ );
+
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('Name: Don Griffin
Company: SENCHA INC., Senior Technomage
Kids:
Aubrey
Joshua
Cale
Nikol
Solomon
');
+ });
+
+ });
+
+ describe('Template member functions', function() {
+ it('example 1, using member functions, should work', function() {
+ var tpl = new Ext.XTemplate(
+ 'Name: {name}
',
+ 'Kids: ',
+ '',
+ '',
+ 'Girl: {name} - {age}
',
+ '',
+ 'Boy: {name} - {age}
',
+ '',
+ '',
+ '{name} is a baby!
',
+ '',
+ '
',
+ {
+ // XTemplate configuration:
+ disableFormats: true,
+ // member functions:
+ isGirl: function(name){
+ return name == 'Aubrey' || name == 'Nikol';
+ },
+ isBaby: function(age){
+ return age < 1;
+ }
+ }
+ );
+
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String
+ .and.be.equal('Name: Don Griffin
Kids:
Girl: Aubrey - 17
Boy: Joshua - 13
Boy: Cale - 10
Girl: Nikol - 5
Boy: Solomon - 0
Solomon is a baby!
');
+ });
+
+ });
+});
+
+describe('Special formatting with XTemplate', function() {
it('date formatting functions should work', function() {
var tpl = new Ext.XTemplate(
- '{name:date("Y-m-d")}
'
+ '',
+ '',
+ '{created:date("Y-m-d")}
',
+ '',
+ ''
);
- var html = tpl.apply({name: new Date(2014, 10, 29)});
+ var html = tpl.apply(data);
//The test below is to see that it's Actually XTemplate at work an not Template
- html.should.be.a.String.and.be.equal('2014-11-29
');
+ html.should.be.a.String.and.be.equal('2014-01-01
2014-01-02
2014-01-03
2014-01-04
');
});
-});
+ it('combining built-in template variables with date formatting functions should work', function() {
+ var tpl = new Ext.XTemplate(
+ '',
+ '',
+ '{[Ext.util.Format.date(values.created, "H:i") || \'-\']}
',
+ '',
+ ''
+ );
+
+ var html = tpl.apply(data);
+ //The test below is to see that it's Actually XTemplate at work an not Template
+ html.should.be.a.String.and.be.equal('11:45
12:45
13:45
14:45
');
+ });
+
+});