Skip to content

Commit dc3cf31

Browse files
committed
Improve tests
1 parent a98f8e1 commit dc3cf31

File tree

4 files changed

+198
-62
lines changed

4 files changed

+198
-62
lines changed

test/functional/index.js

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,139 @@ var expect = require("chai").expect;
33
Globalize.locale("en");
44

55
describe("The compiled `basic.js`", function() {
6-
it("should include dateFormatter", function() {
6+
/**
7+
* Date
8+
*/
9+
it("should include support for dateFormatter", function() {
710
var result = Globalize.dateFormatter({time: "medium"})(new Date(2017, 3, 15, 12, 31, 45));
811
// Note, the reason for the loose match below is due to ignore the local time zone differences.
912
expect(result).to.have.string("31:45");
1013
});
1114

12-
it("should include formatDate", function() {
15+
it("should include support for dateToPartsFormatter", function() {
16+
var result = Globalize.dateToPartsFormatter({time: "long"})(new Date(2017, 3, 15, 10, 17, 37));
17+
expect(result).to.include({type: "second", value: "37"});
18+
});
19+
20+
it("should include support for formatDate", function() {
1321
var result = Globalize.formatDate(new Date(2017, 3, 15), {date: "medium"});
1422
// Note, the reason for the loose match below is due to ignore the local time zone differences.
1523
expect(result).to.have.string("Apr");
1624
expect(result).to.have.string("2017");
1725
});
1826

19-
it("should include formatDate with timeZone support", function() {
27+
it("should include support for formatDate with timeZone support", function() {
2028
var result = Globalize.formatDate(new Date("2017-04-15T12:00:00Z"), {datetime: "full", timeZone: "America/Sao_Paulo"});
2129
expect(result).to.equal("Saturday, April 15, 2017 at 9:00:00 AM Brasilia Standard Time");
2230
});
2331

24-
it("should include formatDateToParts", function() {
25-
var result = Globalize.formatDateToParts(new Date(2017, 3, 15), {datetime: "medium"});
26-
expect(result).to.include({type: "month", value: "Apr"});
32+
it("should include support for formatDateToParts", function() {
33+
var result = Globalize.formatDateToParts(new Date(2017, 3, 15), {date: "long"});
34+
expect(result).to.include({type: "month", value: "April"});
2735
expect(result).to.include({type: "year", value: "2017"});
2836
});
2937

30-
it("should include formatNumber", function() {
38+
it("should include support for dateParser", function() {
39+
var result = Globalize.dateParser({skeleton: "MMMd", timeZone: "America/New_York"})("Jan 1");
40+
expect(result.getMonth()).to.equal(0);
41+
expect(result.getDate()).to.equal(1);
42+
});
43+
44+
it("should include support for parseDate", function() {
45+
var result = Globalize.parseDate("1/2/1982");
46+
expect(result.getFullYear()).to.equal(1982);
47+
expect(result.getMonth()).to.equal(0);
48+
expect(result.getDate()).to.equal(2);
49+
});
50+
51+
it("should include support for parseDate with timeZone support", function() {
52+
var result = Globalize.parseDate("January 1, 2000 at 12:00:00 AM EST", {datetime: "long", timeZone: "America/New_York"});
53+
expect(result).to.deep.equal(new Date("2000-01-01T05:00:00Z"));
54+
});
55+
56+
/**
57+
* Number
58+
*/
59+
it("should include support for numberFormatter", function() {
60+
var result = Globalize.numberFormatter({minimumFractionDigits: 0, maximumFractionDigits: 10})(Math.PI);
61+
expect(result).to.equal("3.1415926536");
62+
});
63+
64+
it("should include support for formatNumber", function() {
3165
var result = Globalize.formatNumber(Math.PI);
3266
expect(result).to.equal("3.142");
3367
});
3468

35-
it("should include formatCurrency", function() {
69+
it("should include support for parseNumber", function() {
70+
var result = Globalize.parseNumber("1,234.56");
71+
expect(result).to.equal(1234.56);
72+
});
73+
74+
/**
75+
* Currency
76+
*/
77+
it("should include support for currencyFormatter", function() {
78+
var result = Globalize.currencyFormatter("EUR")(9.99);
79+
expect(result).to.equal("€9.99");
80+
});
81+
82+
it("should include support for formatCurrency", function() {
3683
var result = Globalize.formatCurrency(69900, "USD");
3784
expect(result).to.equal("$69,900.00");
3885
});
3986

40-
it("should include formatMessage", function() {
87+
/**
88+
* Message
89+
*/
90+
it("should include support for pluralGenerator", function() {
91+
var result = Globalize.pluralGenerator({type: "ordinal"})(2);
92+
expect(result).to.equal("two");
93+
});
94+
95+
it("should include support for plural", function() {
96+
var result = Globalize.plural(5);
97+
expect(result).to.equal("other");
98+
});
99+
100+
it("should include support for messageFormatter", function() {
101+
var result = Globalize.messageFormatter("task")({count: 1000, formattedCount: "1,000"});
102+
expect(result).to.equal("You have 1,000 tasks remaining");
103+
});
104+
105+
it("should include support for formatMessage", function() {
41106
var result = Globalize.formatMessage("like", 0);
42107
expect(result).to.equal("Be the first to like this");
43108
});
44109

45-
it("should include formatRelativeTime", function() {
46-
var result = Globalize.formatRelativeTime(1, "second");
47-
expect(result).to.equal("in 1 second");
48-
});
110+
/**
111+
* Relative Time
112+
*/
49113

50-
it("should include formatUnit", function() {
51-
var result = Globalize.formatUnit(60, "mile/hour", {form: "short"});
52-
expect(result).to.equal("60 mph");
114+
// Use relativeTimeFormatter.
115+
it("should include support for relativeTimeFormatter", function() {
116+
var result = Globalize.relativeTimeFormatter("day")(0);
117+
expect(result).to.equal("today");
53118
});
54119

55-
it("should include parseNumber", function() {
56-
var result = Globalize.parseNumber("1,234.56");
57-
expect(result).to.equal(1234.56);
120+
// Use formatRelativeTime.
121+
it("should include support for formatRelativeTime", function() {
122+
var result = Globalize.formatRelativeTime(1, "second");
123+
expect(result).to.equal("in 1 second");
58124
});
59125

60-
it("should include parseDate", function() {
61-
var result = Globalize.parseDate("1/2/1982");
62-
expect(result.getFullYear()).to.equal(1982);
63-
expect(result.getMonth()).to.equal(0);
64-
expect(result.getDate()).to.equal(2);
126+
/**
127+
* Unit
128+
*/
129+
130+
// Use unitFormatter.
131+
it("should include support for unitFormatter", function() {
132+
var result = Globalize.unitFormatter("kilowatt")(120);
133+
expect(result).to.equal("120 kilowatts");
65134
});
66135

67-
it("should include parseDate with timeZone support", function() {
68-
var result = Globalize.parseDate("January 1, 2000 at 12:00:00 AM EST", {datetime: "long", timeZone: "America/New_York"});
69-
expect(result).to.deep.equal(new Date("2000-01-01T05:00:00Z"));
136+
// Use formatUnit.
137+
it("should include support for formatUnit", function() {
138+
var result = Globalize.formatUnit(60, "mile/hour", {form: "short"});
139+
expect(result).to.equal("60 mph");
70140
});
71141
});

test/unit/fixtures/basic.js

Lines changed: 93 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,110 @@
11
var like;
2-
var Globalize = require( "globalize" );
2+
var Globalize = require("globalize");
33

4-
// Use Globalize to format dates.
5-
var dateFormatter = Globalize.dateFormatter( { time: "medium" } );
6-
console.log( dateFormatter( new Date() ) );
4+
/**
5+
* Date
6+
*/
77

8-
console.log( Globalize.formatDate( new Date(), { date: "medium" } ) );
8+
// Use dateFormatter.
9+
var dateFormatter = Globalize.dateFormatter({time: "medium"});
10+
console.log(dateFormatter(new Date()));
911

10-
// Use Globalize to format dates in specific time zones.
11-
console.log( Globalize.formatDate( new Date(), {
12+
// Use formatDate.
13+
console.log(Globalize.formatDate(new Date(), {date: "medium"}));
14+
15+
// Use formatDate in specific time zones.
16+
console.log(Globalize.formatDate(new Date(), {
1217
datetime: "full",
1318
timeZone: "America/Sao_Paulo"
1419
}));
1520

16-
// Use Globalize to format dates to parts.
17-
console.log( Globalize.formatDateToParts( new Date(), { datetime: "medium" } ) );
21+
// Use dateToPartsFormatter.
22+
var dateToPartsFormatter = Globalize.dateToPartsFormatter({time: "long"});
23+
console.log(dateToPartsFormatter(new Date()));
1824

19-
// Use Globalize to format numbers.
20-
console.log( Globalize.formatNumber( 12345.6789 ) );
25+
// Use formatDateToParts.
26+
console.log(Globalize.formatDateToParts(new Date(), {date: "long"}));
2127

22-
// Use Globalize to format currencies.
23-
console.log( Globalize.formatCurrency( 69900, "USD" ) );
28+
// Use dateParser in specific time zones.
29+
var dateParser = Globalize.dateParser({skeleton: "MMMd", timeZone: "America/New_York"});
30+
console.log(dateParser("Jan 1"));
2431

25-
// Use Globalize to get the plural form of a numeric value.
26-
console.log( Globalize.plural( 12345.6789 ) );
32+
// Use parseDate.
33+
console.log(Globalize.parseDate("1/2/1982"));
2734

28-
// Use Globalize to format a message with plural inflection.
29-
like = Globalize.messageFormatter( "like" );
30-
console.log( like( 0 ) );
31-
console.log( like( 1 ) );
32-
console.log( like( 2 ) );
33-
console.log( like( 3 ) );
35+
// Use parseDate in specific time zones.
36+
console.log(Globalize.parseDate("January 1, 2000 at 12:00:00 AM EST", {
37+
datetime: "long",
38+
timeZone: "America/New_York"
39+
}));
3440

35-
// Use Globalize to format relative time.
36-
console.log( Globalize.formatRelativeTime( -35, "second" ) );
41+
/**
42+
* Number
43+
*/
3744

38-
// Use Globalize to format unit.
39-
console.log( Globalize.formatUnit( 60, "mile/hour", { form: "short" } ) );
45+
// Use numberFormatter.
46+
var numberFormatter = Globalize.numberFormatter({minimumFractionDigits: 0, maximumFractionDigits: 10});
47+
console.log(numberFormatter(Math.PI));
4048

41-
// Use Globalize to parse a number.
42-
console.log( Globalize.parseNumber( "12345.6789" ) );
49+
// Use formatNumber.
50+
console.log(Globalize.formatNumber(12345.6789));
4351

44-
// Use Globalize to parse a date.
45-
console.log( Globalize.parseDate( "1/2/1982" ) );
52+
// Use parseNumber.
53+
console.log(Globalize.parseNumber("12345.6789"));
4654

47-
// Use Globalize to parse a date in specific time zones.
48-
console.log( Globalize.parseDate( "January 1, 2000 at 12:00:00 AM EST", {
49-
datetime: "long",
50-
timeZone: "America/New_York"
51-
}));
55+
/**
56+
* Currency
57+
*/
58+
59+
// Use currencyFormatter.
60+
var currencyFormatter = Globalize.currencyFormatter("EUR");
61+
console.log(currencyFormatter(9.99));
62+
63+
// Use formatCurrency.
64+
console.log(Globalize.formatCurrency(69900, "USD"));
65+
66+
/**
67+
* Message
68+
*/
69+
70+
// Use pluralGenerator.
71+
var pluralGenerator = Globalize.pluralGenerator({type: "ordinal"});
72+
console.log(pluralGenerator(2));
73+
74+
// Use plural.
75+
console.log(Globalize.plural(12345.6789));
76+
77+
// Use messageFormatter.
78+
like = Globalize.messageFormatter("like");
79+
console.log(like(0));
80+
console.log(like(1));
81+
console.log(like(2));
82+
console.log(like(3));
83+
84+
// Use messageFormatter.
85+
like = Globalize.messageFormatter("like");
86+
87+
// Use formatMessage.
88+
console.log(Globalize.formatMessage("task", {count: 1000, formattedCount: "1,000"}));
89+
90+
/**
91+
* Relative Time
92+
*/
93+
94+
// Use relativeTimeFormatter.
95+
var relativeTimeFormatter = Globalize.relativeTimeFormatter("day");
96+
console.log(relativeTimeFormatter(0));
97+
98+
// Use formatRelativeTime.
99+
console.log(Globalize.formatRelativeTime(-35, "second"));
100+
101+
/**
102+
* Unit
103+
*/
104+
105+
// Use unitFormatter.
106+
var unitFormatter = Globalize.unitFormatter("kilowatt");
107+
console.log(unitFormatter(120));
108+
109+
// Use formatUnit.
110+
console.log(Globalize.formatUnit(60, "mile/hour", {form: "short"}));

test/unit/fixtures/en-messages.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
" one {You and someone else liked this}",
88
" other {You and # others liked this}",
99
"}"
10+
],
11+
"task": [
12+
"You have {count, plural,",
13+
" one {one task}",
14+
" other {{formattedCount} tasks}",
15+
"} remaining"
1016
]
1117
}
1218
}

test/unit/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ describe("Extract Formatters & Parsers", function() {
2121
defaultLocale: "en",
2222
messages: {
2323
en: {
24-
like: "Foo"
24+
like: "Foo",
25+
task: "Bar"
2526
}
2627
},
2728
extracts: extract

0 commit comments

Comments
 (0)