File size: 1,446 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import getCurrentUserTimeSinceSignup from '../get-current-user-time-since-signup';
describe( 'getCurrentUserTimeSinceSignup()', () => {
beforeAll( () => {
// Time travel.
jest
.useFakeTimers()
// Mock date => 2022-01-01T05:04:12+00:00.
.setSystemTime( new Date( 1641013452000 ) );
} );
afterAll( () => {
// Back to present.
jest.useRealTimers();
} );
test( 'It should return the correct amount of days from the user signup date', () => {
// Fake global state.
const prevState = {
currentUser: {
user: {
date: '2013-02-12T23:34:51+00:00',
},
},
};
const howManyDays = getCurrentUserTimeSinceSignup( prevState );
// It should be 3244 days
expect( howManyDays ).toEqual( 3244 );
} );
test( 'It should round down the date difference to nearest day', () => {
// Fake global state.
const prevState = {
currentUser: {
user: {
date: '2022-01-01T03:04:12+00:00',
},
},
};
const howManyDays = getCurrentUserTimeSinceSignup( prevState );
// It should be 0 (2 hours)
expect( howManyDays ).toEqual( 0 );
} );
test( 'It should round up the date difference to nearest day', () => {
// Fake global state.
const prevState = {
currentUser: {
user: {
date: '2021-12-31T10:04:12+00:00',
},
},
};
const howManyDays = getCurrentUserTimeSinceSignup( prevState );
// It should be 1 (19 hours)
expect( howManyDays ).toEqual( 1 );
} );
} );
|