import { mount, shallow } from 'enzyme'; import lolex from 'lolex'; import React from 'react'; import { Timestamp } from './Timestamp'; describe('', () => { test('Custom format', () => { const now = new Date(); const rendered = shallow({new Date()}); expect(rendered.text()).toBe(`${now.getMonth() + 1} ${now.getDate()} ${now.getFullYear()}`); }); test('Format relative time', () => { const now = shallow({new Date()}); const lastweek = shallow( {new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)} ); expect(now.text()).toBe('less than a minute ago'); expect(lastweek.text()).toBe('7 days ago'); }); test('render text without wrapper (noWrap prop)', () => { const stamp = shallow( {new Date()} ); expect(stamp.find('span').length).toBe(0); expect(stamp.text()).toBe('less than a minute ago'); }); test('live update relative timestamps', () => { const clock = lolex.install({ now: Date.now() }); const stamp = mount({new Date()}); expect(stamp.text()).toBe('less than a minute ago'); // Fast-forward 1 minute clock.tick('01:00'); expect(stamp.text()).toBe('1 minute ago'); // Fast-forward 2 minutes clock.tick('01:00'); expect(stamp.text()).toBe('2 minutes ago'); // Fast-forward 30 minutes clock.tick('28:00'); expect(stamp.text()).toBe('30 minutes ago'); }); });