Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for refx REQUEST_POST_UPDATE effects network requests. #966

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 212 additions & 0 deletions editor/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,218 @@ describe( 'effects', () => {
} );
} );

describe( '.REQUEST_POST_UPDATE', () => {
const handler = effects.REQUEST_POST_UPDATE;
const dispatch = jest.fn();

beforeEach( () => {
dispatch.mockReset();
} );

// Mock wp.api
const apiCall = function( post ) {
return {
// getPostTypeModel appears to return an object constructor.
getPostTypeModel: function( postType ) { // eslint-disable-line no-unused-vars
return function( toSend ) { // eslint-disable-line no-unused-vars
this.save = function() {
return this;
};
// Callback takes a post object.
this.done = function( callback ) {
if ( post !== 'error' ) {
callback( post );
}
return this;
};
this.fail = function( callback ) {
if ( post === 'error' ) {
callback( post );
}
return this;
};
};
},
};
};

it( 'should dispatch four actions on successful post.', () => {
const post = {
id: 1,
title: {
raw: 'A History of Pork',
},
content: {
raw: '',
},
status: 'draft',
};

const store = { getState: () => ( {
currentPost: post,
previousPost: post,
editor: {
present: {
edits: {
content: 'yao',
},
},
past: {},
},
content: 'Yao',
} ), dispatch };

// Mock wp.api
wp.api = apiCall( post );

handler( {}, store );

const updatePost = {
edits: {
content: 'yao',
id: 1,
},
optimist: {
id: 'post-update',
type: 'BEGIN',
},
type: 'UPDATE_POST',
};

const removeNotice = {
noticeId: 'SAVE_POST_NOTICE_ID',
type: 'REMOVE_NOTICE',
};

const resetPostAction = {
post: {
content: {
raw: '',
},
id: 1,
status: 'draft',
title: {
raw: 'A History of Pork',
},
},
type: 'RESET_POST',
};

const requestPostUpdateSuccess = {
optimist: {
id: 'post-update',
type: 'COMMIT',
},
post: {
content: {
raw: '',
},
id: 1,
status: 'draft',
title: {
raw: 'A History of Pork',
},
},
previousPost: {
content: {
raw: '',
},
id: 1,
status: 'draft',
title: {
raw: 'A History of Pork',
},
},
type: 'REQUEST_POST_UPDATE_SUCCESS',
};

expect( dispatch ).toHaveBeenCalledTimes( 4 );
expect( dispatch ).toHaveBeenCalledWith( updatePost );
expect( dispatch ).toHaveBeenCalledWith( removeNotice );
expect( dispatch ).toHaveBeenCalledWith( resetPostAction );
expect( dispatch ).toHaveBeenCalledWith( requestPostUpdateSuccess );
} );

it( 'should dispatch three actions on failure post.', () => {
const post = {
id: 1,
title: {
raw: 'A History of Pork',
},
content: {
raw: '',
},
status: 'draft',
};

const store = { getState: () => ( {
currentPost: post,
previousPost: post,
editor: {
present: {
edits: {
content: 'yao',
},
},
past: {},
},
content: 'Yao',
} ), dispatch };

// Mock wp.api
wp.api = apiCall( 'error' );

handler( {}, store );

const updatePost = {
edits: {
content: 'yao',
id: 1,
},
optimist: {
id: 'post-update',
type: 'BEGIN',
},
type: 'UPDATE_POST',
};

const removeNotice = {
noticeId: 'SAVE_POST_NOTICE_ID',
type: 'REMOVE_NOTICE',
};

const requestPostFailure = {
edits: {
content: 'yao',
},
error: {
code: 'unknown_error',
message: 'An unknown error occurred.',
},
optimist: {
id: 'post-update',
type: 'REVERT',
},
post: {
content: {
raw: '',
},
id: 1,
status: 'draft',
title: {
raw: 'A History of Pork',
},
},
type: 'REQUEST_POST_UPDATE_FAILURE',
};

expect( dispatch ).toHaveBeenCalledTimes( 3 );
expect( dispatch ).toHaveBeenCalledWith( updatePost );
expect( dispatch ).toHaveBeenCalledWith( removeNotice );
expect( dispatch ).toHaveBeenCalledWith( requestPostFailure );
} );
} );

describe( '.REQUEST_POST_UPDATE_SUCCESS', () => {
beforeAll( () => {
selectors.getDirtyMetaBoxes = jest.spyOn( selectors, 'getDirtyMetaBoxes' );
Expand Down