UNPKG

@seedalpha/moment

Version:

Extracted version of Momentjs

141 lines (126 loc) 5.32 kB
var assert = require('assert'); var extend = require('@seedalpha/extend'); var SeedMoment = require('./index'); var now = new Date(); var test = {} test.a1 = new Date().toISOString(); test.a2 = new Date(Date.now() - 30 * 1000).toISOString(); test.a3 = new Date(Date.now() - 60 * 1000).toISOString(); test.a4 = new Date(Date.now() - 30 * 60 * 1000).toISOString(); test.a5 = new Date(Date.now() - 60 * 60 * 1000).toISOString(); test.a6 = new Date(Date.now() - 20 * 60 * 60 * 1000).toISOString(); test.b1 = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(); test.b2 = new Date(2015,2,11).toISOString(); // March 11 2015 test.c1 = { _date: new Date()}; test.c2 = { _date: new Date(test.a1)}; test.c3 = { _date: new Date(test.a2)}; describe('Extend module integration', function() { it('should integrate extend module', function() { assert.deepEqual(extend({ a: 1, b: 2}, { a: 2, c: 3 }), { a: 2, b: 2, c: 3 }); }); }); describe('SeedMoment', function() { before(function() { this.m = new SeedMoment(); }); it('should bind the time of initialisation to moment obj', function() { assert(this.m._date instanceof Date) }); it('should take in a valid ISOString and convert it to moment date', function() { this.m = new SeedMoment(test.a1); assert(this.m._date instanceof Date); }); describe('getRelativeTime Method', function() { it('should return a string message when _d is valid date from ISOString', function() { this.m = new SeedMoment(test.a1); assert(typeof this.m.getRelativeTime() === 'string'); }); it('should return "just now"', function() { this.m = new SeedMoment(test.a1); assert(this.m.getRelativeTime() === 'just now'); }); it('should return "30 seconds ago"', function() { this.m = new SeedMoment(test.a2); assert(this.m.getRelativeTime() === '30 seconds ago'); }); it('should return "a minute ago"', function() { this.m = new SeedMoment(test.a3); assert(this.m.getRelativeTime() === 'a minute ago'); }); it('should return "30 minutes ago"', function() { this.m = new SeedMoment(test.a4); assert(this.m.getRelativeTime() === '30 minutes ago'); }); it('should return "an hour ago"', function() { this.m = new SeedMoment(test.a5); assert(this.m.getRelativeTime() === 'an hour ago'); }); it('should return "20 hours ago"', function() { this.m = new SeedMoment(test.a6); assert(this.m.getRelativeTime() === '20 hours ago'); }); it('should return "a day ago"', function() { this.m = new SeedMoment(test.b1); assert(this.m.getRelativeTime() === 'a day ago'); }); it('should return a "Locale Date String"', function() { this.m = new SeedMoment(test.b2); assert(this.m.getRelativeTime() === "Wed, Mar 11, 2015"); }); }); describe('Chain relativeTime to New SeedMoment initialisation', function() { it('should return "just now" from init', function() { this.m = new SeedMoment(test.a1).getRelativeTime(); assert(this.m === 'just now'); }); it('should return "30 seconds ago"', function() { this.m = new SeedMoment(test.a2).getRelativeTime(); assert(this.m === '30 seconds ago'); }); it('should return "a minute ago"', function() { this.m = new SeedMoment(test.a3).getRelativeTime(); assert(this.m === 'a minute ago'); }); it('should return "30 minutes ago"', function() { this.m = new SeedMoment(test.a4).getRelativeTime(); assert(this.m === '30 minutes ago'); }); it('should return "an hour ago"', function() { this.m = new SeedMoment(test.a5).getRelativeTime(); assert(this.m === 'an hour ago'); }); it('should return "20 hours ago"', function() { this.m = new SeedMoment(test.a6).getRelativeTime(); assert(this.m === '20 hours ago'); }); it('should return "a day ago"', function() { this.m = new SeedMoment(test.b1).getRelativeTime(); assert(this.m === 'a day ago'); }); it('should return a "Locale Date String"', function() { this.m = new SeedMoment(test.b2).getRelativeTime(); assert(this.m === "Wed, Mar 11, 2015"); }); }); describe('Init SeedMoment with Object Options Params', function() { describe('Check if test objects are objects', function() { assert(typeof test.c1 === 'object'); assert(typeof test.c2 === 'object'); assert(typeof test.c3 === 'object'); }); describe('should accept object from init with valid date in _d', function() { it('should return "just now" from getRelativeTime Fn from _d: new Date()', function() { this.m = new SeedMoment(test.c1); assert(this.m.getRelativeTime() === 'just now'); }); it('should return "just now" from getRelativeTime Fn from _d: new Date(ISOString)', function() { this.m = new SeedMoment(test.c2); assert(this.m.getRelativeTime() === 'just now'); }); it('should return "30 seconds ago" from getRelativeTime Fn from _d: new Date(test.c3) - \n test.c3 = ISOString', function() { this.m = new SeedMoment(test.c3); assert(this.m.getRelativeTime() === '30 seconds ago'); }); }); }); });