grapher/lib/namedQuery/testing/server.test.js

100 lines
2.8 KiB
JavaScript

import { postList, postListCached } from './bootstrap/server.js';
import { createQuery } from 'meteor/cultofcoders:grapher';
describe('Named Query', function () {
it('Should return the proper values', function () {
const createdQuery = createQuery({
postList: {
title: 'User Post - 3'
}
});
const directQuery = postList.clone({
title: 'User Post - 3'
});
_.each([createdQuery, directQuery], (query) => {
const data = query.fetch();
assert.isTrue(data.length > 1);
_.each(data, post => {
assert.equal(post.title, 'User Post - 3');
assert.isObject(post.author);
assert.isObject(post.group);
})
})
});
it('Exposure embodyment should work properly', function () {
const query = createQuery({
postListExposure: {
title: 'User Post - 3'
}
});
const data = query.fetch();
assert.isTrue(data.length > 1);
_.each(data, post => {
assert.equal(post.title, 'User Post - 3');
assert.isObject(post.author);
assert.isObject(post.group);
})
});
it('Should properly cache the values', function (done) {
const posts = postListCached.fetch();
const postsCount = postListCached.getCount();
const Posts = Mongo.Collection.get('posts');
const postId = Posts.insert({title: 'Hello Cacher!'});
assert.equal(posts.length, postListCached.fetch().length);
assert.equal(postsCount, postListCached.getCount());
Meteor.setTimeout(function () {
const newPosts = postListCached.fetch();
const newCount = postListCached.getCount();
Posts.remove(postId);
assert.isArray(newPosts);
assert.isNumber(newCount);
assert.equal(posts.length + 1, newPosts.length);
assert.equal(postsCount + 1, newCount);
done();
}, 400)
});
it('Should allow to securely fetch a subbody of a namedQuery', function () {
const query = createQuery({
postListExposure: {
limit: 5,
$body: {
title: 1,
createdAt: 1, // should fail
group: {
name: 1,
createdAt: 1, // should fail
}
}
}
});
const data = query.fetch();
assert.isTrue(data.length > 1);
_.each(data, post => {
assert.isUndefined(post.createdAt);
assert.isUndefined(post.author);
assert.isObject(post.group);
assert.isUndefined(post.group.createdAt);
})
})
});