← Back
Editing: sagas.test.js
/** * External dependencies */ import moment from 'moment'; import { takeEvery, put, all, select, call, take, fork } from 'redux-saga/effects'; import { cloneableGenerator } from 'redux-saga/utils'; /** * WordPress dependencies */ import { select as wpSelect, dispatch as wpDispatch } from '@wordpress/data'; import { applyFilters, doAction } from '@wordpress/hooks'; /** * Internal Dependencies */ import * as constants from '../constants'; import * as types from '../types'; import * as actions from '../actions'; import watchers, * as sagas from '../sagas'; import * as selectors from '../selectors'; import { DEFAULT_STATE as TICKET_HEADER_IMAGE_DEFAULT_STATE, } from '../reducers/header-image'; import * as rsvpActions from '@moderntribe/tickets/data/blocks/rsvp-shared/actions'; import { DEFAULT_STATE as RSVP_HEADER_IMAGE_DEFAULT_STATE, } from '@moderntribe/tickets/data/blocks/rsvp/reducers/header-image'; import { MOVE_TICKET_SUCCESS } from '@moderntribe/tickets/data/shared/move/types'; import * as moveSelectors from '@moderntribe/tickets/data/shared/move/selectors'; import * as utils from '@moderntribe/tickets/data/utils'; import { api, moment as momentUtil, time as timeUtil, globals, } from '@moderntribe/common/utils'; const { wpREST } = api; const datePickerFormat = globals.tecDateSettings().datepickerFormat; import { plugins } from '@moderntribe/common/data'; import { isTribeEventPostType, createWPEditorSavingChannel, createWPEditorNotSavingChannel, hasPostTypeChannel, createDates, } from '@moderntribe/tickets/data/shared/sagas'; jest.mock( '@moderntribe/common/utils/moment', () => ( { toMoment: ( date ) => date, toDatabaseDate: ( date ) => date, toDate: ( date ) => date, toDatabaseTime: ( date ) => date, toTime: ( date ) => date, } ) ); const { INDEPENDENT, SHARED, TICKET_TYPES, PROVIDER_CLASS_TO_PROVIDER_MAPPING, WOO_CLASS, } = constants; jest.mock( '@wordpress/blocks', () => ( { createBlock: ( name, attributes ) => ( { name, attributes } ), } ) ); jest.mock( '@wordpress/data', () => { return { select: ( key ) => { if ( key === 'core/block-editor' ) { return { getBlockCount: () => {}, getBlockIndex: () => 0, getBlockRootClientId: () => 88, getBlocks: () => {}, getCurrentPostId: () => 10, getCurrentPostAttribute: () => {}, getCurrentPostType: () => 'tribe_events', getEditedPostAttribute: ( attr ) => { if ( attr === 'date' ) { return '2018-11-09T19:48:42'; } }, }; } if ( key === 'core/editor' ) { return { getCurrentPost: () => { return { id: 10, type: 'tec_tickets', }; }, getBlockCount: () => {}, getBlockIndex: () => 0, getBlockRootClientId: () => 88, getBlocks: () => {}, getCurrentPostId: () => 10, getCurrentPostAttribute: () => {}, getCurrentPostType: () => 'tribe_events', getEditedPostAttribute: ( attr ) => { if ( attr === 'date' ) { return '2018-11-09T19:48:42'; } }, } } if ( key === 'core' ) { return { getPostType: () => ( { rest_base: 'tribe_events', } ), }; } }, dispatch: () => ( { editPost: () => {}, insertBlocks: () => {}, removeBlocks: () => {}, clearSelectedBlock: () => {}, } ), }; } ); describe( 'Ticket Block sagas', () => { describe( 'watchers', () => { it( 'should watch actions', () => { const gen = watchers(); expect( gen.next().value ).toEqual( takeEvery( [ types.SET_TICKETS_INITIAL_STATE, types.RESET_TICKETS_BLOCK, types.SET_TICKET_INITIAL_STATE, types.FETCH_TICKET, types.CREATE_NEW_TICKET, types.UPDATE_TICKET, types.DELETE_TICKET, types.FETCH_TICKETS_HEADER_IMAGE, types.UPDATE_TICKETS_HEADER_IMAGE, types.DELETE_TICKETS_HEADER_IMAGE, types.SET_TICKET_DETAILS, types.SET_TICKET_TEMP_DETAILS, types.HANDLE_TICKET_START_DATE, types.HANDLE_TICKET_END_DATE, types.HANDLE_TICKET_START_TIME, types.HANDLE_TICKET_END_TIME, types.HANDLE_TICKET_SALE_START_DATE, types.HANDLE_TICKET_SALE_END_DATE, MOVE_TICKET_SUCCESS, types.UPDATE_UNEDITABLE_TICKETS, ], sagas.handler ), ); expect( gen.next().value ).toEqual( fork( sagas.handleEventStartDateChanges ), ); expect( gen.next().done ).toEqual( true ); } ); } ); describe( 'handler', () => { let action; beforeEach( () => { action = { type: null }; } ); it( 'should set tickets initial state', () => { action.type = types.SET_TICKETS_INITIAL_STATE; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.setTicketsInitialState, action ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should reset tickets block', () => { action.type = types.RESET_TICKETS_BLOCK; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.resetTicketsBlock ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should set ticket initial state', () => { action.type = types.SET_TICKET_INITIAL_STATE; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.setTicketInitialState, action ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should fetch ticket', () => { action.type = types.FETCH_TICKET; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.fetchTicket, action ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should create new ticket', () => { action.type = types.CREATE_NEW_TICKET; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.createNewTicket, action ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should update ticket', () => { action.type = types.UPDATE_TICKET; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.updateTicket, action ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should delete ticket', () => { action.type = types.DELETE_TICKET; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.deleteTicket, action ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should fetch tickets header image', () => { action.type = types.FETCH_TICKETS_HEADER_IMAGE; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.fetchTicketsHeaderImage, action ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should update tickets header image', () => { action.type = types.UPDATE_TICKETS_HEADER_IMAGE; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.updateTicketsHeaderImage, action ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should delete tickets header image', () => { action.type = types.DELETE_TICKETS_HEADER_IMAGE; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.deleteTicketsHeaderImage ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should set ticket details', () => { action.type = types.SET_TICKET_DETAILS; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.setTicketDetails, action ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should set ticket temp details', () => { action.type = types.SET_TICKET_TEMP_DETAILS; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.setTicketTempDetails, action ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should handle ticket start date', () => { action.type = types.HANDLE_TICKET_START_DATE; action.payload = { clientId: 'tribe' }; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.handleTicketStartDate, action ), ); expect( gen.next().value ).toEqual( call( sagas.handleTicketDurationError, action.payload.clientId ), ); expect( gen.next().value ).toEqual( put( actions.setTicketHasChanges( action.payload.clientId, true ) ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should handle ticket end date', () => { action.type = types.HANDLE_TICKET_END_DATE; action.payload = { clientId: 'tribe' }; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.handleTicketEndDate, action ), ); expect( gen.next().value ).toEqual( call( sagas.handleTicketDurationError, action.payload.clientId ), ); expect( gen.next().value ).toEqual( put( actions.setTicketHasChanges( action.payload.clientId, true ) ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should handle ticket start time', () => { action.type = types.HANDLE_TICKET_START_TIME; action.payload = { clientId: 'tribe' }; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.handleTicketStartTime, action ), ); expect( gen.next().value ).toEqual( call( sagas.handleTicketStartTimeInput, action ), ); expect( gen.next().value ).toEqual( call( sagas.handleTicketDurationError, action.payload.clientId ), ); expect( gen.next().value ).toEqual( put( actions.setTicketHasChanges( action.payload.clientId, true ) ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should handle ticket end time', () => { action.type = types.HANDLE_TICKET_END_TIME; action.payload = { clientId: 'tribe' }; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.handleTicketEndTime, action ), ); expect( gen.next().value ).toEqual( call( sagas.handleTicketEndTimeInput, action ), ); expect( gen.next().value ).toEqual( call( sagas.handleTicketDurationError, action.payload.clientId ), ); expect( gen.next().value ).toEqual( put( actions.setTicketHasChanges( action.payload.clientId, true ) ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should handle ticket move', () => { action.type = MOVE_TICKET_SUCCESS; const gen = sagas.handler( action ); expect( gen.next().value ).toEqual( call( sagas.handleTicketMove ), ); expect( gen.next().done ).toEqual( true ); } ); } ); describe( 'createMissingTicketBlocks', () => { it( 'should create missing ticket blocks', () => { const wpDispatchCoreEditor = { insertBlocks: () => {}, }; const wpSelectCoreEditor = { getBlockCount: () => {}, getBlocks: () => [], }; const tickets = [ 'tribe' ]; const gen = sagas.createMissingTicketBlocks( tickets ); expect( gen.next().value ).toEqual( call( wpDispatch, 'core/block-editor' ), ); expect( gen.next( wpDispatchCoreEditor ).value ).toEqual( call( wpSelect, 'core/block-editor' ), ); expect( gen.next( wpSelectCoreEditor ).value ).toMatchSnapshot(); expect( gen.next( [] ).done ).toEqual( true ); } ); const runWithTicketsBlock = ( tickets, innerBlocks ) => { const insertBlock = jest.fn(); const gen = sagas.createMissingTicketBlocks( tickets ); gen.next(); gen.next( { insertBlock, updateBlockListSettings: () => {} } ); gen.next( { getBlockCount: () => innerBlocks.length, getBlocks: () => [] } ); gen.next( [ { clientId: 'tickets-block', innerBlocks } ] ); return insertBlock.mock.calls.map( ( [ block ] ) => block.attributes.ticketId ); }; it( 'should create a ticket block for each ticket when the Tickets block is empty', () => { expect( runWithTicketsBlock( [ 10, 20 ], [] ) ).toEqual( [ 10, 20 ] ); } ); it( 'should not create a ticket block the Tickets block already holds', () => { const innerBlocks = [ { attributes: { ticketId: 10 } } ]; expect( runWithTicketsBlock( [ 10, 20 ], innerBlocks ) ).toEqual( [ 20 ] ); } ); } ); describe( 'setTicketsInitialState', () => { it( 'should set tickets initial state', () => { const HEADER = 13; const SHARED_CAPACITY = '100'; const PROVIDER = 'woo'; const action = { payload: { get: ( key, defaultValue ) => { switch ( key ) { case 'header': return HEADER; case 'sharedCapacity': return SHARED_CAPACITY; case 'provider': return PROVIDER; case 'tickets': return JSON.stringify( [ { id: 10, type: 'tec_tickets' } ] ); default: return defaultValue; } }, }, }; const gen = cloneableGenerator( sagas.setTicketsInitialState )( action ); expect( gen.next().value ).toEqual( { id: 10, type: 'tec_tickets', }, ); const clone1 = gen.clone(); expect( clone1.next().value ).toEqual( select( selectors.getTicketsIdsInBlocks ), ); expect( clone1.next( [] ).value ).toEqual( call( sagas.createMissingTicketBlocks, [ 10 ] ), ); expect( clone1.next().value ).toEqual( all( [ put( actions.setTicketsSharedCapacity( SHARED_CAPACITY ) ), put( actions.setTicketsTempSharedCapacity( SHARED_CAPACITY ) ), ] ), ); expect( clone1.next().value ).toEqual( put( actions.fetchTicketsHeaderImage( HEADER ) ), ); expect( clone1.next().value ).toEqual( put( actions.setTicketsProvider( PROVIDER ) ), ); expect( clone1.next().done ).toEqual( true ); const clone2 = gen.clone(); expect( clone2.next().value ).toEqual( select( selectors.getTicketsIdsInBlocks ), ); expect( clone2.next().value ).toEqual( call( sagas.createMissingTicketBlocks, [ 10 ] ), ); expect( clone2.next().value ).toEqual( all( [ put( actions.setTicketsSharedCapacity( SHARED_CAPACITY ) ), put( actions.setTicketsTempSharedCapacity( SHARED_CAPACITY ) ), ] ), ); expect( clone2.next().value ).toEqual( put( actions.fetchTicketsHeaderImage( HEADER ) ), ); expect( clone2.next().value ).toEqual( put( actions.setTicketsProvider( PROVIDER ) ), ); expect( clone2.next().done ).toEqual( true ); } ); it( 'should set tickets initial state for new event and no provider', () => { const HEADER = 0; const SHARED_CAPACITY = '0'; const PROVIDER = ''; const DEFAULT_PROVIDER = 'woo'; const action = { payload: { get: ( key, defaultValue ) => { switch ( key ) { case 'header': return HEADER; case 'sharedCapacity': return SHARED_CAPACITY; case 'provider': return PROVIDER; case 'tickets': return JSON.stringify( [ { id: 10, type: 'tec_tickets' } ] ); default: return defaultValue; } }, }, }; const gen = cloneableGenerator( sagas.setTicketsInitialState )( action ); expect( gen.next().value ).toEqual( { id: 10, type: 'tec_tickets' }, ); const clone1 = gen.clone(); expect( clone1.next().value ).toEqual( select( selectors.getTicketsIdsInBlocks ), ); expect( clone1.next( [] ).value ).toEqual( call( sagas.createMissingTicketBlocks, [ 10 ] ), ); expect( clone1.next().value ).toEqual( select( selectors.getDefaultTicketProvider ), ); expect( clone1.next( DEFAULT_PROVIDER ).value ).toEqual( put( actions.setTicketsProvider( DEFAULT_PROVIDER ) ), ); expect( clone1.next().done ).toEqual( true ); const clone2 = gen.clone(); expect( clone2.next().value ).toEqual( select( selectors.getTicketsIdsInBlocks ), ); expect( clone2.next().value ).toEqual( call( sagas.createMissingTicketBlocks, [ 10 ] ), ); expect( clone2.next().value ).toEqual( select( selectors.getDefaultTicketProvider ), ); expect( clone2.next( DEFAULT_PROVIDER ).value ).toEqual( put( actions.setTicketsProvider( DEFAULT_PROVIDER ) ), ); expect( clone2.next().done ).toEqual( true ); } ); } ); describe( 'resetTicketsBlock', () => { it( 'should reset tickets block', () => { const gen = sagas.resetTicketsBlock(); expect( gen.next().value ).toEqual( select( selectors.hasCreatedTickets ), ); expect( gen.next( false ).value ).toEqual( all( [ put( actions.removeTicketBlocks() ), put( actions.setTicketsIsSettingsOpen( false ) ), ] ), ); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( {} ).value ).toMatchSnapshot(); expect( gen.next().value ).toEqual( all( [ put( actions.setTicketsSharedCapacity( '' ) ), put( actions.setTicketsTempSharedCapacity( '' ) ), ] ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should not reset tickets block', () => { const gen = sagas.resetTicketsBlock(); expect( gen.next().value ).toEqual( select( selectors.hasCreatedTickets ), ); expect( gen.next( true ).value ).toEqual( all( [ put( actions.removeTicketBlocks() ), put( actions.setTicketsIsSettingsOpen( false ) ), ] ), ); expect( gen.next().done ).toEqual( true ); } ); } ); describe( 'setTicketInitialState', () => { let publishDate, startMoment, startDate, startDateInput, startTime, eventStart, endMoment, endDate, endDateInput, endTime; beforeEach( () => { publishDate = '2018-11-09T19:48:42'; startMoment = momentUtil.toMoment( publishDate ); startDate = momentUtil.toDatabaseDate( startMoment ); startDateInput = momentUtil.toDate( startMoment ); startTime = momentUtil.toDatabaseTime( startMoment ); eventStart = 'November 30, 2018 12:30:00'; endMoment = momentUtil.toMoment( eventStart ); endDate = momentUtil.toDatabaseDate( endMoment ); endDateInput = momentUtil.toDate( endMoment ); endTime = momentUtil.toDatabaseTime( endMoment ); global.window = global.window || {}; window.tec = { events: { app: { main: { data: { blocks: { datetime: { selectors: { getStart: jest.fn(), }, }, }, }, }, }, }, }; } ); afterEach( () => { delete window.tec; } ); it( 'should set ticket initial state', () => { const TICKET_ID = 99; const CLIENT_ID = 'modern-tribe'; const HAS_BEEN_CREATED = true; const action = { payload: { get: ( key ) => { if ( key === 'ticketId' ) { return TICKET_ID; } else if ( key === 'hasBeenCreated' ) { return HAS_BEEN_CREATED; } }, clientId: CLIENT_ID, }, }; const gen = cloneableGenerator( sagas.setTicketInitialState )( action ); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( publishDate ).value ).toEqual( call( momentUtil.toMoment, publishDate ), ); expect( gen.next( startMoment ).value ).toEqual( call( momentUtil.toDatabaseDate, startMoment ), ); expect( gen.next( startDate ).value ).toEqual( call( momentUtil.toDate, startMoment, datePickerFormat ), ); expect( gen.next( startDateInput ).value ).toEqual( call( momentUtil.toDatabaseTime, startMoment ), ); expect( gen.next( startTime ).value ).toEqual( call( momentUtil.toTime, startMoment ), ); expect( gen.next( startTime ).value ).toEqual( all( [ put( actions.setTicketStartDate( action.payload.clientId, startDate ) ), put( actions.setTicketStartDateInput( action.payload.clientId, startDateInput ) ), put( actions.setTicketStartDateMoment( action.payload.clientId, startMoment ) ), put( actions.setTicketStartTime( action.payload.clientId, startTime ) ), put( actions.setTicketStartTimeInput( action.payload.clientId, startTime ) ), put( actions.setTicketTempStartDate( action.payload.clientId, startDate ) ), put( actions.setTicketTempStartDateInput( action.payload.clientId, startDateInput ) ), put( actions.setTicketTempStartDateMoment( action.payload.clientId, startMoment ) ), put( actions.setTicketTempStartTime( action.payload.clientId, startTime ) ), put( actions.setTicketTempStartTimeInput( action.payload.clientId, startTime ) ), put( actions.setTicketHasBeenCreated( action.payload.clientId, HAS_BEEN_CREATED ) ), ] ), ); expect( gen.next().value ).toEqual( call( isTribeEventPostType ), ); /* * Existing tickets skip the end date default; fetchTicket loads * their saved end date. */ expect( gen.next( true ).value ).toEqual( select( plugins.selectors.hasPlugin, plugins.constants.TICKETS_PLUS ), ); expect( gen.next( false ).value ).toEqual( select( selectors.getTicketsSharedCapacity ), ); const clone1 = gen.clone(); const blankSharedCapacity = ''; expect( clone1.next( blankSharedCapacity ).value ).toEqual( all( [ put( actions.setTicketId( CLIENT_ID, TICKET_ID ) ), call( sagas.fetchTicket, { payload: { clientId: CLIENT_ID, ticketId: TICKET_ID } } ), ] ), ); expect( clone1.next().value ).toEqual( call( sagas.handleTicketDurationError, CLIENT_ID ), ); expect( clone1.next().value ).toEqual( fork( sagas.saveTicketWithPostSave, CLIENT_ID ), ); expect( clone1.next().done ).toEqual( true ); const clone2 = gen.clone(); const sharedCapacity = '100'; expect( clone2.next( sharedCapacity ).value ).toEqual( all( [ put( actions.setTicketCapacity( CLIENT_ID, sharedCapacity ) ), put( actions.setTicketTempCapacity( CLIENT_ID, sharedCapacity ) ), ] ), ); expect( clone2.next().value ).toEqual( all( [ put( actions.setTicketId( CLIENT_ID, TICKET_ID ) ), call( sagas.fetchTicket, { payload: { clientId: CLIENT_ID, ticketId: TICKET_ID } } ), ] ), ); expect( clone2.next().value ).toEqual( call( sagas.handleTicketDurationError, CLIENT_ID ), ); expect( clone2.next().value ).toEqual( fork( sagas.saveTicketWithPostSave, CLIENT_ID ), ); expect( clone2.next().done ).toEqual( true ); } ); it( 'should end the sale a day after it starts when the event start is unset', () => { const CLIENT_ID = 'unset-event-start'; const action = { payload: { get: ( key ) => ( key === 'ticketId' ? 0 : undefined ), clientId: CLIENT_ID, }, }; const saleStartMoment = moment( publishDate ); const fallbackMoment = saleStartMoment.clone().add( 1, 'day' ); const gen = sagas.setTicketInitialState( action ); gen.next(); gen.next( publishDate ); gen.next( saleStartMoment ); gen.next( startDate ); gen.next( startDateInput ); gen.next( startTime ); gen.next( startTime ); gen.next(); gen.next( true ); expect( gen.next( '' ).value ).toEqual( call( momentUtil.toMoment, '' ), ); expect( gen.next( moment.invalid() ).value ).toEqual( call( momentUtil.toDatabaseDate, fallbackMoment ), ); } ); it( 'should set tickets initial state for new ticket', () => { const TICKET_ID = 0; const CLIENT_ID = 'modern-tribe'; const HAS_BEEN_CREATED = true; const action = { payload: { get: ( key ) => { if ( key === 'ticketId' ) { return TICKET_ID; } else if ( key === 'hasBeenCreated' ) { return HAS_BEEN_CREATED; } }, clientId: CLIENT_ID, }, }; window.tec.events.app.main.data.blocks.datetime.selectors.getStart = jest.fn(); const gen = cloneableGenerator( sagas.setTicketInitialState )( action ); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( publishDate ).value ).toEqual( call( momentUtil.toMoment, publishDate ), ); expect( gen.next( startMoment ).value ).toEqual( call( momentUtil.toDatabaseDate, startMoment ), ); expect( gen.next( startDate ).value ).toEqual( call( momentUtil.toDate, startMoment, datePickerFormat ), ); expect( gen.next( startDateInput ).value ).toEqual( call( momentUtil.toDatabaseTime, startMoment ), ); expect( gen.next( startTime ).value ).toEqual( call( momentUtil.toTime, startMoment ), ); expect( gen.next( startTime ).value ).toEqual( all( [ put( actions.setTicketStartDate( action.payload.clientId, startDate ) ), put( actions.setTicketStartDateInput( action.payload.clientId, startDateInput ) ), put( actions.setTicketStartDateMoment( action.payload.clientId, startMoment ) ), put( actions.setTicketStartTime( action.payload.clientId, startTime ) ), put( actions.setTicketStartTimeInput( action.payload.clientId, startTime ) ), put( actions.setTicketTempStartDate( action.payload.clientId, startDate ) ), put( actions.setTicketTempStartDateInput( action.payload.clientId, startDateInput ) ), put( actions.setTicketTempStartDateMoment( action.payload.clientId, startMoment ) ), put( actions.setTicketTempStartTime( action.payload.clientId, startTime ) ), put( actions.setTicketTempStartTimeInput( action.payload.clientId, startTime ) ), put( actions.setTicketHasBeenCreated( action.payload.clientId, HAS_BEEN_CREATED ) ), ] ), ); expect( gen.next().value ).toEqual( call( isTribeEventPostType ), ); expect( gen.next( true ).value ).toEqual( select( window.tec.events.app.main.data.blocks.datetime.selectors.getStart ), ); expect( gen.next( eventStart ).value ).toEqual( call( momentUtil.toMoment, eventStart ), ); expect( gen.next( endMoment ).value ).toEqual( call( momentUtil.toDatabaseDate, endMoment ), ); expect( gen.next( endDate ).value ).toEqual( call( momentUtil.toDate, endMoment, datePickerFormat ), ); expect( gen.next( endDateInput ).value ).toEqual( call( momentUtil.toDatabaseTime, endMoment ), ); expect( gen.next( endTime ).value ).toEqual( call( momentUtil.toTime, endMoment ), ); expect( gen.next( endTime ).value ).toEqual( all( [ put( actions.setTicketEndDate( action.payload.clientId, endDate ) ), put( actions.setTicketEndDateInput( action.payload.clientId, endDateInput ) ), put( actions.setTicketEndDateMoment( action.payload.clientId, endMoment ) ), put( actions.setTicketEndTime( action.payload.clientId, endTime ) ), put( actions.setTicketEndTimeInput( action.payload.clientId, endTime ) ), put( actions.setTicketTempEndDate( action.payload.clientId, endDate ) ), put( actions.setTicketTempEndDateInput( action.payload.clientId, endDateInput ) ), put( actions.setTicketTempEndDateMoment( action.payload.clientId, endMoment ) ), put( actions.setTicketTempEndTime( action.payload.clientId, endTime ) ), put( actions.setTicketTempEndTimeInput( action.payload.clientId, endTime ) ), ] ), ); expect( gen.next().value ).toEqual( select( plugins.selectors.hasPlugin, plugins.constants.TICKETS_PLUS ), ); expect( gen.next( false ).value ).toEqual( select( selectors.getTicketsSharedCapacity ), ); const clone1 = gen.clone(); const blankSharedCapacity = ''; expect( clone1.next().value ).toEqual( call( sagas.handleTicketDurationError, CLIENT_ID ), ); expect( clone1.next().value ).toEqual( fork( sagas.saveTicketWithPostSave, CLIENT_ID ), ); expect( clone1.next( blankSharedCapacity ).done ).toEqual( true ); const clone2 = gen.clone(); const sharedCapacity = '100'; expect( clone2.next( sharedCapacity ).value ).toEqual( all( [ put( actions.setTicketCapacity( CLIENT_ID, sharedCapacity ) ), put( actions.setTicketTempCapacity( CLIENT_ID, sharedCapacity ) ), ] ), ); expect( clone2.next().value ).toEqual( call( sagas.handleTicketDurationError, CLIENT_ID ), ); expect( clone2.next().value ).toEqual( fork( sagas.saveTicketWithPostSave, CLIENT_ID ), ); expect( clone2.next().done ).toEqual( true ); } ); it( 'should set capacity type to shared if tickets plus is active', () => { const TICKET_ID = 0; const CLIENT_ID = 'modern-tribe'; const HAS_BEEN_CREATED = true; const action = { payload: { get: ( key ) => { if ( key === 'ticketId' ) { return TICKET_ID; } else if ( key === 'hasBeenCreated' ) { return HAS_BEEN_CREATED; } }, clientId: CLIENT_ID, }, }; window.tec.events.app.main.data.blocks.datetime.selectors.getStart = jest.fn(); const gen = cloneableGenerator( sagas.setTicketInitialState )( action ); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( publishDate ).value ).toEqual( call( momentUtil.toMoment, publishDate ), ); expect( gen.next( startMoment ).value ).toEqual( call( momentUtil.toDatabaseDate, startMoment ), ); expect( gen.next( startDate ).value ).toEqual( call( momentUtil.toDate, startMoment, datePickerFormat ), ); expect( gen.next( startDateInput ).value ).toEqual( call( momentUtil.toDatabaseTime, startMoment ), ); expect( gen.next( startTime ).value ).toEqual( call( momentUtil.toTime, startMoment ), ); expect( gen.next( startTime ).value ).toEqual( all( [ put( actions.setTicketStartDate( action.payload.clientId, startDate ) ), put( actions.setTicketStartDateInput( action.payload.clientId, startDateInput ) ), put( actions.setTicketStartDateMoment( action.payload.clientId, startMoment ) ), put( actions.setTicketStartTime( action.payload.clientId, startTime ) ), put( actions.setTicketStartTimeInput( action.payload.clientId, startTime ) ), put( actions.setTicketTempStartDate( action.payload.clientId, startDate ) ), put( actions.setTicketTempStartDateInput( action.payload.clientId, startDateInput ) ), put( actions.setTicketTempStartDateMoment( action.payload.clientId, startMoment ) ), put( actions.setTicketTempStartTime( action.payload.clientId, startTime ) ), put( actions.setTicketTempStartTimeInput( action.payload.clientId, startTime ) ), put( actions.setTicketHasBeenCreated( action.payload.clientId, HAS_BEEN_CREATED ) ), ] ), ); expect( gen.next().value ).toEqual( call( isTribeEventPostType ), ); expect( gen.next( true ).value ).toEqual( select( window.tec.events.app.main.data.blocks.datetime.selectors.getStart ), ); expect( gen.next( eventStart ).value ).toEqual( call( momentUtil.toMoment, eventStart ), ); expect( gen.next( endMoment ).value ).toEqual( call( momentUtil.toDatabaseDate, endMoment ), ); expect( gen.next( endDate ).value ).toEqual( call( momentUtil.toDate, endMoment, datePickerFormat ), ); expect( gen.next( endDateInput ).value ).toEqual( call( momentUtil.toDatabaseTime, endMoment ), ); expect( gen.next( endTime ).value ).toEqual( call( momentUtil.toTime, endMoment ), ); expect( gen.next( endTime ).value ).toEqual( all( [ put( actions.setTicketEndDate( action.payload.clientId, endDate ) ), put( actions.setTicketEndDateInput( action.payload.clientId, endDateInput ) ), put( actions.setTicketEndDateMoment( action.payload.clientId, endMoment ) ), put( actions.setTicketEndTime( action.payload.clientId, endTime ) ), put( actions.setTicketEndTimeInput( action.payload.clientId, endTime ) ), put( actions.setTicketTempEndDate( action.payload.clientId, endDate ) ), put( actions.setTicketTempEndDateInput( action.payload.clientId, endDateInput ) ), put( actions.setTicketTempEndDateMoment( action.payload.clientId, endMoment ) ), put( actions.setTicketTempEndTime( action.payload.clientId, endTime ) ), put( actions.setTicketTempEndTimeInput( action.payload.clientId, endTime ) ), ] ), ); expect( gen.next().value ).toEqual( select( plugins.selectors.hasPlugin, plugins.constants.TICKETS_PLUS ), ); expect( gen.next( true ).value ).toEqual( all( [ put( actions.setTicketCapacityType( CLIENT_ID, constants.TICKET_TYPES[ constants.SHARED ], ) ), put( actions.setTicketTempCapacityType( CLIENT_ID, constants.TICKET_TYPES[ constants.SHARED ], ) ), ] ), ); expect( gen.next().value ).toEqual( select( selectors.getTicketsSharedCapacity ), ); expect( gen.next().value ).toEqual( call( sagas.handleTicketDurationError, CLIENT_ID ), ); expect( gen.next().value ).toEqual( fork( sagas.saveTicketWithPostSave, CLIENT_ID ), ); expect( gen.next( '' ).done ).toEqual( true ); } ); } ); describe( 'syncTicketSaleEndWithEventStart', () => { beforeEach( () => { global.window = global.window || {}; window.tec = { events: { app: { main: { data: { blocks: { datetime: { selectors: { getStart: jest.fn(), }, }, }, }, }, }, }, }; } ); afterEach( () => { delete window.tec; } ); it( 'should not sync a custom sale end date with the event start', () => { const CLIENT_ID = 'modern-tribe'; const prevStartDate = 'November 20, 2018 12:30:00'; const tempEndMoment = { local: jest.fn(), isSame: jest.fn() }; const endMoment = { local: jest.fn() }; const prevEventStartMoment = { local: jest.fn() }; const gen = cloneableGenerator( sagas.syncTicketSaleEndWithEventStart )( prevStartDate, CLIENT_ID ); expect( gen.next().value ).toEqual( select( selectors.getTicketTempEndDateMoment, { clientId: CLIENT_ID } ), ); expect( gen.next( tempEndMoment ).value ).toEqual( select( selectors.getTicketEndDateMoment, { clientId: CLIENT_ID } ), ); expect( gen.next( endMoment ).value ).toEqual( call( createDates, prevStartDate ), ); expect( gen.next( { moment: prevEventStartMoment } ).value ).toEqual( all( [ call( [ tempEndMoment, 'local' ] ), call( [ endMoment, 'local' ] ), call( [ prevEventStartMoment, 'local' ] ), ] ), ); expect( gen.next().value ).toEqual( call( [ tempEndMoment, 'isSame' ], endMoment, 'minute' ), ); expect( gen.next( true ).value ).toEqual( call( [ tempEndMoment, 'isSame' ], prevEventStartMoment, 'minute' ), ); expect( gen.next( false ).value ).toEqual( call( isTribeEventPostType ) ); expect( gen.next( true ).value ).toEqual( call( applyFilters, 'tec.tickets.blocks.syncSaleEndWithEventStart', true, CLIENT_ID ) ); expect( gen.next( true ).done ).toEqual( true ); } ); it( 'should sync a fallback sale end date with the event start', () => { const CLIENT_ID = 'fallback-sale-end'; const prevStartDate = ''; const eventStart = 'November 25, 2018 12:30:00'; const tempEndMoment = { local: jest.fn(), isSame: jest.fn() }; const endMoment = { local: jest.fn() }; const prevEventStartMoment = { local: jest.fn() }; const publishDate = 'November 10, 2018 12:30:00'; const startDate = '2018-11-10'; const startDateInput = 'November 10, 2018'; const startTime = '12:30:00'; const init = sagas.setTicketInitialState( { payload: { get: ( key ) => ( key === 'ticketId' ? 0 : undefined ), clientId: CLIENT_ID, }, } ); init.next(); init.next( publishDate ); init.next( moment( publishDate ) ); init.next( startDate ); init.next( startDateInput ); init.next( startTime ); init.next( startTime ); init.next(); init.next( true ); init.next( '' ); init.next( moment.invalid() ); const gen = sagas.syncTicketSaleEndWithEventStart( prevStartDate, CLIENT_ID ); gen.next(); gen.next( tempEndMoment ); gen.next( endMoment ); gen.next( { moment: prevEventStartMoment } ); gen.next(); gen.next( true ); // The fallback never matched the invalid previous start, yet it still syncs. expect( gen.next( false ).value ).toEqual( call( isTribeEventPostType ) ); expect( gen.next( true ).value ).toEqual( call( applyFilters, 'tec.tickets.blocks.syncSaleEndWithEventStart', true, CLIENT_ID ) ); expect( gen.next( true ).value ).toEqual( select( window.tec.events.app.main.data.blocks.datetime.selectors.getStart ), ); expect( gen.next( eventStart ).value ).toEqual( call( createDates, eventStart ) ); } ); it( 'should keep the sale end of a ticket the tec.tickets.blocks.syncSaleEndWithEventStart filter excludes', () => { const CLIENT_ID = 'modern-tribe'; const prevStartDate = 'November 20, 2018 12:30:00'; const tempEndMoment = { local: jest.fn(), isSame: jest.fn() }; const endMoment = { local: jest.fn() }; const prevEventStartMoment = { local: jest.fn() }; const gen = cloneableGenerator( sagas.syncTicketSaleEndWithEventStart )( prevStartDate, CLIENT_ID ); gen.next(); gen.next( tempEndMoment ); gen.next( endMoment ); gen.next( { moment: prevEventStartMoment } ); gen.next(); gen.next( true ); expect( gen.next( true ).value ).toEqual( call( isTribeEventPostType ) ); expect( gen.next( true ).value ).toEqual( call( applyFilters, 'tec.tickets.blocks.syncSaleEndWithEventStart', true, CLIENT_ID ) ); expect( gen.next( false ).done ).toEqual( true ); } ); it( 'should sync the sale end date with the event start when it follows the event', () => { const CLIENT_ID = 'modern-tribe'; const prevStartDate = 'November 20, 2018 12:30:00'; const eventStart = 'November 25, 2018 12:30:00'; const tempEndMoment = { local: jest.fn(), isSame: jest.fn() }; const endMoment = { local: jest.fn() }; const prevEventStartMoment = { local: jest.fn() }; const endDateMoment = { local: jest.fn() }; const gen = cloneableGenerator( sagas.syncTicketSaleEndWithEventStart )( prevStartDate, CLIENT_ID ); expect( gen.next().value ).toEqual( select( selectors.getTicketTempEndDateMoment, { clientId: CLIENT_ID } ), ); expect( gen.next( tempEndMoment ).value ).toEqual( select( selectors.getTicketEndDateMoment, { clientId: CLIENT_ID } ), ); expect( gen.next( endMoment ).value ).toEqual( call( createDates, prevStartDate ), ); expect( gen.next( { moment: prevEventStartMoment } ).value ).toEqual( all( [ call( [ tempEndMoment, 'local' ] ), call( [ endMoment, 'local' ] ), call( [ prevEventStartMoment, 'local' ] ), ] ), ); expect( gen.next().value ).toEqual( call( [ tempEndMoment, 'isSame' ], endMoment, 'minute' ), ); expect( gen.next( true ).value ).toEqual( call( [ tempEndMoment, 'isSame' ], prevEventStartMoment, 'minute' ), ); expect( gen.next( true ).value ).toEqual( call( isTribeEventPostType ) ); expect( gen.next( true ).value ).toEqual( call( applyFilters, 'tec.tickets.blocks.syncSaleEndWithEventStart', true, CLIENT_ID ) ); expect( gen.next( true ).value ).toEqual( select( window.tec.events.app.main.data.blocks.datetime.selectors.getStart ), ); expect( gen.next( eventStart ).value ).toEqual( call( createDates, eventStart ), ); expect( gen.next( { moment: endDateMoment, date: '2018-11-25', dateInput: 'November 25, 2018', time: '12:30:00', timeInput: '12:30 pm', } ).value ).toEqual( all( [ put( actions.setTicketTempEndDate( CLIENT_ID, '2018-11-25' ) ), put( actions.setTicketTempEndDateInput( CLIENT_ID, 'November 25, 2018' ) ), put( actions.setTicketTempEndDateMoment( CLIENT_ID, endDateMoment ) ), put( actions.setTicketTempEndTime( CLIENT_ID, '12:30:00' ) ), put( actions.setTicketTempEndTimeInput( CLIENT_ID, '12:30 pm' ) ), put( actions.setTicketEndDate( CLIENT_ID, '2018-11-25' ) ), put( actions.setTicketEndDateInput( CLIENT_ID, 'November 25, 2018' ) ), put( actions.setTicketEndDateMoment( CLIENT_ID, endDateMoment ) ), put( actions.setTicketEndTime( CLIENT_ID, '12:30:00' ) ), put( actions.setTicketEndTimeInput( CLIENT_ID, '12:30 pm' ) ), put( actions.setTicketHasChanges( CLIENT_ID, true ) ), call( sagas.handleTicketDurationError, CLIENT_ID ), ] ), ); expect( gen.next().done ).toEqual( true ); } ); } ); describe( 'toRequestSaleDate', () => { it( 'should send a sale price date the store keeps as Invalid date empty, as on create', () => { expect( sagas.toRequestSaleDate( 'Invalid date' ) ).toBe( '' ); expect( sagas.toRequestSaleDate( '' ) ).toBe( '' ); expect( sagas.toRequestSaleDate( '2040-10-01' ) ).toBe( '2040-10-01' ); } ); } ); describe( 'setBodyDetails', () => { it( 'should set body details', () => { const postId = 10; const rootClientId = 0; const clientId = 'modern-tribe'; const menuOrder = 0; const props = { clientId }; const gen = cloneableGenerator( sagas.setBodyDetails )( clientId ); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( rootClientId ).value ).toEqual( select( selectors.getTicketProvider, props ), ); expect( gen.next().value ).toEqual( select( selectors.getTicketsProvider ), ); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( postId ).value ).toEqual( select( selectors.getTicketTempTitle, props ), ); expect( gen.next().value ).toEqual( select( selectors.getTicketTempDescription, props ), ); expect( gen.next().value ).toEqual( select( selectors.getTicketTempPrice, props ), ); expect( gen.next().value ).toEqual( select( selectors.getTicketTempStartDate, props ), ); expect( gen.next().value ).toEqual( select( selectors.getTicketTempStartTime, props ), ); expect( gen.next().value ).toEqual( select( selectors.getTicketTempEndDate, props ), ); expect( gen.next().value ).toEqual( select( selectors.getTicketTempEndTime, props ), ); expect( gen.next().value ).toEqual( select( selectors.getTicketTempSku, props ), ); expect( gen.next().value ).toEqual( select( selectors.getTicketTempIACSetting, props ), ); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( menuOrder ).value ).toEqual( select( selectors.getTicketTempCapacityType, props ), ); const clone1 = gen.clone(); const sharedCapacityType = TICKET_TYPES[ SHARED ]; expect( clone1.next( sharedCapacityType ).value ).toEqual( select( selectors.getTicketTempCapacity, props ), ); expect( clone1.next().value ).toEqual( select( selectors.getTicketsTempSharedCapacity ), ); expect( clone1.next().value ).toEqual( select( selectors.showSalePrice, props ), ); expect( clone1.next().done ).toEqual( true ); const clone2 = gen.clone(); const independentCapacityType = TICKET_TYPES[ INDEPENDENT ]; expect( clone2.next( independentCapacityType ).value ).toEqual( select( selectors.getTicketTempCapacity, props ), ); expect( clone2.next().value ).toEqual( select( selectors.showSalePrice, props ), ); expect( clone2.next().done ).toEqual( true ); } ); it( 'should send a sale price date the store holds as Invalid date empty', () => { const props = { clientId: 'modern-tribe' }; const answers = new Map( [ [ selectors.getTicketTempCapacityType, TICKET_TYPES[ INDEPENDENT ] ], [ selectors.showSalePrice, true ], [ selectors.getTicketTempSaleStartDate, 'Invalid date' ], [ selectors.getTicketTempSaleEndDate, '2040-10-01' ], ] ); const gen = sagas.setBodyDetails( props.clientId ); let step = gen.next(); while ( ! step.done ) { step = gen.next( answers.get( step.value?.SELECT?.selector ) ); } expect( step.value.get( 'ticket[sale_price][start_date]' ) ).toBe( '' ); expect( step.value.get( 'ticket[sale_price][end_date]' ) ).toBe( '2040-10-01' ); } ); } ); describe( 'fetchTicket', () => { it( 'should fetch ticket', () => { const TICKET_ID = 13; const CLIENT_ID = 'modern-tribe'; const action = { payload: { ticketId: TICKET_ID, clientId: CLIENT_ID, }, }; const gen = cloneableGenerator( sagas.fetchTicket )( action ); expect( gen.next().value ).toEqual( put( actions.setTicketIsLoading( CLIENT_ID, true ) ) ); expect( gen.next().value ).toEqual( call( wpREST, { path: `tickets/${ TICKET_ID }`, namespace: 'tribe/tickets/v1', } ) ); const apiResponse = { response: { ok: true, status: 200, }, data: { provider: 'provider', title: 'title', description: 'description', status: 'draft', cost_details: { values: [ 10 ], currency_symbol: 'R', currency_position: 'suffix', }, sku: '', iac: 'none', available_from: '2018-11-09 19:48:42', available_until: '2018-11-12 19:48:42', capacity_type: 'unlimited', capacity: 0, supports_attendee_information: true, totals: { sold: 0, stock: 0, }, type: 'default', on_sale: true, sale_price_data: { enabled: true, sale_price: '15', start_date: '2018-11-09 19:48:42', end_date: '2018-11-12 19:48:42', }, attendee_information_fields: null, }, }; expect( gen.next( apiResponse ).value ).toEqual( call( sagas.getTicketSaleDates, apiResponse.data ) ); const startMoment = momentUtil.toMoment( apiResponse.data.available_from ); const endMoment = momentUtil.toMoment( apiResponse.data.available_until ); const saleDates = { startDate: momentUtil.toDatabaseDate( startMoment ), startDateInput: momentUtil.toDate( startMoment ), startDateMoment: startMoment, endDate: momentUtil.toDatabaseDate( endMoment ), endDateInput: momentUtil.toDate( endMoment ), endDateMoment: endMoment, startTime: momentUtil.toDatabaseTime( startMoment ), endTime: momentUtil.toDatabaseTime( endMoment ), startTimeInput: momentUtil.toTime( startMoment ), endTimeInput: momentUtil.toTime( endMoment ), }; const { startDate, startDateInput, endDate, endDateInput, startTime, endTime, startTimeInput, endTimeInput } = saleDates; // The sale price dates take the date picker format, as they do after a create or update. expect( gen.next( saleDates ).value ).toEqual( call( sagas.getTicketSalePriceDates, apiResponse.data ) ); const saleStartDateMoment = momentUtil.toMoment( apiResponse.data.sale_price_data.start_date ); const saleEndDateMoment = momentUtil.toMoment( apiResponse.data.sale_price_data.end_date ); const salePriceDates = { saleStartDate: momentUtil.toDatabaseDate( saleStartDateMoment ), saleStartDateInput: momentUtil.toDate( saleStartDateMoment, datePickerFormat ), saleStartDateMoment, saleEndDate: momentUtil.toDatabaseDate( saleEndDateMoment ), saleEndDateInput: momentUtil.toDate( saleEndDateMoment, datePickerFormat ), saleEndDateMoment, }; const { saleStartDate, saleStartDateInput, saleEndDate, saleEndDateInput } = salePriceDates; const details = { title: apiResponse.data.title, description: apiResponse.data.description, price: apiResponse.data.cost_details.values[ 0 ], sku: apiResponse.data.sku, iac: apiResponse.data.iac, startDate, startDateInput, startDateMoment: startMoment, endDate, endDateInput, endDateMoment: endMoment, startTime, endTime, startTimeInput, endTimeInput, capacityType: apiResponse.data.capacity_type, capacity: apiResponse.data.capacity, attendeeInfoFields: apiResponse.data.attendee_information_fields, type: apiResponse.data.type, on_sale: apiResponse.data.on_sale, salePriceChecked: apiResponse.data.sale_price_data.enabled, salePrice: apiResponse.data.sale_price_data.sale_price, saleStartDate, saleStartDateInput, saleStartDateMoment, saleEndDate, saleEndDateInput, saleEndDateMoment, }; expect( gen.next( salePriceDates ).value ).toEqual( all( [ put( actions.setTicketDetails( CLIENT_ID, details ) ), put( actions.setTicketTempDetails( CLIENT_ID, details ) ), put( actions.setTicketSold( CLIENT_ID, apiResponse.data.totals.sold ) ), put( actions.setTicketAvailable( CLIENT_ID, apiResponse.data.totals.stock ) ), put( actions.setTicketCurrencySymbol( CLIENT_ID, apiResponse.data.cost_details.currency_symbol ) ), put( actions.setTicketCurrencyPosition( CLIENT_ID, apiResponse.data.cost_details.currency_position ) ), put( actions.setTicketProvider( CLIENT_ID, apiResponse.data.provider ) ), put( actions.setTicketHasAttendeeInfoFields( CLIENT_ID, apiResponse.data.supports_attendee_information ) ), put( actions.setTicketHasBeenCreated( CLIENT_ID, true ) ), ] ) ); expect( gen.next().value ).toEqual( doAction( 'tec.tickets.blocks.fetchTicket', CLIENT_ID, apiResponse.data, details ) ); expect( gen.next().value ).toEqual( put( actions.setTicketIsLoading( CLIENT_ID, false ) ) ); expect( gen.next().done ).toEqual( true ); } ); it( 'should not fetch ticket if new ticket', () => { const TICKET_ID = 0; const CLIENT_ID = 'modern-tribe'; const action = { payload: { ticketId: TICKET_ID, clientId: CLIENT_ID, }, }; const gen = sagas.fetchTicket( action ); expect( gen.next().done ).toEqual( true ); } ); } ); describe( 'saveTicketWithPostSave', () => { // A ticket with a relative rule is resolved against the event dates the server reads when the update arrives. it( 'should update the ticket once the post save is done', () => { const clientId = 'saved-with-the-post'; const savingChannel = { take: jest.fn(), close: jest.fn() }; const notSavingChannel = { take: jest.fn(), close: jest.fn() }; const gen = sagas.saveTicketWithPostSave( clientId ); expect( gen.next().value ).toEqual( select( selectors.getTicketHasBeenCreated, { clientId } ) ); expect( gen.next( true ).value ).toEqual( call( createWPEditorSavingChannel ) ); expect( gen.next( savingChannel ).value ).toEqual( call( createWPEditorNotSavingChannel ) ); expect( gen.next( notSavingChannel ).value ).toEqual( take( savingChannel ) ); expect( gen.next().value ).toEqual( take( notSavingChannel ) ); expect( gen.next().value ).toEqual( call( applyFilters, 'tec.tickets.blocks.updateTicketWithPostSave', true, clientId ) ); expect( gen.next( true ).value ).toEqual( call( sagas.updateTicket, { payload: { clientId } } ) ); expect( gen.next().value ).toEqual( take( savingChannel ) ); } ); it( 'should not update the ticket when tec.tickets.blocks.updateTicketWithPostSave returns false', () => { const clientId = 'kept-out-of-the-post-save'; const savingChannel = { take: jest.fn(), close: jest.fn() }; const notSavingChannel = { take: jest.fn(), close: jest.fn() }; const gen = sagas.saveTicketWithPostSave( clientId ); gen.next(); gen.next( true ); gen.next( savingChannel ); gen.next( notSavingChannel ); gen.next(); expect( gen.next().value ).toEqual( call( applyFilters, 'tec.tickets.blocks.updateTicketWithPostSave', true, clientId ) ); expect( gen.next( false ).value ).toEqual( select( selectors.getTicketHasChanges, { clientId } ) ); expect( gen.next( false ).value ).toEqual( take( savingChannel ) ); } ); it( 'should warn when the skipped update leaves the ticket changes unsaved', () => { const clientId = 'skipped-with-changes'; const savingChannel = { take: jest.fn(), close: jest.fn() }; const notSavingChannel = { take: jest.fn(), close: jest.fn() }; const gen = sagas.saveTicketWithPostSave( clientId ); gen.next(); gen.next( true ); gen.next( savingChannel ); gen.next( notSavingChannel ); gen.next(); gen.next(); expect( gen.next( false ).value ).toEqual( select( selectors.getTicketHasChanges, { clientId } ) ); expect( gen.next( true ).value ).toEqual( call( sagas.noticeTicketNotUpdated, clientId ) ); expect( gen.next().value ).toEqual( take( savingChannel ) ); } ); } ); describe( 'noticeTicketNotUpdated', () => { it( 'should add one warning notice per ticket', () => { const clientId = 'ticket-not-updated'; const title = 'General admission'; const createWarningNotice = jest.fn(); const gen = sagas.noticeTicketNotUpdated( clientId ); expect( gen.next().value ).toEqual( select( selectors.getTicketTitle, { clientId } ) ); expect( gen.next( title ).value ).toEqual( call( wpDispatch, 'core/notices' ) ); const { fn, args } = gen.next( { createWarningNotice } ).value.CALL; expect( fn ).toBe( createWarningNotice ); expect( args[ 0 ] ).toContain( `"${ title }"` ); expect( args[ 1 ] ).toEqual( { id: `tec-tickets-ticket-not-updated-${ clientId }` } ); expect( gen.next().done ).toEqual( true ); } ); } ); describe( 'noticeTicketNotSaved', () => { const clientId = 'ticket-not-saved'; const title = 'General admission'; const response = { ok: false, status: 400, json: jest.fn() }; /** * Runs the saga from the reading of the response body to the notice it adds. * * @param {Function} answerBody Steps the saga past the reading of the body. * * @return {Array} The arguments the error notice is created with. */ const getNoticeArgs = ( answerBody ) => { const createErrorNotice = jest.fn(); const gen = sagas.noticeTicketNotSaved( clientId, response ); expect( gen.next().value ).toEqual( call( [ response, 'json' ] ) ); expect( answerBody( gen ).value ).toEqual( select( selectors.getTicketTempTitle, { clientId } ) ); expect( gen.next( title ).value ).toEqual( call( wpDispatch, 'core/notices' ) ); const { fn, args } = gen.next( { createErrorNotice } ).value.CALL; expect( fn ).toBe( createErrorNotice ); expect( gen.next().done ).toEqual( true ); return args; }; it( 'should add the reason the server gave as an error notice, one per ticket', () => { const reason = 'Ticket sales cannot end before they start. Please adjust the sales window.'; const [ message, options ] = getNoticeArgs( ( gen ) => gen.next( { code: 'invalid', message: reason } ) ); expect( message ).toContain( `"${ title }"` ); expect( message ).toContain( reason ); expect( options ).toEqual( { id: `tec-tickets-ticket-not-updated-${ clientId }` } ); } ); it( 'should still add the notice when the answer has no JSON body to read', () => { const [ message ] = getNoticeArgs( ( gen ) => gen.throw( new SyntaxError( 'Unexpected end of JSON input' ) ) ); expect( message ).toContain( `"${ title }"` ); } ); } ); describe( 'getTicketSaleDates', () => { /** * Runs a saga that only yields call effects, answering each with what its function returns. * * @param {Generator} gen The saga. * * @return {*} What the saga returns. */ const runCalls = ( gen ) => { let step = gen.next(); while ( ! step.done ) { const { context, fn, args } = step.value.CALL; step = gen.next( fn.apply( context, args ) ); } return step.value; }; it( 'should map the sale dates a ticket was stored with', () => { const dates = runCalls( sagas.getTicketSaleDates( { available_from: '2040-09-10 19:00:00', available_until: '2040-10-19 18:00:00' } ), ); expect( dates ).toMatchObject( { startDate: '2040-09-10', startTime: '19:00:00', endDate: '2040-10-19', endTime: '18:00:00', } ); } ); /* * The moment stays a moment, as the ticket details sent to `tec.tickets.blocks.ticketUpdated` callbacks always * held one, but an invalid one: its "Invalid date" times are left out for the duration check, which cannot read * them. */ it.each( [ [ 'end', { available_from: '2040-09-10 19:00:00', available_until: '' }, 'end' ], [ 'start', { available_from: '', available_until: '2040-10-19 18:00:00' }, 'start' ], ] )( 'should leave empty the %s a ticket was stored without', ( title, ticket, boundary ) => { const dates = runCalls( sagas.getTicketSaleDates( ticket ) ); expect( dates ).toMatchObject( { [ `${ boundary }Date` ]: '', [ `${ boundary }DateInput` ]: '', [ `${ boundary }Time` ]: '', [ `${ boundary }TimeInput` ]: '', } ); expect( moment.isMoment( dates[ `${ boundary }DateMoment` ] ) ).toBe( true ); expect( dates[ `${ boundary }DateMoment` ].isValid() ).toBe( false ); } ); // A ticket can be stored without an end, so its edits, a price change say, must not wait for one. it( 'should not mark a ticket loaded without an end as a duration error', () => { const clientId = 'stored-without-end'; const gen = sagas.handleTicketDurationError( clientId ); gen.next(); gen.next( moment( '2040-09-10 19:00:00' ) ); expect( gen.next( moment.invalid() ).value ).toEqual( put( actions.setTicketHasDurationError( clientId, false ) ) ); expect( gen.next().done ).toBe( true ); } ); it( 'should mark an end cleared in the date picker as a duration error', () => { const clientId = 'cleared-end'; const gen = sagas.handleTicketDurationError( clientId ); gen.next(); gen.next( moment( '2040-09-10 19:00:00' ) ); expect( gen.next( undefined ).value ).toEqual( put( actions.setTicketHasDurationError( clientId, true ) ) ); expect( gen.next().done ).toBe( true ); } ); it( 'should map the dates a ticket sale price was stored with', () => { const dates = runCalls( sagas.getTicketSalePriceDates( { sale_price_data: { start_date: '2040-09-01', end_date: '2040-09-30' } } ), ); expect( dates ).toMatchObject( { saleStartDate: '2040-09-01', saleEndDate: '2040-09-30' } ); } ); } ); /* * A ticket the server stored without an end, the dates parsed from it, and the end the form holds, for the sagas * that put a saved ticket back in the form. */ const ticketStoredWithoutEnd = { id: 123, available_from: '2018-11-09 19:48:42', available_until: '', capacity_details: { available: 100, sold: 0 }, provider_class: WOO_CLASS, sale_price_data: { enabled: false, sale_price: '' }, on_sale: false, }; const parsedSaleDatesWithoutEnd = { startDate: '2018-11-09', startDateInput: 'November 9, 2018', startDateMoment: moment( '2018-11-09 19:48:42' ), endDate: '', endDateInput: '', endDateMoment: moment.invalid(), startTime: '19:48:42', endTime: '', startTimeInput: '7:48 pm', endTimeInput: '', }; const tempEnd = { endDate: '2018-11-12', endDateInput: 'November 12, 2018', endDateMoment: moment( '2018-11-12 18:00:00' ), endTime: '18:00:00', endTimeInput: '6:00 pm', }; const salePriceDatesWithoutDates = { saleStartDate: '', saleStartDateInput: '', saleStartDateMoment: undefined, saleEndDate: '', saleEndDateInput: '', saleEndDateMoment: undefined, }; const tempFields = { title: 'title', description: 'description', price: 10, sku: 'sku', iac: undefined, capacityType: 'unlimited', capacity: 100, }; /** * Steps the saga that reads the sale dates a ticket was saved with, answering its reads with the given parsed * dates and form end. * * @param {Object} ticket The ticket, as the tickets REST API returns it. * @param {string} clientId The ticket's client ID. * @param {Object} parsedDates The dates parsed from the ticket. * @param {Object} formEnd The end the form holds. * * @return {Object} What the saga returns. */ const stepSavedTicketSaleDates = ( ticket, clientId, parsedDates, formEnd ) => { const props = { clientId }; const gen = sagas.getSavedTicketSaleDates( ticket, clientId ); expect( gen.next().value ).toEqual( call( sagas.getTicketSaleDates, ticket ) ); expect( gen.next( parsedDates ).value ).toEqual( all( [ select( selectors.getTicketTempEndDate, props ), select( selectors.getTicketTempEndDateInput, props ), select( selectors.getTicketTempEndDateMoment, props ), select( selectors.getTicketTempEndTime, props ), select( selectors.getTicketTempEndTimeInput, props ), ] ) ); const result = gen.next( [ formEnd.endDate, formEnd.endDateInput, formEnd.endDateMoment, formEnd.endTime, formEnd.endTimeInput, ] ); expect( result.done ).toBe( true ); return result.value; }; /** * Gets the selects a saved ticket's form fields are read with. * * @param {Object} props The props holding the ticket's client ID. * * @return {Object} The all effect of the selects. */ const selectTempFields = ( props ) => all( [ select( selectors.getTicketTempTitle, props ), select( selectors.getTicketTempDescription, props ), select( selectors.getTicketTempPrice, props ), select( selectors.getTicketTempSku, props ), select( selectors.getTicketTempIACSetting, props ), select( selectors.getTicketTempCapacityType, props ), select( selectors.getTicketTempCapacity, props ), ] ); describe( 'createNewTicket', () => { it( 'should create a new ticket', () => { const action = { payload: { clientId: 'tribe', details: { title: 'title', description: 'description', price: 10, sku: 'sku', startDate: '2018-01-01', endDate: '2018-01-02', startTime: '10:00', endTime: '12:00', capacityType: 'unlimited', capacity: 100, }, }, }; const mockFormData = { append: jest.fn(), }; const gen = sagas.createNewTicket( action ); expect( gen.next().value ).toEqual( call( sagas.setBodyDetails, action.payload.clientId ), ); expect( gen.next( mockFormData ).value ).toEqual( put( actions.setTicketIsLoading( action.payload.clientId, true ) ), ); expect( gen.next().value ).toEqual( call( wpREST, { path: 'tickets/', namespace: 'tribe/tickets/v1', initParams: { method: 'POST', body: mockFormData, }, } ), ); const response = { response: { ok: true, }, data: { id: 123, capacity_details: { available: 100, }, provider_class: WOO_CLASS, sale_price_data: { enabled: true, sale_price: '15', }, }, }; expect( gen.next( response ).value ).toEqual( select( selectors.getTicketsSharedCapacity ), ); expect( gen.next( '' ).value ).toEqual( select( selectors.getTicketsTempSharedCapacity ), ); expect( gen.next( 0 ).value ).toEqual( call( sagas.getSavedTicketSaleDates, response.data, action.payload.clientId ) ); const saleDates = { startDate: '2018-01-01', startDateInput: 'January 1, 2018', startDateMoment: undefined, endDate: '2018-01-02', endDateInput: 'January 2, 2018', endDateMoment: undefined, startTime: '10:00:00', endTime: '12:00:00', startTimeInput: '10:00 am', endTimeInput: '12:00 pm', }; expect( gen.next( saleDates ).value ).toEqual( call( sagas.getTicketSalePriceDates, response.data ), ); const salePriceDates = { saleStartDate: '', saleStartDateInput: '', saleStartDateMoment: undefined, saleEndDate: '', saleEndDateInput: '', saleEndDateMoment: undefined, }; expect( gen.next( salePriceDates ).value ).toEqual( all( [ select( selectors.getTicketTempTitle, { clientId: action.payload.clientId } ), select( selectors.getTicketTempDescription, { clientId: action.payload.clientId } ), select( selectors.getTicketTempPrice, { clientId: action.payload.clientId } ), select( selectors.getTicketTempSku, { clientId: action.payload.clientId } ), select( selectors.getTicketTempIACSetting, { clientId: action.payload.clientId } ), select( selectors.getTicketTempCapacityType, { clientId: action.payload.clientId } ), select( selectors.getTicketTempCapacity, { clientId: action.payload.clientId } ), ] ), ); const ticketDetails = { title: 'title', description: 'description', price: 10, sku: 'sku', iac: undefined, ...saleDates, capacityType: 'unlimited', capacity: 100, salePriceChecked: true, salePrice: '15', ...salePriceDates, }; expect( gen.next( [ 'title', 'description', 10, 'sku', undefined, 'unlimited', 100, ] ).value ).toEqual( all( [ put( actions.setTicketDetails( action.payload.clientId, ticketDetails ) ), put( actions.setTicketTempDetails( action.payload.clientId, ticketDetails ) ), put( actions.setTicketId( action.payload.clientId, 123 ) ), put( actions.setTicketHasBeenCreated( action.payload.clientId, true ) ), put( actions.setTicketAvailable( action.payload.clientId, 100 ) ), put( actions.setTicketProvider( action.payload.clientId, PROVIDER_CLASS_TO_PROVIDER_MAPPING[WOO_CLASS] ) ), put( actions.setTicketHasChanges( action.payload.clientId, false ) ), ] ), ); const removeNotice = jest.fn(); expect( gen.next().value ).toEqual( call( wpDispatch, 'core/notices' ) ); expect( gen.next( { removeNotice } ).value ).toEqual( call( removeNotice, `tec-tickets-ticket-not-updated-${ action.payload.clientId }` ) ); expect( gen.next().value ).toEqual( fork( sagas.saveTicketWithPostSave, action.payload.clientId ), ); expect( gen.next().value ).toEqual( put( actions.setTicketIsLoading( action.payload.clientId, false ) ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should put back the end the form holds for a ticket the server stored without one', () => { const clientId = 'tribe'; const props = { clientId }; const gen = sagas.createNewTicket( { payload: { clientId } } ); gen.next(); gen.next( { append: jest.fn() } ); gen.next(); expect( gen.next( { response: { ok: true }, data: ticketStoredWithoutEnd } ).value ).toEqual( select( selectors.getTicketsSharedCapacity ) ); expect( gen.next( '' ).value ).toEqual( select( selectors.getTicketsTempSharedCapacity ) ); expect( gen.next( 0 ).value ).toEqual( call( sagas.getSavedTicketSaleDates, ticketStoredWithoutEnd, clientId ) ); expect( gen.next( stepSavedTicketSaleDates( ticketStoredWithoutEnd, clientId, parsedSaleDatesWithoutEnd, tempEnd ) ).value ).toEqual( call( sagas.getTicketSalePriceDates, ticketStoredWithoutEnd ) ); expect( gen.next( salePriceDatesWithoutDates ).value ).toEqual( selectTempFields( props ) ); const ticketDetails = { ...tempFields, ...parsedSaleDatesWithoutEnd, ...tempEnd, salePriceChecked: ticketStoredWithoutEnd.sale_price_data.enabled, salePrice: ticketStoredWithoutEnd.sale_price_data.sale_price, ...salePriceDatesWithoutDates, }; const puts = gen.next( Object.values( tempFields ) ).value; expect( puts.ALL ).toEqual( expect.arrayContaining( [ put( actions.setTicketDetails( clientId, ticketDetails ) ), put( actions.setTicketTempDetails( clientId, ticketDetails ) ), ] ) ); gen.next(); gen.next( { removeNotice: jest.fn() } ); expect( gen.next().value ).toEqual( fork( sagas.saveTicketWithPostSave, clientId ) ); } ); it( 'should remove the notice a rejected save added once the ticket is created', () => { const clientId = 'created-after-a-rejected-save'; const removeNotice = jest.fn(); const gen = sagas.createNewTicket( { payload: { clientId } } ); gen.next(); gen.next( { append: jest.fn() } ); gen.next(); gen.next( { response: { ok: true }, data: ticketStoredWithoutEnd } ); gen.next( '' ); gen.next( 0 ); gen.next( stepSavedTicketSaleDates( ticketStoredWithoutEnd, clientId, parsedSaleDatesWithoutEnd, tempEnd ) ); gen.next( salePriceDatesWithoutDates ); gen.next( Object.values( tempFields ) ); expect( gen.next().value ).toEqual( call( wpDispatch, 'core/notices' ) ); expect( gen.next( { removeNotice } ).value ).toEqual( call( removeNotice, `tec-tickets-ticket-not-updated-${ clientId }` ) ); } ); it( 'should show why the server rejected the ticket, and keep the form as it is', () => { const clientId = 'rejected-on-create'; const rejected = { ok: false, status: 400 }; const gen = sagas.createNewTicket( { payload: { clientId } } ); gen.next(); gen.next( { append: jest.fn() } ); gen.next(); expect( gen.next( { response: rejected, data: {} } ).value ).toEqual( call( sagas.noticeTicketNotSaved, clientId, rejected ) ); expect( gen.next().value ).toEqual( put( actions.setTicketIsLoading( clientId, false ) ) ); expect( gen.next().done ).toEqual( true ); } ); } ); describe( 'updateTicket', () => { it( 'should update a ticket', () => { const action = { payload: { clientId: 'tribe', details: { title: 'title', description: 'description', price: 10, sku: 'sku', startDate: '2018-01-01', endDate: '2018-01-02', startTime: '10:00', endTime: '12:00', capacityType: 'unlimited', capacity: 100, }, }, }; const mockFormData = { append: jest.fn(), entries: () => [], }; const gen = sagas.updateTicket( action ); expect( gen.next().value ).toEqual( call( sagas.setBodyDetails, action.payload.clientId ), ); expect( gen.next( mockFormData ).value ).toEqual( select( selectors.getTicketId, { clientId: action.payload.clientId } ), ); expect( gen.next( 123 ).value ).toEqual( put( actions.setTicketIsLoading( action.payload.clientId, true ) ), ); expect( gen.next().value ).toEqual( call( wpREST, { path: 'tickets/123', namespace: 'tribe/tickets/v1', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', }, initParams: { method: 'PUT', body: '', }, } ), ); const response = { response: { ok: true, }, data: { id: 123, }, }; expect( gen.next( response ).value ).toEqual( put( actions.setTicketIsLoading( action.payload.clientId, false ) ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should put back the end the form holds for a ticket the server stored without one', () => { const clientId = 'tribe'; const props = { clientId }; const gen = sagas.updateTicket( { payload: { clientId } } ); gen.next(); gen.next( { append: jest.fn(), entries: () => [] } ); gen.next( ticketStoredWithoutEnd.id ); gen.next(); expect( gen.next( { response: { ok: true }, data: ticketStoredWithoutEnd } ).value ).toEqual( call( sagas.getSavedTicketSaleDates, ticketStoredWithoutEnd, clientId ) ); expect( gen.next( stepSavedTicketSaleDates( ticketStoredWithoutEnd, clientId, parsedSaleDatesWithoutEnd, tempEnd ) ).value ).toEqual( call( sagas.getTicketSalePriceDates, ticketStoredWithoutEnd ) ); expect( gen.next( salePriceDatesWithoutDates ).value ).toEqual( selectTempFields( props ) ); const ticketDetails = { ...tempFields, on_sale: ticketStoredWithoutEnd.on_sale, ...parsedSaleDatesWithoutEnd, ...tempEnd, salePriceChecked: ticketStoredWithoutEnd.sale_price_data.enabled, salePrice: ticketStoredWithoutEnd.sale_price_data.sale_price, ...salePriceDatesWithoutDates, }; const puts = gen.next( Object.values( tempFields ) ).value; expect( puts.ALL ).toEqual( expect.arrayContaining( [ put( actions.setTicketDetails( clientId, ticketDetails ) ), put( actions.setTicketTempDetails( clientId, ticketDetails ) ), ] ) ); } ); it( 'should remove the notice a skipped post save update added once the ticket is updated', () => { const clientId = 'updated-after-a-skipped-post-save'; const removeNotice = jest.fn(); const gen = sagas.updateTicket( { payload: { clientId } } ); gen.next(); gen.next( { append: jest.fn(), entries: () => [] } ); gen.next( ticketStoredWithoutEnd.id ); gen.next(); gen.next( { response: { ok: true }, data: ticketStoredWithoutEnd } ); gen.next( stepSavedTicketSaleDates( ticketStoredWithoutEnd, clientId, parsedSaleDatesWithoutEnd, tempEnd ) ); gen.next( salePriceDatesWithoutDates ); gen.next( Object.values( tempFields ) ); expect( gen.next().value ).toEqual( call( wpDispatch, 'core/notices' ) ); expect( gen.next( { removeNotice } ).value ).toEqual( call( removeNotice, `tec-tickets-ticket-not-updated-${ clientId }` ) ); } ); it( 'should show why the server rejected the ticket, and keep the form as it is', () => { const clientId = 'rejected-on-update'; const rejected = { ok: false, status: 400 }; const gen = sagas.updateTicket( { payload: { clientId } } ); gen.next(); gen.next( { append: jest.fn(), entries: () => [] } ); gen.next( 123 ); gen.next(); expect( gen.next( { response: rejected, data: {} } ).value ).toEqual( call( sagas.noticeTicketNotSaved, clientId, rejected ) ); expect( gen.next().value ).toEqual( put( actions.setTicketIsLoading( clientId, false ) ) ); expect( gen.next().done ).toEqual( true ); } ); } ); describe( 'deleteTicket', () => { it( 'should delete ticket', () => { const TICKET_ID = 13; const POST_ID = 10; const CLIENT_ID = 'modern-tribe'; const props = { clientId: CLIENT_ID }; const action = { payload: { clientId: CLIENT_ID, }, }; const gen = cloneableGenerator( sagas.deleteTicket )( action ); expect( gen.next().value ).toEqual( call( [ window, 'confirm' ], 'Are you sure you want to delete this ticket? It cannot be undone.', ) ); expect( gen.next( true ).value ).toEqual( select( selectors.getTicketId, props ), ); expect( gen.next( TICKET_ID ).value ).toEqual( select( selectors.getTicketHasBeenCreated, props ), ); const clone1 = gen.clone(); const hasBeenCreated1 = false; expect( clone1.next( hasBeenCreated1 ).value ).toEqual( put( actions.setTicketIsSelected( CLIENT_ID, false ) ), ); expect( clone1.next().value ).toEqual( put( actions.removeTicketBlock( CLIENT_ID ) ), ); expect( clone1.next().value ).toMatchSnapshot(); expect( clone1.next().value ).toMatchSnapshot(); expect( clone1.next().done ).toEqual( true ); const clone2 = gen.clone(); const hasBeenCreated2 = true; const body = [ `${ encodeURIComponent( 'post_id' ) }=${ encodeURIComponent( POST_ID ) }`, `${ encodeURIComponent( 'remove_ticket_nonce' ) }=${ encodeURIComponent( '' ) }`, ]; expect( clone2.next( hasBeenCreated2 ).value ).toEqual( put( actions.setTicketIsSelected( CLIENT_ID, false ) ), ); expect( clone2.next().value ).toEqual( put( actions.removeTicketBlock( CLIENT_ID ) ), ); expect( clone2.next().value ).toMatchSnapshot(); expect( clone2.next().value ).toMatchSnapshot(); expect( clone2.next().value ).toMatchSnapshot(); expect( clone2.next( POST_ID ).value ).toEqual( call( wpREST, { path: `tickets/${ TICKET_ID }`, namespace: 'tribe/tickets/v1', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', }, initParams: { method: 'DELETE', body: body.join( '&' ), }, } ), ); expect( clone2.next().done ).toEqual( true ); } ); } ); describe( 'getSavedTicketSaleDates', () => { it( 'should fill the end from the form for a ticket the server stored without one', () => { expect( stepSavedTicketSaleDates( ticketStoredWithoutEnd, 'tribe', parsedSaleDatesWithoutEnd, tempEnd ) ).toEqual( { ...parsedSaleDatesWithoutEnd, ...tempEnd } ); } ); it( 'should keep the end of a ticket the server stored with one', () => { const ticket = { ...ticketStoredWithoutEnd, available_until: '2018-11-12 19:48:42' }; const gen = sagas.getSavedTicketSaleDates( ticket, 'tribe' ); gen.next(); const result = gen.next( parsedSaleDatesWithoutEnd ); expect( result.done ).toBe( true ); expect( result.value ).toBe( parsedSaleDatesWithoutEnd ); } ); } ); describe( 'fetchTicketsHeaderImage', () => { it( 'should fetch tickets header image', () => { const action = { payload: { id: 99, }, }; const gen = cloneableGenerator( sagas.fetchTicketsHeaderImage )( action ); expect( gen.next().value ).toEqual( put( actions.setTicketsIsSettingsLoading( true ) ), ); expect( gen.next().value ).toEqual( call( wpREST, { path: `media/${ action.payload.id }` } ), ); const clone1 = gen.clone(); const apiResponseBad = { response: { ok: false, }, data: {}, }; expect( clone1.next( apiResponseBad ).value ).toEqual( put( actions.setTicketsIsSettingsLoading( false ) ), ); expect( clone1.next().done ).toEqual( true ); const clone2 = gen.clone(); const apiResponseGood = { response: { ok: true, }, data: { id: 99, alt_text: 'tribe', media_details: { sizes: { medium: { source_url: '#', }, }, }, }, }; expect( clone2.next( apiResponseGood ).value ).toEqual( put( actions.setTicketsHeaderImage( { id: apiResponseGood.data.id, alt: apiResponseGood.data.alt_text, src: apiResponseGood.data.media_details.sizes.medium.source_url, } ) ), ); expect( clone2.next().value ).toEqual( put( actions.setTicketsIsSettingsLoading( false ) ), ); expect( clone2.next().done ).toEqual( true ); } ); } ); describe( 'updateTicketsHeaderImage', () => { it( 'should update tickets header image', () => { const postId = 10; const action = { payload: { image: { id: 99, alt: 'tribe', sizes: { medium: { url: '#', }, }, }, }, }; const gen = sagas.updateTicketsHeaderImage( action ); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( postId ).value ).toEqual( put( actions.setTicketsIsSettingsLoading( true ) ), ); expect( gen.next().value ).toEqual( put( rsvpActions.setRSVPIsSettingsLoading( true ) ), ); expect( gen.next().value ).toEqual( call( wpREST, { path: `tribe_events/${ postId }`, headers: { 'Content-Type': 'application/json', }, initParams: { method: 'PUT', body: JSON.stringify( { meta: { [ utils.KEY_TICKET_HEADER ]: `${ action.payload.image.id }`, }, } ), }, } ), ); const apiResponse = { response: { ok: true, }, }; const headerImage = { id: action.payload.image.id, alt: action.payload.image.alt, src: action.payload.image.sizes.medium.url, }; expect( gen.next( apiResponse ).value ).toEqual( put( actions.setTicketsHeaderImage( headerImage ) ), ); expect( gen.next().value ).toEqual( put( rsvpActions.setRSVPHeaderImage( headerImage ) ), ); expect( gen.next().value ).toEqual( put( actions.setTicketsIsSettingsLoading( false ) ), ); expect( gen.next().value ).toEqual( put( rsvpActions.setRSVPIsSettingsLoading( false ) ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should not update tickets header image', () => { const postId = 10; const action = { payload: { image: { id: 99, alt: 'tribe', sizes: { medium: { url: '#', }, }, }, }, }; const gen = sagas.updateTicketsHeaderImage( action ); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( postId ).value ).toEqual( put( actions.setTicketsIsSettingsLoading( true ) ), ); expect( gen.next().value ).toEqual( put( rsvpActions.setRSVPIsSettingsLoading( true ) ), ); expect( gen.next().value ).toEqual( call( wpREST, { path: `tribe_events/${ postId }`, headers: { 'Content-Type': 'application/json', }, initParams: { method: 'PUT', body: JSON.stringify( { meta: { [ utils.KEY_TICKET_HEADER ]: `${ action.payload.image.id }`, }, } ), }, } ), ); const apiResponse = { response: { ok: false, }, }; expect( gen.next( apiResponse ).value ).toEqual( put( actions.setTicketsIsSettingsLoading( false ) ), ); expect( gen.next().value ).toEqual( put( rsvpActions.setRSVPIsSettingsLoading( false ) ), ); expect( gen.next().done ).toEqual( true ); } ); } ); describe( 'deleteTicketsHeaderImage', () => { it( 'should delete tickets header image', () => { const postId = 10; const gen = sagas.deleteTicketsHeaderImage(); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( postId ).value ).toEqual( put( actions.setTicketsIsSettingsLoading( true ) ), ); expect( gen.next().value ).toEqual( put( rsvpActions.setRSVPIsSettingsLoading( true ) ), ); expect( gen.next().value ).toEqual( call( wpREST, { path: `tribe_events/${ postId }`, headers: { 'Content-Type': 'application/json', }, initParams: { method: 'PUT', body: JSON.stringify( { meta: { [ utils.KEY_TICKET_HEADER ]: null, }, } ), }, } ), ); const apiResponse = { response: { ok: true, }, }; expect( gen.next( apiResponse ).value ).toEqual( put( actions.setTicketsHeaderImage( TICKET_HEADER_IMAGE_DEFAULT_STATE ) ), ); expect( gen.next().value ).toEqual( put( rsvpActions.setRSVPHeaderImage( RSVP_HEADER_IMAGE_DEFAULT_STATE ) ), ); expect( gen.next().value ).toEqual( put( actions.setTicketsIsSettingsLoading( false ) ), ); expect( gen.next().value ).toEqual( put( rsvpActions.setRSVPIsSettingsLoading( false ) ), ); expect( gen.next().done ).toEqual( true ); } ); it( 'should not delete tickets header image', () => { const postId = 10; const gen = sagas.deleteTicketsHeaderImage(); expect( gen.next().value ).toMatchSnapshot(); expect( gen.next( postId ).value ).toEqual( put( actions.setTicketsIsSettingsLoading( true ) ), ); expect( gen.next().value ).toEqual( put( rsvpActions.setRSVPIsSettingsLoading( true ) ), ); expect( gen.next().value ).toEqual( call( wpREST, { path: `tribe_events/${ postId }`, headers: { 'Content-Type': 'application/json', }, initParams: { method: 'PUT', body: JSON.stringify( { meta: { [ utils.KEY_TICKET_HEADER ]: null, }, } ), }, } ), ); const apiResponse = { response: { ok: false, }, }; expect( gen.next( apiResponse ).value ).toEqual( put( actions.setTicketsIsSettingsLoading( false ) ), ); expect( gen.next().value ).toEqual( put( rsvpActions.setRSVPIsSettingsLoading( false ) ), ); expect( gen.next().done ).toEqual( true ); } ); } ); describe( 'setTicketDetails', () => { it( 'should set ticket details', () => { const action = { payload: { clientId: 'tribe', details: { title: 'title', description: 'description', price: 10, sku: 'sku', startDate: '2018-01-01', endDate: '2018-01-02', startTime: '10:00', endTime: '12:00', capacityType: 'unlimited', capacity: 100, startDateMoment: undefined, endDateMoment: undefined, }, }, }; const gen = sagas.setTicketDetails( action ); expect( gen.next().value ).toEqual( all( [ put( actions.setTicketAttendeeInfoFields( action.payload.clientId, undefined ) ), put( actions.setTicketTitle( action.payload.clientId, action.payload.details.title ) ), put( actions.setTicketDescription( action.payload.clientId, action.payload.details.description ) ), put( actions.setTicketPrice( action.payload.clientId, action.payload.details.price ) ), put( actions.setTicketOnSale( action.payload.clientId, undefined ) ), put( actions.setTicketSku( action.payload.clientId, action.payload.details.sku ) ), put( actions.setTicketIACSetting( action.payload.clientId, undefined ) ), put( actions.setTicketStartDate( action.payload.clientId, action.payload.details.startDate ) ), put( actions.setTicketStartDateInput( action.payload.clientId, undefined ) ), put( actions.setTicketStartDateMoment( action.payload.clientId, action.payload.details.startDateMoment ) ), put( actions.setTicketEndDate( action.payload.clientId, action.payload.details.endDate ) ), put( actions.setTicketEndDateInput( action.payload.clientId, undefined ) ), put( actions.setTicketEndDateMoment( action.payload.clientId, action.payload.details.endDateMoment ) ), put( actions.setTicketStartTime( action.payload.clientId, action.payload.details.startTime ) ), put( actions.setTicketEndTime( action.payload.clientId, action.payload.details.endTime ) ), put( actions.setTicketStartTimeInput( action.payload.clientId, undefined ) ), put( actions.setTicketEndTimeInput( action.payload.clientId, undefined ) ), put( actions.setTicketCapacityType( action.payload.clientId, action.payload.details.capacityType ) ), put( actions.setTicketCapacity( action.payload.clientId, action.payload.details.capacity ) ), put( actions.setTicketType( action.payload.clientId, undefined ) ), put( actions.setSalePriceChecked( action.payload.clientId, undefined ) ), put( actions.setSalePrice( action.payload.clientId, undefined ) ), put( actions.setTicketSaleStartDate( action.payload.clientId, undefined ) ), put( actions.setTicketSaleStartDateInput( action.payload.clientId, undefined ) ), put( actions.setTicketSaleStartDateMoment( action.payload.clientId, undefined ) ), put( actions.setTicketSaleEndDate( action.payload.clientId, undefined ) ), put( actions.setTicketSaleEndDateInput( action.payload.clientId, undefined ) ), put( actions.setTicketSaleEndDateMoment( action.payload.clientId, undefined ) ), ] ), ); expect( gen.next().done ).toEqual( true ); } ); } );} );
Save File
Cancel