UNPKG

@shopgate/engage

Version:
6 lines 1.91 kB
/* eslint-disable extra-rules/no-single-line-objects */import React from'react';import{mount}from'enzyme';import{second$}from'@shopgate/pwa-common/streams/interval';import TimeBoundary from"./index";describe('<TimeBoundary>',function(){var nowMs=new Date().getTime();var subscription;var subscribeSpy;var children;beforeEach(function(){subscription={unsubscribe:jest.fn()};subscribeSpy=jest.spyOn(second$,'subscribe').mockReturnValue(subscription);children=jest.fn();});afterEach(function(){jest.clearAllMocks();});it('should render valid time span',function(){expect.assertions(1);// -30sec < wrapper < +30sec mount(React.createElement(TimeBoundary,{start:new Date(nowMs-30000),end:new Date(nowMs+30000)},children));expect(children).toHaveBeenCalledWith({before:false,between:true,after:false});});it('should not render expired time span',function(){expect.assertions(1);// -60sec < wrapper < -30sec mount(React.createElement(TimeBoundary,{start:new Date(nowMs-60000),end:new Date(nowMs-30000)},children));expect(children).toHaveBeenCalledWith({before:false,between:false,after:true});});it('should hide after time span expired',function(){expect.assertions(4);// -30sec < wrapper < +2sec var wrapper=mount(React.createElement(TimeBoundary,{start:new Date(nowMs-30000),end:new Date(nowMs+2000)},children));var checkBoundaryMethod=wrapper.instance().checkBoundary;expect(subscribeSpy).toHaveBeenCalledTimes(1);expect(subscribeSpy).toHaveBeenCalledWith(checkBoundaryMethod);// Simulate time is over jest.spyOn(Date,'now').mockReturnValue(new Date(nowMs+3000));// simulate rxjs emitting checkBoundaryMethod();wrapper.update();expect(subscription.unsubscribe).toHaveBeenCalledTimes(1);expect(children.mock.calls).toEqual([[{after:false,before:false,between:false}],[{after:false,before:false,between:true}],[{after:true,before:false,between:false}]]);});});/* eslint-enable extra-rules/no-single-line-objects */