Skip to content

Commit

Permalink
One more case to test
Browse files Browse the repository at this point in the history
  • Loading branch information
waterfoul committed Aug 12, 2016
1 parent 2b3b5dd commit 655b551
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
46 changes: 42 additions & 4 deletions node/relationships/OneToOne.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ describe('node/relationships/OneToOne', function() {
let connect: any;
let classes: any;
let instance: {
get: any
get: any,
set: any,
save: any
};

before(function(){
Expand Down Expand Up @@ -42,7 +44,9 @@ describe('node/relationships/OneToOne', function() {
beforeEach(function () {
mockConnect.reset(connect);
instance = {
get: sinon.stub()
get: sinon.stub(),
set: sinon.stub(),
save: sinon.stub()
};
});

Expand Down Expand Up @@ -190,7 +194,7 @@ describe('node/relationships/OneToOne', function() {
});
});

it('Does add to a create when not set (existing)', function () {
it('Does add to a create when set (existing)', function () {
instance.get.withArgs('id').returns(0);
mockConnect.model.create.returns(Promise.resolve(instance));
(<any> mockConnect.model).name = 'OneToOneB';
Expand All @@ -207,7 +211,7 @@ describe('node/relationships/OneToOne', function() {
});
});

it('Does add to a create when not set (new)', function () {
it('Does add to a create when set (new)', function () {
instance.get.withArgs('id').returns(0);
mockConnect.model.create.returns(Promise.resolve(instance));
(<any> mockConnect.model).name = 'OneToOneB';
Expand All @@ -217,13 +221,47 @@ describe('node/relationships/OneToOne', function() {
return b.save().then(() => {
const args = mockConnect.model.create.args;
chai.expect(args).to.deep.equal([
[ {} ],
[ {
OneToOneBId: 0
} ]
]);
});
});

it('Does add to a create when set (null)', function () {
instance.get.withArgs('id').returns(0);
mockConnect.model.create.returns(Promise.resolve(instance));
(<any> mockConnect.model).name = 'OneToOneB';

const b: classTypes.OneToOneB = new classes.OneToOneB();
b.a.set(null);
return b.save().then(() => {
const args = mockConnect.model.create.args;
chai.expect(args).to.deep.equal([
[ {
OneToOneBId: null
} ]
]);
});
});

it('Sets the base model when set', function () {
instance.get.withArgs('id').returns(0);
mockConnect.model.create.returns(Promise.resolve(instance));
instance.save.returns(Promise.resolve(instance));
(<any> mockConnect.model).name = 'OneToOneB';

const b: classTypes.OneToOneB = new classes.OneToOneB(instance);
const setPromise = b.a.set(new classes.OneToOneA(instance));
return setPromise.then(() => {
const args = instance.set.args;
chai.expect(args).to.deep.equal([
[ 'OneToOneBId', 0 ]
]);
});
});

after(function() {
mockery.deregisterAll();
mockery.disable();
Expand Down
7 changes: 6 additions & 1 deletion node/relationships/OneToOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class OneToOne<T extends DataContract> {
private currentValue: T = null;
/** This will only be set after a new value is set to this object and isFirst returns true */
private idName: string = null;
private changedModels: DataContract[];
private changedModels: DataContract[] = [];

public constructor(private parent: DataContract,
private target: () => IDataContractConstruct<T>) {
Expand Down Expand Up @@ -99,6 +99,11 @@ export class OneToOne<T extends DataContract> {
}

this.currentValue = newModel;

if (!newModel.createdAt) { // If the passed model is new
// Mark it as changed so we can get the ID later
this.changedModels.push(newModel);
}
} catch (e) {
/* istanbul ignore next */
return Promise.reject(e);
Expand Down
1 change: 1 addition & 0 deletions node/relationships/relatedField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function relatedField<T extends DataContract>(
}
return this['_' + key];
};
/* istanbul ignore next */
const setter = () => { /* */ };

sharedField(
Expand Down

0 comments on commit 655b551

Please sign in to comment.