-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(schema): add unit tests for simple queries
- Loading branch information
Peter Marton
committed
Jul 27, 2015
1 parent
3b962d6
commit 28d7ee6
Showing
2 changed files
with
136 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,148 @@ | ||
import {expect} from 'chai'; | ||
import {graphql} from 'graphql'; | ||
import mongoose from 'mongoose'; | ||
import {getSchema} from './schema'; | ||
|
||
describe('schema', () => { | ||
it('should generate schema for model', () => { | ||
let schema; | ||
let User; | ||
|
||
before(() => { | ||
var UserSchema = new mongoose.Schema({ | ||
name: { | ||
type: String | ||
}, | ||
age: Number | ||
friends: [{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User' | ||
}] | ||
}); | ||
|
||
User = mongoose.model('User', UserSchema); | ||
|
||
schema = getSchema([User]); | ||
}); | ||
|
||
it('should exist', () => { | ||
expect(schema).to.be.not.undefined; | ||
}); | ||
|
||
describe('singular resource', () => { | ||
it('should get data by id with selected fields', function* () { | ||
var user = new User({ | ||
name: 'Foo' | ||
}); | ||
|
||
var findByIdStub = this.sandbox.stub(User, 'findById').returnsWithResolve(user); | ||
|
||
var result = yield graphql(schema, `{ | ||
user(id: "aaa") { | ||
name | ||
} | ||
}`); | ||
|
||
expect(findByIdStub).to.calledWith('aaa', { | ||
name: 1 | ||
}); | ||
|
||
expect(result).to.be.eql({ | ||
data: { | ||
user: { | ||
name: 'Foo' | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
var User = mongoose.model('User', UserSchema); | ||
// TODO: missing test cases | ||
it('should get data by indexed fields'); | ||
|
||
it('should get data with primitive fields'); | ||
it('should get data with array of primitives fields'); | ||
it('should get data with sub-document fields'); | ||
|
||
it('should get data with ref fields', function* () { | ||
var user1 = new User({ | ||
name: 'Foo' | ||
}); | ||
|
||
var schema = getSchema([User]); | ||
var user2 = new User({ | ||
name: 'Bar', | ||
friends: [user1._id] | ||
}); | ||
|
||
var findByIdStub = this.sandbox.stub(User, 'findById').returnsWithResolve(user2); | ||
var findStub = this.sandbox.stub(User, 'find').returnsWithResolve([user1]); | ||
|
||
var result = yield graphql(schema, `{ | ||
user(id: "bbb") { | ||
name | ||
friends { | ||
name | ||
} | ||
} | ||
}`); | ||
|
||
expect(findByIdStub).to.calledWith('bbb', { | ||
name: 1, | ||
friends: 1 | ||
}); | ||
|
||
expect(findStub).to.calledWithMatch({ | ||
_id: { | ||
$in: [user1._id.toString()] | ||
} | ||
}, { | ||
name: 1 | ||
}); | ||
|
||
expect(result).to.be.eql({ | ||
data: { | ||
user: { | ||
name: 'Bar', | ||
friends: [ | ||
{ | ||
name: 'Foo' | ||
} | ||
] | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('plural resource', () => { | ||
it('should get data with selected fields', function* () { | ||
var user1 = new User({ | ||
name: 'Foo' | ||
}); | ||
|
||
var user2 = new User({ | ||
name: 'Bar' | ||
}); | ||
|
||
this.sandbox.stub(User, 'find').returnsWithResolve([user1, user2]); | ||
|
||
var result = yield graphql(schema, `{ | ||
users { | ||
name | ||
} | ||
}`); | ||
|
||
expect(result).to.be.eql({ | ||
data: { | ||
users: [ | ||
{ | ||
name: 'Foo' | ||
}, { | ||
name: 'Bar' | ||
} | ||
] | ||
} | ||
}); | ||
}); | ||
|
||
expect(schema).to.be.not.null; | ||
// TODO: missing test cases | ||
it('should filter data by indexed fields'); | ||
}); | ||
}); |