@shopgate/pwa-common
Version:
Common library for the Shopgate Connect PWA.
26 lines • 4.59 kB
JavaScript
import React from'react';import{shallow}from'enzyme';import CountdownTimer,{getFormattedTimeString}from"./index";describe('<CountdownTimer>',function(){jest.useFakeTimers();/**
* Creates a new countdown timer element.
* @param {number} remainingDays The remaining days.
* @param {number} remainingHours The remaining hours.
* @param {number} remainingMinutes The remaining minutes.
* @param {number} remainingSeconds The remaining seconds.
* @param {Function} callback The expiration callback.
* @return {JSX}
*/var createTimerElement=function createTimerElement(remainingDays,remainingHours,remainingMinutes,remainingSeconds,callback){var timeout=Math.floor(Date.now()/1000)+remainingDays*86400+remainingHours*3600+remainingMinutes*60+remainingSeconds;var wrapper=shallow(React.createElement(CountdownTimer,{timeout:timeout,onExpire:callback}));var currentTimeOffset=timeout-Math.floor(Date.now()/1000);wrapper.instance().getRemainingTime=function(){currentTimeOffset-=1;return currentTimeOffset;};return wrapper;};/**
* Performs a time format check for a specific remaining time.
* @param {number} remainingDays The remaining days.
* @param {number} remainingHours The remaining hours.
* @param {number} remainingMinutes The remaining minutes.
* @param {number} remainingSeconds The remaining seconds.
*/var performFormatCheck=function performFormatCheck(remainingDays,remainingHours,remainingMinutes,remainingSeconds){jest.clearAllTimers();setInterval.mock.calls=[];var wrapper=createTimerElement(remainingDays,remainingHours,remainingMinutes,remainingSeconds,null);var expectedTimeFormat=getFormattedTimeString(remainingDays,remainingHours,remainingMinutes,remainingSeconds-1// Expect the decremented timeout.
);// We cannot perform a snapshot match here because the timestamp changes for each call.
expect(setInterval.mock.calls.length).toBe(1);jest.runTimersToTime(1000);wrapper.update();var _wrapper$props=wrapper.props(),params=_wrapper$props.params,string=_wrapper$props.string;var renderedTimeFormat={params:params,string:string};expect(renderedTimeFormat).toEqual(expectedTimeFormat);};it('should render the correct time for < 24h',function(){return performFormatCheck(0,0,0,5);});it('should render the correct time for 24h - 48h',function(){return performFormatCheck(1,12,6,5);});it('should render the correct time for > 2d',function(){return performFormatCheck(30,1,2,3);});it('should not render negative durations',function(){jest.clearAllTimers();var wrapper=createTimerElement(-1,-2,-3,-5,null);var expectedTimeFormat=getFormattedTimeString(0,0,0,0);jest.runTimersToTime(1000);var _wrapper$props2=wrapper.props(),params=_wrapper$props2.params,string=_wrapper$props2.string;var renderedTimeFormat={params:params,string:string};expect(renderedTimeFormat).toEqual(expectedTimeFormat);});it('should stop at 00:00:00 when the timer expires',function(){var wrapper=createTimerElement(0,0,0,1,null);var expectedTimeFormat=getFormattedTimeString(0,0,0,0);var renderedTimeFormat;// Run down to 00:00:00.
jest.runTimersToTime(1000);wrapper.update();var _wrapper$props3=wrapper.props(),params=_wrapper$props3.params,string=_wrapper$props3.string;renderedTimeFormat={params:params,string:string};expect(renderedTimeFormat).toEqual(expectedTimeFormat);// Advance time a bit further and make sure the timer stays at 00:00:00.
jest.runTimersToTime(1000);var _wrapper$props4=wrapper.props();params=_wrapper$props4.params;string=_wrapper$props4.string;renderedTimeFormat={params:params,string:string};expect(renderedTimeFormat).toEqual(expectedTimeFormat);});it('should invoke the callback when the timer expires',function(){var timesCallbackInvoked=0;/**
* The callback method will just increment a counter when invoked.
*/var callback=function callback(){timesCallbackInvoked+=1;};createTimerElement(0,0,0,2,callback);// The timer is not expired yet. Make sure the callback is not invoked.
jest.runTimersToTime(1000);expect(timesCallbackInvoked).toBe(0);// The timer should expire by now.
jest.runTimersToTime(1000);expect(timesCallbackInvoked).toBe(1);// Run it again and make sure it won't be called twice.
jest.runTimersToTime(1000);expect(timesCallbackInvoked).toBe(1);});it('should not invoke the callback when the timeout is already expired.',function(){var timesCallbackInvoked=0;/**
* The callback method will just increment a counter when invoked.
*/var callback=function callback(){timesCallbackInvoked+=1;};createTimerElement(0,0,0,0,callback);jest.runTimersToTime(1000);expect(timesCallbackInvoked).toBe(0);});});