grapher/lib/query/testing/reducers.server.test.js

237 lines
6.6 KiB
JavaScript
Raw Normal View History

import { createQuery } from 'meteor/cultofcoders:grapher';
import Authors from './bootstrap/authors/collection';
import Comments from './bootstrap/comments/collection';
2018-04-17 11:17:26 +03:00
describe('Reducers', function() {
it('Should work with field only reducers', function() {
const data = createQuery({
authors: {
2018-04-17 11:17:26 +03:00
fullName: 1,
},
}).fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
assert.isString(author.fullName);
assert.isUndefined(author.name);
2018-04-17 11:17:26 +03:00
assert.isTrue(author.fullName.substr(0, 7) === 'full - ');
});
});
2018-04-17 11:17:26 +03:00
it('Should work with nested fields reducers', function() {
const data = createQuery({
authors: {
2018-04-17 11:17:26 +03:00
fullNameNested: 1,
},
}).fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
assert.isString(author.fullNameNested);
assert.isString(author.fullNameNested);
assert.isFalse(author.fullNameNested === 'undefined undefined');
assert.isUndefined(author.profile);
2018-04-17 11:17:26 +03:00
});
});
it('Should work with deep reducers', function() {
const data = createQuery({
posts: {
$options: { limit: 5 },
author: {
fullName: 1,
fullNameNested: 1,
},
},
}).fetch();
assert.isTrue(data.length > 0);
data.forEach(post => {
<<<<<<< HEAD
2018-04-17 11:17:26 +03:00
console.log(post);
=======
>>>>>>> Added support for Meteor 1.7 & Node Drivers 3+ with Hypernova
2018-04-17 11:17:26 +03:00
const author = post.author;
assert.isUndefined(author.name);
assert.isTrue(author.fullName.substr(0, 7) === 'full - ');
});
});
2018-04-17 11:17:26 +03:00
it('Should work with nested fields reducers', function() {
const data = createQuery({
authors: {
profile: {
2018-04-17 11:17:26 +03:00
firstName: 1,
},
fullNameNested: 1,
2018-04-17 11:17:26 +03:00
},
}).fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
assert.isString(author.fullNameNested);
assert.isFalse(author.fullNameNested === 'undefined undefined');
assert.isObject(author.profile);
assert.isString(author.profile.firstName);
assert.isUndefined(author.profile.lastName);
2018-04-17 11:17:26 +03:00
});
});
2018-04-17 11:17:26 +03:00
it('Should work with links reducers', function() {
const data = createQuery({
authors: {
2018-04-17 11:17:26 +03:00
groupNames: 1,
},
}).fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
assert.isArray(author.groupNames);
assert.isUndefined(author.groups);
2018-04-17 11:17:26 +03:00
});
});
2018-04-17 11:17:26 +03:00
it('Should work with One link reducers', function() {
const sampleComment = Comments.findOne();
const comment = createQuery({
comments: {
$filters: {
_id: sampleComment._id,
},
authorLinkReducer: 1,
2018-04-17 11:17:26 +03:00
},
}).fetchOne();
assert.isObject(comment);
assert.isObject(comment.authorLinkReducer);
});
2018-04-17 11:17:26 +03:00
it('Should work with links and nested reducers', function() {
const data = createQuery({
authors: {
2018-04-17 11:17:26 +03:00
referenceReducer: 1,
},
}).fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
assert.isString(author.referenceReducer);
assert.isUndefined(author.fullName);
2018-04-17 11:17:26 +03:00
assert.isTrue(author.referenceReducer.substr(0, 9) === 'nested - ');
});
});
it('Should not clean nested reducers if not specified', function() {
const data = createQuery({
authors: {
referenceReducer: 1,
fullName: 1,
},
}).fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
assert.isString(author.referenceReducer);
assert.isUndefined(author.fullName);
assert.isTrue(author.referenceReducer.substr(0, 9) === 'nested - ');
});
});
2018-04-17 11:17:26 +03:00
it('Should not clean nested reducers if not specified', function() {
const data = createQuery({
authors: {
referenceReducer: 1,
fullName: 1,
2018-04-17 11:17:26 +03:00
},
}).fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
assert.isString(author.referenceReducer);
assert.isString(author.fullName);
2018-04-17 11:17:26 +03:00
});
});
2018-04-17 11:17:26 +03:00
it('Should keep previously used items - Part 1', function() {
const data = createQuery({
authors: {
fullName: 1,
name: 1,
groupNames: 1,
groups: {
2018-04-17 11:17:26 +03:00
name: 1,
},
},
}).fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
assert.isDefined(author.name);
assert.isDefined(author.groups);
assert.isArray(author.groupNames);
assert.isString(author.fullName);
2018-04-17 11:17:26 +03:00
assert.isTrue(author.fullName.substr(0, 7) === 'full - ');
});
});
2018-04-17 11:17:26 +03:00
it('Should keep previously used items - Part 2', function() {
const data = createQuery({
authors: {
groupNames: 1,
groups: {
2018-04-17 11:17:26 +03:00
_id: 1,
},
},
}).fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
assert.isDefined(author.groups);
assert.isArray(author.groupNames);
author.groupNames.forEach(groupName => {
assert.isTrue(groupName.length > 2);
assert.isTrue(groupName.substr(0, 2) == 'G#');
assert.isFalse(groupName.slice(2) === 'undefined');
});
author.groups.forEach(group => {
assert.isDefined(group._id);
assert.isUndefined(group.name);
2018-04-17 11:17:26 +03:00
});
});
});
2017-12-21 19:59:29 +02:00
2018-04-17 11:17:26 +03:00
it('Should work with params reducers', function() {
2017-12-21 19:59:29 +02:00
const query = createQuery({
authors: {
2018-04-17 11:17:26 +03:00
$options: { limit: 1 },
2017-12-21 19:59:29 +02:00
paramBasedReducer: 1,
2018-04-17 11:17:26 +03:00
},
2017-12-21 19:59:29 +02:00
});
query.setParams({
2018-04-17 11:17:26 +03:00
element: 'TEST_STRING',
2017-12-21 19:59:29 +02:00
});
const data = query.fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
assert.equal(author.paramBasedReducer, 'TEST_STRING');
2018-04-17 11:17:26 +03:00
});
2017-12-21 19:59:29 +02:00
});
});