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

Tests: cleanup account recovery store test #4897

Merged
merged 1 commit into from
Apr 21, 2016
Merged
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
93 changes: 43 additions & 50 deletions client/lib/security-checkup/test/account-recovery-store.js
Original file line number Diff line number Diff line change
@@ -1,139 +1,132 @@
require( 'lib/react-test-env-setup' )();
require( 'lib/mixins/i18n' ).initialize();

/**
* External dependencies
*/
const chai = require( 'chai' ),
expect = chai.expect,
sinon = require( 'sinon' ),
sinonChai = require( 'sinon-chai' );

chai.use( sinonChai );
import { expect } from 'chai';

/**
* Internal dependencies
*/
const Dispatcher = require( 'dispatcher' ),
testConstants = require( './fixtures/constants' ),
undocumentedMe = require( 'lib/wpcom-undocumented/lib/me' );
import { useSandbox } from 'test/helpers/use-sinon';
import useI18n from 'test/helpers/use-i18n';

describe( 'AccountRecoveryStore', function() {
var AccountRecoveryStore, sandbox, dispatchHandler;
import Dispatcher from 'dispatcher';
import undocumentedMe from 'lib/wpcom-undocumented/lib/me';

function dispatch( action ) {
dispatchHandler( {
action: action
} );
}
describe( 'AccountRecoveryStore', () => {
let AccountRecoveryStore, sandbox, dispatchHandler, testConstants;

beforeEach( function() {
sandbox = sinon.sandbox.create();
useI18n();

useSandbox( sandy => sandbox = sandy );

beforeEach( () => {
testConstants = require( './fixtures/constants' );
sandbox.spy( Dispatcher, 'register' );
sandbox.stub( undocumentedMe.prototype, 'getAccountRecovery' )
.callsArgWithAsync( 0, null, testConstants.DUMMY_ACCOUNT_RECOVERY_RESPONSE );

delete require.cache[ require.resolve( '../account-recovery-store' ) ];
AccountRecoveryStore = require( '../account-recovery-store' );
dispatchHandler = Dispatcher.register.lastCall.args[ 0 ];
} );

afterEach( function() {
afterEach( () => {
sandbox.restore();
} );

describe( 'getEmail()', function() {
it( 'should call the WP.com REST API on first request', function() {
var data = AccountRecoveryStore.getEmail();
describe( 'getEmail()', () => {
it( 'should call the WP.com REST API on first request', () => {
const data = AccountRecoveryStore.getEmail();
expect( undocumentedMe.prototype.getAccountRecovery ).to.have.been.calledOnce;

expect( data ).to.eql( testConstants.DUMMY_STORE_EMAIL_OBJECT_LOADING );
} );

it( 'should only call the WP.com REST API only once', function( done ) {
it( 'should only call the WP.com REST API only once', done => {
AccountRecoveryStore.getEmail();
process.nextTick( function() {
process.nextTick( () => {
AccountRecoveryStore.getEmail();

process.nextTick( function() {
process.nextTick( () => {
expect( undocumentedMe.prototype.getAccountRecovery ).to.have.been.calledOnce;

done();
} );
} );
} );

it( 'should return remote data', function( done ) {
it( 'should return remote data', done => {
AccountRecoveryStore.getEmail();

process.nextTick( function() {
var data = AccountRecoveryStore.getEmail();
process.nextTick( () => {
const data = AccountRecoveryStore.getEmail();
expect( data ).to.eql( testConstants.DUMMY_STORE_EMAIL_OBJECT );
done();
} );
} );
} );

describe( 'getPhone()', function() {
it( 'should call the WP.com REST API on first request', function() {
var data = AccountRecoveryStore.getPhone();
describe( 'getPhone()', () => {
it( 'should call the WP.com REST API on first request', () => {
const data = AccountRecoveryStore.getPhone();

expect( undocumentedMe.prototype.getAccountRecovery ).to.have.been.calledOnce;

expect( data ).to.eql( testConstants.DUMMY_STORE_PHONE_OBJECT_LOADING );
} );

it( 'should only call the WP.com REST API only once', function( done ) {
it( 'should only call the WP.com REST API only once', done => {
AccountRecoveryStore.getPhone();
process.nextTick( function() {
process.nextTick( () => {
AccountRecoveryStore.getPhone();

process.nextTick( function() {
process.nextTick( () => {
expect( undocumentedMe.prototype.getAccountRecovery ).to.have.been.calledOnce;
done();
} );
} );
} );

it( 'should return remote data', function( done ) {
it( 'should return remote data', done => {
AccountRecoveryStore.getPhone();

process.nextTick( function() {
process.nextTick( () => {
var data = AccountRecoveryStore.getPhone();
expect( data ).to.eql( testConstants.DUMMY_STORE_PHONE_OBJECT );
done();
} );
} );
} );

describe( '.dispatchToken', function() {
it( 'should expose dispatch ID', function() {
describe( '.dispatchToken', () => {
let dispatch;
before( () => {
dispatch = action => dispatchHandler( { action } );
} );

it( 'should expose dispatch ID', () => {
expect( AccountRecoveryStore.dispatchToken ).to.be.a( 'string' );
} );

it( 'should update email on UPDATE_ACCOUNT_RECOVERY_EMAIL', function() {
it( 'should update email on UPDATE_ACCOUNT_RECOVERY_EMAIL', () => {
dispatch( testConstants.DISPATCH_UPDATE_ACCOUNT_RECOVERY_EMAIL );
expect( AccountRecoveryStore.getEmail() ).to.have.deep.property( 'data.email', testConstants.DUMMY_EMAIL );
} );

it( 'should update and set notice on RECEIVE_UPDATED_ACCOUNT_RECOVERY_EMAIL', function() {
it( 'should update and set notice on RECEIVE_UPDATED_ACCOUNT_RECOVERY_EMAIL', () => {
dispatch( testConstants.DISPATCH_RECEIVE_UPDATED_ACCOUNT_RECOVERY_EMAIL );
expect( AccountRecoveryStore.getEmail() ).to.have.deep.property( 'data.email', testConstants.DUMMY_EMAIL );
expect( AccountRecoveryStore.getEmail() ).to.have.property( 'lastNotice' ).that.is.an( 'object' );
} );

it( 'should delete on DELETE_ACCOUNT_RECOVERY_EMAIL', function() {
it( 'should delete on DELETE_ACCOUNT_RECOVERY_EMAIL', () => {
dispatch( testConstants.DISPATCH_DELETE_ACCOUNT_RECOVERY_EMAIL );
expect( AccountRecoveryStore.getEmail() ).to.have.deep.property( 'data.email', null );
} );

it( 'should delete and update notice on RECEIVE_DELETED_ACCOUNT_RECOVERY_EMAIL', function() {
it( 'should delete and update notice on RECEIVE_DELETED_ACCOUNT_RECOVERY_EMAIL', () => {
dispatch( testConstants.DISPATCH_RECEIVE_DELETED_ACCOUNT_RECOVERY_EMAIL );
expect( AccountRecoveryStore.getEmail() ).to.have.deep.property( 'data.email', null );
expect( AccountRecoveryStore.getEmail() ).to.have.property( 'lastNotice' ).that.is.an( 'object' );
} );

it( 'should dismiss notice on DISMISS_ACCOUNT_RECOVERY_EMAIL_NOTICE', function() {
it( 'should dismiss notice on DISMISS_ACCOUNT_RECOVERY_EMAIL_NOTICE', () => {
dispatch( testConstants.DISPATCH_RECEIVE_UPDATED_ACCOUNT_RECOVERY_EMAIL );
dispatch( testConstants.DISPATCH_DISMISS_ACCOUNT_RECOVERY_EMAIL_NOTICE );
expect( AccountRecoveryStore.getEmail() ).to.have.property( 'lastNotice' ).that.is.false;
Expand Down