Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #93 from kutschenator/ck/tests
Browse files Browse the repository at this point in the history
Adding tests for php format
  • Loading branch information
pestevez authored May 18, 2020
2 parents ee16407 + b5c9445 commit b7f7474
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 1 deletion.
240 changes: 240 additions & 0 deletions src/js/utils/__tests__/phpFormatMoment.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/**
* Copyright 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* This tests all the cases listed here: http://www.php.net/manual/en/function.date.php
*/

import moment from '../phpFormatMoment';

function formatTest(moment, phpFormat, expected, description) {
it(description, () => {
const res = moment.formatPHP(phpFormat);
expect(res).toEqual(expected);
});
}

describe('phpFormatMoment', () => {
describe('given a valid utc moment', () => {
const utcTime = moment.utc('2016-01-03T13:01:03.153');

// Day
formatTest(
utcTime,
'd',
'03',
'outputs the right Day of the month, 2 digits with leading zeros'
);
formatTest(
utcTime,
'D',
'Sun',
'outputs the right textual representation of a day, three letters'
);
formatTest(
utcTime,
'j',
'3',
'outputs the right day of the month without leading zeros'
);
formatTest(
utcTime,
'l',
'Sunday',
'outputs the right full textual representation of the day of the week'
);
formatTest(
utcTime,
'N',
'7',
'outputs the right ISO-8601 numeric representation of the day of the week (1-7)'
);
formatTest(
utcTime,
'S',
'rd',
'outputs the right English ordinal suffix for the day of the month, 2 characters'
);
formatTest(
utcTime,
'w',
'0',
'outputs the right numeric representation of the day of the week (0-6)'
);
formatTest(
utcTime,
'z',
'2',
'outputs the right day of the year (starting from 0)'
);

// Week
formatTest(
utcTime,
'W',
'53',
'outputs the right ISO-8601 week number of year, weeks starting on Monday'
);

// Month
formatTest(
utcTime,
'F',
'January',
'outputs the right full textual representation of a month, such as January or March'
);
formatTest(
utcTime,
'm',
'01',
'outputs the right numeric representation of a month, with leading zeros'
);
formatTest(
utcTime,
'M',
'Jan',
'outputs the right short textual representation of a month, three letters'
);
formatTest(
utcTime,
'n',
'1',
'outputs the right numeric representation of a month, without leading zeros'
);
formatTest(
utcTime,
't',
'31',
'outputs the right number of days in the given month'
);

// Year
formatTest(utcTime, 'L', '1', 'outputs the info whether its a leap year');
formatTest(
utcTime,
'o',
'2015',
'outputs the right ISO-8601 week-numbering year'
);
formatTest(
utcTime,
'Y',
'2016',
'outputs the right full numeric representation of a year, 4 digits'
);
formatTest(
utcTime,
'y',
'16',
'outputs the right two digit representation of a year'
);

// Time
formatTest(
utcTime,
'a',
'pm',
'outputs the right lowercase Ante meridiem and Post meridiem'
);
formatTest(
utcTime,
'A',
'PM',
'outputs the right uppercase Ante meridiem and Post meridiem'
);
formatTest(
utcTime,
'B',
'584',
'outputs the right Swatch Internet time ¯\\_(ツ)_/¯'
);
formatTest(
utcTime,
'g',
'1',
'outputs the right 12-hour format of an hour without leading zeros'
);
formatTest(
utcTime,
'G',
'13',
'outputs the right 24-hour format of an hour without leading zeros'
);
formatTest(
utcTime,
'h',
'01',
'outputs the right 12-hour format of an hour with leading zeros'
);
formatTest(
utcTime,
'H',
'13',
'outputs the right 24-hour format of an hour with leading zeros'
);
formatTest(
utcTime,
'i',
'01',
'outputs the right minutes with leading zeros'
);
formatTest(
utcTime,
's',
'03',
'outputs the right seconds, with leading zeros'
);
formatTest(utcTime, 'v', '153', 'outputs the right number of milliseconds');

// Timezone
formatTest(utcTime, 'e', 'e', 'outputs the right timezone identifier'); // Moment does not have this
formatTest(
utcTime,
'I',
'0',
'outputs whether or not the date is in daylight saving time '
);
formatTest(
utcTime,
'O',
'+0000',
'outputs the difference to Greenwich time (GMT) in hours '
);
formatTest(
utcTime,
'P',
'+00:00',
'outputs the difference to Greenwich time (GMT) with colon between hours and minutes '
);
formatTest(utcTime, 'T', 'T', 'outputs the right timezone abbreviation');
formatTest(
utcTime,
'Z',
'0',
'outputs the right timezone offset in seconds.'
);

// Full Date/Time
formatTest(
utcTime,
'c',
'2016-01-03T13:01:03+00:00',
'outputs the right ISO 8601 date.'
);
formatTest(
utcTime,
'r',
'Sun, 03 Jan 2016 13:01:03 +0000',
'outputs the right RFC 2822 formatted date.'
);
formatTest(
utcTime,
'U',
'1451826063',
'outputs the right seconds since the Unix Epoch .'
);
});
});
3 changes: 2 additions & 1 deletion src/js/utils/phpFormatMoment.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const formatMap = {
i: 'mm',
s: 'ss',
u: '[u]', // not sure if moment has this
v: 'SSS',
e: '[e]', // moment does not have this
I: function() {
return this.isDST() ? 1 : 0;
Expand All @@ -76,7 +77,7 @@ const formatMap = {
r: 'ddd, DD MMM YYYY HH:mm:ss ZZ',
U: 'X',
};
const formatEx = /[dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU]/g;
const formatEx = /[dDjlNSwzWFmMntLoYyaABgGhHisuveIOPTZcrU]/g;

moment.fn.formatPHP = function(format) {
var that = this;
Expand Down

0 comments on commit b7f7474

Please sign in to comment.