ichigoo
Version:
Static site generator with React and GraphQL
180 lines (176 loc) • 4.32 kB
JavaScript
;
var _graphql = require("graphql");
var _schemaBuilder = _interopRequireDefault(require("./schemaBuilder"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
test('plugs resolvers with schema', function () {
var schema = (0, _schemaBuilder["default"])({
posts: [{
id: 0,
title: 'hello',
foo: 'bar'
}]
});
return (0, _graphql.graphql)(schema, 'query { Post(id: 0) { id title } }').then(function (result) {
return expect(result).toEqual({
data: {
Post: {
id: '0',
title: 'hello'
}
}
});
});
});
var data = {
posts: [{
id: 1,
title: 'Lorem Ipsum',
views: 254,
user_id: 123
}, {
id: 2,
title: 'Ut enim ad minim veniam',
views: 65,
user_id: 456
}, {
id: 3,
title: 'Sic Dolor amet',
views: 76,
user_id: 123
}],
users: [{
id: 123,
name: 'John Doe'
}, {
id: 456,
name: 'Jane Doe'
}],
comments: [{
id: 987,
post_id: 1,
body: 'Consectetur adipiscing elit'
}, {
id: 995,
post_id: 1,
body: 'Nam molestie pellentesque dui'
}, {
id: 998,
post_id: 2,
body: 'Sunt in culpa qui officia'
}]
};
var schema = (0, _schemaBuilder["default"])(data);
test('all* route returns all entities by default', function () {
return (0, _graphql.graphql)(schema, '{ allPosts { id } }').then(function (result) {
return expect(result).toEqual({
data: {
allPosts: [{
id: '1'
}, {
id: '2'
}, {
id: '3'
}]
}
});
});
});
test('all* route supports pagination', function () {
return (0, _graphql.graphql)(schema, '{ allPosts(page: 0, perPage: 2) { id } }').then(function (result) {
return expect(result).toEqual({
data: {
allPosts: [{
id: '1'
}, {
id: '2'
}]
}
});
});
});
test('all* route supports sorting', function () {
return (0, _graphql.graphql)(schema, '{ allPosts(sortField: "views", sortOrder: "desc") { id } }').then(function (result) {
return expect(result).toEqual({
data: {
allPosts: [{
id: '1'
}, {
id: '3'
}, {
id: '2'
}]
}
});
});
});
test('all* route supports filtering', function () {
return (0, _graphql.graphql)(schema, '{ allPosts(filter: { q: "lorem"}) { id } }').then(function (result) {
return expect(result).toEqual({
data: {
allPosts: [{
id: '1'
}]
}
});
});
});
test('entity route returns a single entity', function () {
return (0, _graphql.graphql)(schema, '{ Post(id: 2) { id } }').then(function (result) {
return expect(result).toEqual({
data: {
Post: {
id: '2'
}
}
});
});
});
test('entity route gets all the entity fields', function () {
return (0, _graphql.graphql)(schema, '{ Post(id: 1) { id title views user_id } }').then(function (result) {
return expect(result).toEqual({
data: {
Post: {
id: '1',
title: 'Lorem Ipsum',
user_id: '123',
views: 254
}
}
});
});
});
test('entity route get many to one relationships fields', function () {
return (0, _graphql.graphql)(schema, '{ Post(id: 1) { User { name } } }').then(function (result) {
return expect(result).toEqual({
data: {
Post: {
User: {
name: 'John Doe'
}
}
}
});
});
});
test('entity route get one to many relationships fields', function () {
return (0, _graphql.graphql)(schema, '{ Post(id: 1) { Comments { body } } }').then(function (result) {
return expect(result).toEqual({
data: {
Post: {
Comments: [{
body: 'Consectetur adipiscing elit'
}, {
body: 'Nam molestie pellentesque dui'
}]
}
}
});
});
});
test('returns an error when asked for a non existent field', function () {
return (0, _graphql.graphql)(schema, '{ Post(id: 1) { foo } }').then(function (result) {
return expect(result).toEqual({
errors: [new _graphql.GraphQLError('Cannot query field "foo" on type "Post".')]
});
});
});