You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			128 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			TypeScript
		
	
		
		
			
		
	
	
			128 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			TypeScript
		
	
| 
											8 years ago
										 | import { assert } from 'chai'; | ||
| 
											8 years ago
										 | import { shuffle } from 'lodash'; | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 | import { | ||
|  |   groupMessagesByDate, | ||
|  |   Section, | ||
|  | } from '../../../components/conversation/media-gallery/groupMessagesByDate'; | ||
| 
											8 years ago
										 | import { Message } from '../../../components/conversation/media-gallery/types/Message'; | ||
| 
											8 years ago
										 | 
 | ||
|  | const toMessage = (date: Date): Message => ({ | ||
| 
											8 years ago
										 |   id: date.toUTCString(), | ||
| 
											8 years ago
										 |   received_at: date.getTime(), | ||
|  |   attachments: [], | ||
|  | }); | ||
|  | 
 | ||
|  | describe('groupMessagesByDate', () => { | ||
|  |   it('should group messages', () => { | ||
| 
											8 years ago
										 |     const referenceTime = new Date('2018-04-12T18:00Z').getTime(); // Thu
 | ||
| 
											8 years ago
										 |     const input: Array<Message> = shuffle([ | ||
| 
											8 years ago
										 |       // Today
 | ||
| 
											8 years ago
										 |       toMessage(new Date('2018-04-12T12:00Z')), // Thu
 | ||
|  |       toMessage(new Date('2018-04-12T00:01Z')), // Thu
 | ||
| 
											8 years ago
										 |       // This week
 | ||
| 
											8 years ago
										 |       toMessage(new Date('2018-04-11T23:59Z')), // Wed
 | ||
|  |       toMessage(new Date('2018-04-09T00:01Z')), // Mon
 | ||
| 
											8 years ago
										 |       // This month
 | ||
| 
											8 years ago
										 |       toMessage(new Date('2018-04-08T23:59Z')), // Sun
 | ||
| 
											8 years ago
										 |       toMessage(new Date('2018-04-01T00:01Z')), | ||
|  |       // March 2018
 | ||
|  |       toMessage(new Date('2018-03-31T23:59Z')), | ||
|  |       toMessage(new Date('2018-03-01T14:00Z')), | ||
|  |       // February 2011
 | ||
|  |       toMessage(new Date('2011-02-28T23:59Z')), | ||
|  |       toMessage(new Date('2011-02-01T10:00Z')), | ||
| 
											8 years ago
										 |     ]); | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     const expected: Array<Section> = [ | ||
|  |       { | ||
|  |         type: 'today', | ||
|  |         messages: [ | ||
|  |           { | ||
| 
											8 years ago
										 |             id: 'Thu, 12 Apr 2018 12:00:00 GMT', | ||
| 
											8 years ago
										 |             received_at: 1523534400000, | ||
|  |             attachments: [], | ||
|  |           }, | ||
| 
											8 years ago
										 |           { | ||
| 
											8 years ago
										 |             id: 'Thu, 12 Apr 2018 00:01:00 GMT', | ||
| 
											8 years ago
										 |             received_at: 1523491260000, | ||
|  |             attachments: [], | ||
|  |           }, | ||
| 
											8 years ago
										 |         ], | ||
|  |       }, | ||
|  |       { | ||
|  |         type: 'yesterday', | ||
|  |         messages: [ | ||
|  |           { | ||
| 
											8 years ago
										 |             id: 'Wed, 11 Apr 2018 23:59:00 GMT', | ||
| 
											8 years ago
										 |             received_at: 1523491140000, | ||
|  |             attachments: [], | ||
|  |           }, | ||
| 
											8 years ago
										 |         ], | ||
|  |       }, | ||
|  |       { | ||
|  |         type: 'thisWeek', | ||
|  |         messages: [ | ||
|  |           { | ||
| 
											8 years ago
										 |             id: 'Mon, 09 Apr 2018 00:01:00 GMT', | ||
| 
											8 years ago
										 |             received_at: 1523232060000, | ||
|  |             attachments: [], | ||
|  |           }, | ||
| 
											8 years ago
										 |         ], | ||
|  |       }, | ||
|  |       { | ||
|  |         type: 'thisMonth', | ||
|  |         messages: [ | ||
|  |           { | ||
| 
											8 years ago
										 |             id: 'Sun, 08 Apr 2018 23:59:00 GMT', | ||
| 
											8 years ago
										 |             received_at: 1523231940000, | ||
| 
											8 years ago
										 |             attachments: [], | ||
|  |           }, | ||
| 
											8 years ago
										 |           { | ||
| 
											8 years ago
										 |             id: 'Sun, 01 Apr 2018 00:01:00 GMT', | ||
| 
											8 years ago
										 |             received_at: 1522540860000, | ||
|  |             attachments: [], | ||
|  |           }, | ||
| 
											8 years ago
										 |         ], | ||
|  |       }, | ||
|  |       { | ||
|  |         type: 'yearMonth', | ||
|  |         year: 2018, | ||
|  |         month: 2, | ||
|  |         messages: [ | ||
|  |           { | ||
| 
											8 years ago
										 |             id: 'Sat, 31 Mar 2018 23:59:00 GMT', | ||
| 
											8 years ago
										 |             received_at: 1522540740000, | ||
|  |             attachments: [], | ||
|  |           }, | ||
| 
											8 years ago
										 |           { | ||
| 
											8 years ago
										 |             id: 'Thu, 01 Mar 2018 14:00:00 GMT', | ||
| 
											8 years ago
										 |             received_at: 1519912800000, | ||
|  |             attachments: [], | ||
|  |           }, | ||
| 
											8 years ago
										 |         ], | ||
|  |       }, | ||
|  |       { | ||
|  |         type: 'yearMonth', | ||
|  |         year: 2011, | ||
|  |         month: 1, | ||
|  |         messages: [ | ||
|  |           { | ||
| 
											8 years ago
										 |             id: 'Mon, 28 Feb 2011 23:59:00 GMT', | ||
| 
											8 years ago
										 |             received_at: 1298937540000, | ||
|  |             attachments: [], | ||
|  |           }, | ||
| 
											8 years ago
										 |           { | ||
| 
											8 years ago
										 |             id: 'Tue, 01 Feb 2011 10:00:00 GMT', | ||
| 
											8 years ago
										 |             received_at: 1296554400000, | ||
|  |             attachments: [], | ||
|  |           }, | ||
| 
											8 years ago
										 |         ], | ||
|  |       }, | ||
|  |     ]; | ||
| 
											8 years ago
										 | 
 | ||
|  |     const actual = groupMessagesByDate(referenceTime, input); | ||
|  |     assert.deepEqual(actual, expected); | ||
|  |   }); | ||
|  | }); |