Skip to content

Commit

Permalink
Add unit tests for SavedSate component
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jun 20, 2017
1 parent d069891 commit eb93fde
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion editor/header/saved-state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
getEditedPostAttribute,
} from '../../selectors';

function SavedState( { isNew, isDirty, isSaving, isSaveable, status, onStatusChange, onSave } ) {
export function SavedState( { isNew, isDirty, isSaving, isSaveable, status, onStatusChange, onSave } ) {
const className = 'editor-saved-state';

if ( isSaving ) {
Expand Down
70 changes: 70 additions & 0 deletions editor/header/saved-state/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* External dependencies
*/
import { expect } from 'chai';
import { shallow } from 'enzyme';
import sinon from 'sinon';

/**
* Internal dependencies
*/
import { SavedState } from '../';

describe( 'SavedState', () => {
it( 'should display saving while save in progress, even if not saveable', () => {
const wrapper = shallow(
<SavedState
isNew
isDirty={ false }
isSaving={ true }
isSaveable={ false } />
);

expect( wrapper.text() ).to.equal( 'Saving' );
} );

it( 'returns null if the post is not saveable', () => {
const wrapper = shallow(
<SavedState
isNew
isDirty={ false }
isSaving={ false }
isSaveable={ false } />
);

expect( wrapper.type() ).to.be.null();
} );

it( 'should return Saved text if not new and not dirty', () => {
const wrapper = shallow(
<SavedState
isNew={ false }
isDirty={ false }
isSaving={ false }
isSaveable={ true } />
);

expect( wrapper.childAt( 0 ).name() ).to.equal( 'Dashicon' );
expect( wrapper.childAt( 1 ).text() ).to.equal( 'Saved' );
} );

it( 'should return Save button if edits to be saved', () => {
const statusSpy = sinon.spy();
const saveSpy = sinon.spy();
const wrapper = shallow(
<SavedState
isNew={ false }
isDirty={ true }
isSaving={ false }
isSaveable={ true }
onStatusChange={ statusSpy }
onSave={ saveSpy } />
);

expect( wrapper.name() ).to.equal( 'Button' );
expect( wrapper.childAt( 0 ).text() ).to.equal( 'Save' );
wrapper.simulate( 'click' );
expect( statusSpy ).to.have.been.calledWith( 'draft' );
expect( saveSpy ).to.have.been.called();
} );
} );

0 comments on commit eb93fde

Please sign in to comment.