Skip to content

Commit

Permalink
Merge pull request #72 from deepstreamIO/feature/anonymous-record-rea…
Browse files Browse the repository at this point in the history
…dy-event

Fixes #27 anonymous record ready event
  • Loading branch information
WolframHempel committed Feb 15, 2016
2 parents 7b6f811 + 8acbb4e commit 8012382
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/record/anonymous-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ AnonymousRecord.prototype.setName = function( recordName ) {
for( i = 0; i < this._subscriptions.length; i++ ) {
this._record.unsubscribe( this._subscriptions[ i ] );
}

this._record.discard();
}

Expand All @@ -135,6 +134,7 @@ AnonymousRecord.prototype.setName = function( recordName ) {
this._record.subscribe( this._subscriptions[ i ] );
}

this._record.whenReady( this.emit.bind( this, 'ready' ) );
this.emit( 'nameChanged', recordName );
};

Expand Down
8 changes: 8 additions & 0 deletions test-e2e/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Remote
module.exports = {
redisPort: 18741,
redisHost: 'pub-redis-18741.us-east-1-4.4.ec2.garantiadata.com',
messageTimeout: 2000
};

//Local
// module.exports = {
// redisPort: 6379,
// redisHost: 'localhost',
// messageTimeout: 2000
// };
16 changes: 8 additions & 8 deletions test-e2e/specs/anonymous-recordSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ describe( 'anonymous record', function() {
});

it( 'sets recordA', function(done) {
anonymousRecord.setName( 'recordA' );
setTimeout(function(){
expect( currentPet ).toBe( 'hamster' );
anonymousRecord.once( 'ready', function(){
expect( anonymousRecord.get() ).toEqual({ pet: 'hamster' });
expect( currentPet ).toBe( 'hamster' );
done();
}, 20);
});
anonymousRecord.setName( 'recordA' );
});

it( 'sets recordB', function(done) {
anonymousRecord.setName( 'recordB' );
setTimeout(function(){
expect( currentPet ).toBe( 'pug' );
anonymousRecord.once( 'ready', function(){
expect( anonymousRecord.get() ).toEqual({ pet: 'pug' });
expect( currentPet ).toBe( 'pug' );
done();
}, 20);
});
anonymousRecord.setName( 'recordB' );
});

/**************** TEAR DOWN ****************/
Expand Down
23 changes: 20 additions & 3 deletions test-unit/unit/record/anonymous-recordSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ describe( 'anonymous record allows switching of underlying records', function(){
var anonymousRecord,
recordHandler = new RecordHandler( options, new ConnectionMock(), new ClientMock() ),
generalCallback = jasmine.createSpy( 'general' ),
firstnameCallback = jasmine.createSpy( 'firstname' );
firstnameCallback = jasmine.createSpy( 'firstname' ),
readyCallback = jasmine.createSpy( 'ready' );

it( 'creates the anonymous record', function(){
anonymousRecord = new AnonymousRecord( recordHandler );
Expand All @@ -23,6 +24,7 @@ describe( 'anonymous record allows switching of underlying records', function(){

anonymousRecord.subscribe( 'firstname', firstnameCallback );
anonymousRecord.subscribe( generalCallback );
anonymousRecord.on( 'ready', readyCallback );

expect( firstnameCallback ).not.toHaveBeenCalled();
expect( generalCallback ).not.toHaveBeenCalled();
Expand All @@ -39,13 +41,15 @@ describe( 'anonymous record allows switching of underlying records', function(){
it( 'updates subscriptions once the record is ready', function(){
expect( firstnameCallback ).not.toHaveBeenCalled();
expect( generalCallback ).not.toHaveBeenCalled();
expect( readyCallback ).not.toHaveBeenCalled();

recordHandler._$handle({
topic: 'R',
action: 'R',
data: [ 'recordA', 1, '{"firstname":"Wolfram"}' ]
});

expect( readyCallback.calls.length ).toBe( 1 );
expect( firstnameCallback ).toHaveBeenCalledWith( 'Wolfram' );
expect( generalCallback ).toHaveBeenCalledWith({ firstname: 'Wolfram' });
});
Expand All @@ -59,13 +63,15 @@ describe( 'anonymous record allows switching of underlying records', function(){
data: [ 'recordB', 1, '{"firstname":"Egon", "lastname":"Kowalski"}' ]
});

expect( readyCallback.calls.length ).toBe( 1 );
expect( firstnameCallback ).toHaveBeenCalledWith( 'Wolfram' );
expect( generalCallback ).toHaveBeenCalledWith({ firstname: 'Wolfram' });
});

it( 'updates subscriptions when the record changes to an existing record', function(){
anonymousRecord.setName( 'recordB' );
expect( anonymousRecord.name ).toBe( 'recordB' );
expect( readyCallback.calls.length ).toBe( 2 );
expect( firstnameCallback ).toHaveBeenCalledWith( 'Egon' );
expect( generalCallback ).toHaveBeenCalledWith({ firstname: 'Egon', lastname: 'Kowalski' });
});
Expand All @@ -87,7 +93,7 @@ describe( 'anonymous record allows switching of underlying records', function(){
});

anonymousRecord.setName( 'recordC' );

expect( readyCallback.calls.length ).toBe( 2 );
expect( errorCallback ).not.toHaveBeenCalled();
});

Expand All @@ -99,5 +105,16 @@ describe( 'anonymous record allows switching of underlying records', function(){

expect( readyEventListener ).toHaveBeenCalled();
expect( readyEventListener ).toHaveBeenCalledWith( 'recordC' );
} );
});

it( 'emits an additional ready event once the new record becomes available', function(){
expect( readyCallback.calls.length ).toBe( 2 );
recordHandler._$handle({
topic: 'R',
action: 'R',
data: [ 'recordC', 1, '{"firstname":"Egon", "lastname":"Kowalski"}' ]
});
expect( readyCallback.calls.length ).toBe( 3 );
});

});

0 comments on commit 8012382

Please sign in to comment.