Skip to content

Commit

Permalink
Fix #138 adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yasserf committed Jul 27, 2016
1 parent fd537d2 commit ce53e62
Showing 1 changed file with 24 additions and 113 deletions.
137 changes: 24 additions & 113 deletions test-e2e/specs/recordSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,25 @@ describe( 'record', function() {

/**************** TEST ****************/
it( 'subscribes and sets a record', function(done) {
clientA.record.getRecord( 'record1' ).subscribe( 'user.firstname', function( firstname ){
expect( firstname ).toBe( 'Wolfram' );
done();
});
var update = 0;
var record = clientA.record.getRecord( 'record1' );

record.subscribe( function( data ) {
if( update === 0 ) {
expect( data ).toEqual( {} );
update++;
}
}, true);

record.subscribe( 'user.firstname', function( firstname ){
if( update === 1 ) {
expect( firstname ).toBeUndefined();
} else if( update === 2 ) {
expect( firstname ).toBe( 'Wolfram' );
done();
}
update++;
}, true);

clientB.record.getRecord( 'record1' ).set({
user: {
Expand Down Expand Up @@ -98,8 +113,12 @@ describe( 'record', function() {
clientA.record.getRecord( 'record1' ).subscribe( 'objectToDelete', subscribePath );
clientB.record.getRecord( 'record1' ).subscribe( 'objectToDelete', subscribePath );

var count = 0;
var subscribeObject = function( value ) {
expect( value ).toEqual( { user: { firstname: 'Wolfram', lastname: 'Hempel' } } );
if( count === 0 ) {
expect( value ).toEqual( { user: { firstname: 'Wolfram', lastname: 'Hempel' } } );
}
count++;
};
clientA.record.getRecord( 'record1' ).subscribe( subscribeObject );
clientB.record.getRecord( 'record1' ).subscribe( subscribeObject );
Expand Down Expand Up @@ -158,114 +177,6 @@ describe( 'record', function() {
}, 20 );
});


it( 'does not keep objects by reference', function() {
var a = {
number: 1
};
clientB.record.getRecord( 'record1' ).set( 'myObject', a );
a.number = 2;
expect( clientB.record.getRecord( 'record1' ).get( 'myObject' ) ).not.toEqual( a );
} );

it( 'does update after object properties are changed and set' ,function( done ) {
var b = {
digit: 1
};
clientB.record.getRecord( 'record1' ).set( 'myObject', b );
b.digit = 2;
clientB.record.getRecord( 'record1' ).set( 'myObject', b );
expect( clientB.record.getRecord( 'record1' ).get( 'myObject' ) ).toEqual( b );
setTimeout( function() {
expect( clientA.record.getRecord( 'record1' ).get( 'myObject' ) ).toEqual( b );
done();
}, 20 );
});

it( 'subscribes and unsubscribes', function( done ) {
var pet,
recordA2 = clientA.record.getRecord( 'record2' ),
recordB2 = clientB.record.getRecord( 'record2' ),
setPet = function( _pet ){ pet = _pet; };

// Set value before record is ready
recordA2.set( 'pets[2]', 'Samoyed' );
recordA2.subscribe( 'pets[ 2 ]', setPet , true );

recordA2.on( 'ready', function(){
// Record was created correctly
expect( recordA2.get() ).toEqual({ pets: [ null, null, 'Samoyed' ] });

// subscriber was notified
expect( pet ).toBe( 'Samoyed' );

// subscriber notified of change
recordA2.set( 'pets[2]', 'Turtle' );
expect( pet ).toBe( 'Turtle' );

// subscriber removed
recordA2.unsubscribe( 'pets[ 2 ]', setPet );

// subscriber won't be notified
recordA2.set( 'pets[2]', 'Guineapig' );
expect( pet ).toBe( 'Turtle' );

// structure was changed correctly
expect( recordA2.get() ).toEqual({ pets: [ null, null, 'Guineapig' ] });

// all good
done();
});
});

it( 'subscribes and discards', function( done ) {
var city,
recordA3 = clientA.record.getRecord( 'record3' ),
recordB3 = clientB.record.getRecord( 'record3' );

// Trigger immediatly
recordA3.subscribe( 'city', function( _city ){ city = _city; }, true );
expect( city ).toBe( undefined );

recordB3.set( 'city', 'Berlin' );
expect( city ).toBe( undefined );

setTimeout(function(){
expect( city ).toBe( 'Berlin' );
recordA3.discard();
recordB3.set( 'city', 'Dresden' );
setTimeout(function() {
expect( city ).toBe( 'Berlin' );
done();
}, 20 );
}, 20 );
});

/**************** TEAR DOWN ****************/
it( 'allows discard to be called before the record is ready', function( done ) {
var recordToDiscard = clientA.record.getRecord( 'recordToDiscardImmediately' );
recordToDiscard
.set( { key: 'value' } )
.discard();

setTimeout( function() {
//Not failing the test is proof this works
done();
}, 20 );
});

it( 'allows delete to be called before the record is ready', function( done ) {
var recordToDelete = clientA.record.getRecord( 'recordToDeleteImmediately' );
recordToDelete
.set( { key: 'value' } )
.delete();

setTimeout( function() {
//Not failing the test is proof this works
done();
}, 20 );
});

/**************** TEAR DOWN ****************/
it( 'closes the clients', function() {
clientA.close();
Expand Down

0 comments on commit ce53e62

Please sign in to comment.