|
| 1 | +import { expect, it, describe, vi, beforeEach } from 'vitest'; |
| 2 | +import getIntevalFromNowFormatted, { INTERVAL_DATE_ERR } from '.'; |
| 3 | + |
| 4 | +describe('getRemainingTimeFormatted', () => { |
| 5 | + beforeEach(() => { |
| 6 | + vi.useFakeTimers(); |
| 7 | + }); |
| 8 | + |
| 9 | + it('should return formatted time remaining in the future', () => { |
| 10 | + const futureDate = new Date(Date.now() + 1000 * 60 * 60 * 24); // 1 day in the future |
| 11 | + const locale = 'en-US'; |
| 12 | + const result = getIntevalFromNowFormatted(futureDate, locale); |
| 13 | + expect(result).toBe('in 1 day'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return formatted time remaining in the past', () => { |
| 17 | + const pastDate = new Date(Date.now() - 1000 * 60 * 60 * 24); // 1 day in the past |
| 18 | + const locale = 'en-US'; |
| 19 | + const result = getIntevalFromNowFormatted(pastDate, locale); |
| 20 | + expect(result).toBe('1 day ago'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should return formatted time for 12 hours in the future', () => { |
| 24 | + const futureDate = new Date(Date.now() + 1000 * 60 * 60 * 12); // 12 hours in the future |
| 25 | + const locale = 'en-US'; |
| 26 | + const result = getIntevalFromNowFormatted(futureDate, locale); |
| 27 | + expect(result).toBe('in 12 hours'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return formatted time for 12 minutes in the future', () => { |
| 31 | + const futureDate = new Date(Date.now() + 1000 * 60 * 12); // 12 minutes in the future |
| 32 | + const locale = 'en-US'; |
| 33 | + const result = getIntevalFromNowFormatted(futureDate, locale); |
| 34 | + expect(result).toBe('in 12 minutes'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return formatted time in Portuguese', () => { |
| 38 | + const futureDate = new Date(Date.now() + 1000 * 60 * 60 * 24); // 1 day in the future |
| 39 | + const locale = 'pt-BR'; |
| 40 | + const result = getIntevalFromNowFormatted(futureDate, locale); |
| 41 | + expect(result).toBe('em 1 dia'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return formatted time in Europe Portuguese', () => { |
| 45 | + const futureDate = new Date(Date.now() + 1000 * 60 * 60 * 24); // 1 day in the future |
| 46 | + const locale = 'pt-PT'; |
| 47 | + const result = getIntevalFromNowFormatted(futureDate, locale); |
| 48 | + expect(result).toBe('dentro de 1 dia'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should return formatted time in Spanish', () => { |
| 52 | + const futureDate = new Date(Date.now() + 1000 * 60 * 60 * 24); // 1 day in the future |
| 53 | + const locale = 'es-ES'; |
| 54 | + const result = getIntevalFromNowFormatted(futureDate, locale); |
| 55 | + expect(result).toBe('dentro de 1 día'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should throw for invalid date', () => { |
| 59 | + const invalidDate = new Date('invalid-date'); |
| 60 | + const locale = 'en-US'; |
| 61 | + expect(() => getIntevalFromNowFormatted(invalidDate, locale)).toThrowError( |
| 62 | + INTERVAL_DATE_ERR |
| 63 | + ); |
| 64 | + }); |
| 65 | +}); |
0 commit comments