it( 'Should execute the correct actions on success', async () => {
		const { invoke } = create();

		const body = {
			id: 1217,
			date: '2018-05-26T23:07:05',
			meta: {},
		};

		const headers = new Headers();

		global.fetch = jest.fn().mockImplementation( () =>
			Promise.resolve( {
				ok: true,
				status: 200,
				json: () => body,
				headers,
			} ),
		);

		await invoke( actions.wpRequest( { ...meta, path: 'tribe_organizer/1217' } ) );

		expect.assertions( 8 );
		expect( meta.actions.none ).not.toHaveBeenCalled();
		expect( meta.actions.error ).not.toHaveBeenCalled();
		expect( meta.actions.start ).toHaveBeenCalledWith( 'wp/v2/tribe_organizer/1217', {} );
		expect( meta.actions.start ).toHaveBeenCalled();
		expect( meta.actions.start ).toHaveBeenCalledTimes( 1 );
		expect( meta.actions.success ).toHaveBeenCalled();
		expect( meta.actions.success )
			.toHaveBeenCalledWith( { body, headers } );
		expect( meta.actions.success ).toHaveBeenCalledTimes( 1 );
	} );
	it( 'Should reject on 404 status code', async () => {
		const { invoke } = create();

		global.fetch = jest.fn().mockImplementation( () => Promise.resolve( { status: 404 } ) );

		const error = await invoke( actions.wpRequest( { ...meta, path: 'tribe_organizer/1217' } ) );
		expect.assertions( 6 );
		expect( meta.actions.none ).not.toHaveBeenCalled();
		expect( meta.actions.success ).not.toHaveBeenCalled();
		expect( meta.actions.start ).toHaveBeenCalled();
		expect( meta.actions.start ).toHaveBeenCalledWith( 'wp/v2/tribe_organizer/1217', {} );
		expect( meta.actions.error ).toHaveBeenCalled();
		expect( meta.actions.error ).toHaveBeenCalledWith( error );
	} );
	it( 'Should execute the none action if the path is empty', () => {
		const { next, invoke } = create();
		const action = actions.wpRequest( meta );
		invoke( action );

		expect( next ).toHaveBeenCalled();
		expect( next ).toHaveBeenCalledTimes( 1 );
		expect( next ).toHaveBeenCalledWith( action );
		expect( meta.actions.none ).toHaveBeenCalled();
		expect( meta.actions.none ).toHaveBeenCalledTimes( 1 );
		expect( meta.actions.none ).toHaveBeenLastCalledWith( meta.path );
		expect( meta.actions.start ).not.toHaveBeenCalled();
		expect( meta.actions.success ).not.toHaveBeenCalled();
		expect( meta.actions.error ).not.toHaveBeenCalled();
	} );
Example #4
0
export const maybeRemoveEntry = ( id, details = {} ) => ( dispatch, getState ) => {
	const state = getState();
	const type = selectors.getFormType( state, { name: id } );

	if ( isEmpty( details ) ) {
		return;
	}

	const path = `${ type }/${ details.id }`;
	const options = {
		path,
		actions: {
			success: deleteEntry( dispatch )( path ),
		},
	};
	dispatch( requestActions.wpRequest( options ) );
};
	it( 'Should execute the correct actions on failure', async () => {
		const { invoke } = create();

		global.fetch = jest.fn().mockImplementation( () => Promise.reject( 'Wrong path' ) );

		const error = await invoke( actions.wpRequest( {
			...meta,
			path: 'tribe_organizer/1217//////',
		} ) );
		expect.assertions( 6 );
		expect( meta.actions.none ).not.toHaveBeenCalled();
		expect( meta.actions.success ).not.toHaveBeenCalled();
		expect( meta.actions.start ).toHaveBeenCalled();
		expect( meta.actions.start ).toHaveBeenCalledWith( 'wp/v2/tribe_organizer/1217//////', {} );
		expect( meta.actions.error ).toHaveBeenCalled();
		expect( meta.actions.error ).toHaveBeenCalledWith( error );
	} );
Example #6
0
const deleteEntry = ( dispatch ) => ( path ) => ( { body } ) => {
	const { id, status } = body;

	if ( 'draft' !== status ) {
		dispatch( removeVolatile( id ) );
		return;
	}

	const options = {
		path,
		params: {
			method: 'DELETE',
		},
		actions: {
			success: () => dispatch( removeVolatile( id ) ),
		},
	};
	dispatch( requestActions.wpRequest( options ) );
};
Example #7
0
export const sendForm = ( id, fields = {}, completed ) => ( dispatch, getState ) => {
	const state = getState();
	const props = { name: id };
	const type = selectors.getFormType( state, props );
	const create = selectors.getFormCreate( state, props );
	const details = selectors.getFormFields( state, props );
	const saving = selectors.getFormSaving( state, props );

	if ( saving ) {
		return;
	}

	const path = create
		? `${ type }`
		: `${ type }/${ details.id }`;

	const options = {
		path,
		params: {
			method: create ? 'POST' : 'PUT',
			body: JSON.stringify( fields ),
		},
		actions: {
			start: () => dispatch( setSaving( id, true ) ),
			success: ( { body } ) => {
				const postID = get( body, 'id', '' );

				if ( create && postID ) {
					dispatch( addVolatile( postID ) );
				}
				completed( body );
				dispatch( clearForm( id ) );
				dispatch( setSaving( id, false ) );
			},
			error: () => {
				dispatch( clearForm( id ) );
				dispatch( setSaving( id, false ) );
			},
		},
	};
	dispatch( requestActions.wpRequest( options ) );
};