2016-09-23 13:31:33 +03:00
|
|
|
import { createQuery } from 'meteor/cultofcoders:grapher';
|
2016-10-14 10:57:26 +03:00
|
|
|
import './units/deepClone';
|
2017-01-04 10:32:26 +02:00
|
|
|
import './reducers.client.test';
|
2017-03-03 10:00:30 +02:00
|
|
|
import './security.client.test';
|
2017-01-04 11:11:20 +02:00
|
|
|
import waitForHandleToBeReady from './lib/waitForHandleToBeReady';
|
2016-09-23 13:31:33 +03:00
|
|
|
|
|
|
|
describe('Query Client Tests', function () {
|
|
|
|
it('Should work with queries via method call', function (done) {
|
|
|
|
const query = createQuery({
|
|
|
|
posts: {
|
|
|
|
$options: {limit: 5},
|
|
|
|
title: 1,
|
|
|
|
comments: {
|
|
|
|
$filters: {text: 'Good'},
|
|
|
|
text: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
query.fetch((err, res) => {
|
|
|
|
assert.isUndefined(err);
|
|
|
|
|
|
|
|
assert.isArray(res);
|
|
|
|
_.each(res, post => {
|
|
|
|
assert.isString(post.title);
|
|
|
|
assert.isString(post._id);
|
|
|
|
_.each(post.comments, comment => {
|
|
|
|
assert.isString(comment._id);
|
|
|
|
assert.equal('Good', comment.text);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should work with queries reactively', function (done) {
|
|
|
|
const query = createQuery({
|
|
|
|
posts: {
|
|
|
|
$options: {limit: 5},
|
|
|
|
title: 1,
|
|
|
|
comments: {
|
|
|
|
$filters: {text: 'Good'},
|
|
|
|
text: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const handle = query.subscribe();
|
|
|
|
|
|
|
|
Tracker.autorun(c => {
|
|
|
|
if (handle.ready()) {
|
|
|
|
c.stop();
|
|
|
|
|
|
|
|
const res = query.fetch();
|
|
|
|
|
|
|
|
assert.isArray(res);
|
|
|
|
_.each(res, post => {
|
|
|
|
assert.isString(post.title);
|
|
|
|
assert.isString(post._id);
|
|
|
|
_.each(post.comments, comment => {
|
|
|
|
assert.isString(comment._id);
|
|
|
|
assert.equal('Good', comment.text);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2016-10-07 10:31:58 +03:00
|
|
|
handle.stop();
|
2016-09-23 13:31:33 +03:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2016-10-25 10:51:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
it('Should fetch direct One links with $metadata via Subscription', function (done) {
|
|
|
|
let query = createQuery({
|
|
|
|
posts: {
|
|
|
|
group: {
|
|
|
|
name: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let handle = query.subscribe();
|
|
|
|
Tracker.autorun((c) => {
|
|
|
|
if (handle.ready()) {
|
|
|
|
c.stop();
|
|
|
|
let data = query.fetch();
|
|
|
|
|
|
|
|
handle.stop();
|
|
|
|
|
|
|
|
_.each(data, post => {
|
|
|
|
assert.isObject(post.group.$metadata);
|
|
|
|
assert.isDefined(post.group.$metadata.random);
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should fetch direct Many links with $metadata via Subscription', function (done) {
|
|
|
|
let query = createQuery({
|
|
|
|
authors: {
|
|
|
|
groups: {
|
|
|
|
$options: {limit: 1},
|
|
|
|
name: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let handle = query.subscribe();
|
|
|
|
Tracker.autorun((c) => {
|
|
|
|
if (handle.ready()) {
|
|
|
|
c.stop();
|
|
|
|
let data = query.fetch();
|
|
|
|
|
|
|
|
handle.stop();
|
|
|
|
|
|
|
|
_.each(data, author => {
|
|
|
|
assert.isArray(author.groups);
|
|
|
|
|
|
|
|
_.each(author.groups, group => {
|
|
|
|
assert.isObject(group.$metadata);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should fetch Inversed One Meta links with $metadata via Subscription', function (done) {
|
|
|
|
let query = createQuery({
|
|
|
|
groups: {
|
|
|
|
posts: {
|
|
|
|
title: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let handle = query.subscribe();
|
|
|
|
|
|
|
|
Tracker.autorun((c) => {
|
|
|
|
if (handle.ready()) {
|
|
|
|
c.stop();
|
|
|
|
|
|
|
|
let data = query.fetch();
|
|
|
|
handle.stop();
|
|
|
|
|
|
|
|
_.each(data, group => {
|
|
|
|
_.each(group.posts, post => {
|
|
|
|
assert.isObject(post.$metadata);
|
|
|
|
assert.isDefined(post.$metadata.random);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should fetch Inversed Many Meta links with $metadata via Subscription', function (done) {
|
|
|
|
let query = createQuery({
|
|
|
|
groups: {
|
|
|
|
authors: {
|
|
|
|
$options: {limit: 1},
|
|
|
|
name: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let handle = query.subscribe();
|
|
|
|
|
|
|
|
Tracker.autorun((c) => {
|
|
|
|
if (handle.ready()) {
|
|
|
|
c.stop();
|
|
|
|
|
|
|
|
let data = query.fetch();
|
|
|
|
|
|
|
|
_.each(data, group => {
|
|
|
|
_.each(group.authors, author => {
|
|
|
|
assert.isObject(author.$metadata);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
handle.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-11-29 10:52:42 +02:00
|
|
|
|
|
|
|
it('Should work with promises', async function () {
|
|
|
|
let query = createQuery({
|
|
|
|
groups: {
|
|
|
|
posts: {
|
|
|
|
title: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let result = await query.fetchSync();
|
|
|
|
|
|
|
|
assert.isArray(result);
|
|
|
|
assert.isTrue(result.length > 0);
|
|
|
|
result.forEach(item => {
|
|
|
|
assert.isArray(item.posts);
|
|
|
|
assert.isTrue(item.posts.length > 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
result = await query.fetchOneSync();
|
|
|
|
|
|
|
|
assert.isObject(result);
|
|
|
|
assert.isString(result._id);
|
|
|
|
assert.isArray(result.posts);
|
2016-11-30 00:20:01 +01:00
|
|
|
|
|
|
|
result = await query.getCountSync();
|
|
|
|
|
|
|
|
assert.isNumber(result);
|
2017-01-04 11:05:26 +02:00
|
|
|
});
|
2017-01-04 11:11:20 +02:00
|
|
|
|
|
|
|
it('Should work with fetchOne', async function (done) {
|
|
|
|
let query = createQuery({
|
|
|
|
groups: {
|
|
|
|
posts: {
|
|
|
|
title: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
query.fetchOne((err, group) => {
|
|
|
|
assert.isNotArray(group);
|
|
|
|
assert.isObject(group);
|
|
|
|
assert.isArray(group.posts);
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
})
|
2017-01-04 11:29:03 +02:00
|
|
|
|
|
|
|
it('Should work sorting with options that contain a dot', function () {
|
|
|
|
let query = createQuery({
|
|
|
|
posts: {
|
|
|
|
author: {
|
|
|
|
$filter({options}) {
|
|
|
|
options.sort = {
|
|
|
|
'profile.firstName': 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
profile: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
query.fetch((err, data) => {
|
|
|
|
assert.isArray(data);
|
|
|
|
})
|
2017-03-03 10:00:30 +02:00
|
|
|
});
|
2017-03-02 10:03:24 +02:00
|
|
|
|
|
|
|
it('Should properly clone and work with setParams', function () {
|
|
|
|
let query = createQuery({
|
|
|
|
posts: {
|
|
|
|
$options: {limit: 5}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let clone = query.clone({});
|
|
|
|
|
|
|
|
assert.isFunction(clone.fetch);
|
|
|
|
assert.isFunction(clone.fetchOne);
|
|
|
|
assert.isFunction(clone.setParams);
|
|
|
|
assert.isFunction(clone.setParams({}).fetchOne);
|
2017-03-03 10:00:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should work securely with reactive queries and linked exposures', function () {
|
|
|
|
|
2017-03-02 10:03:24 +02:00
|
|
|
})
|
2016-11-30 00:20:01 +01:00
|
|
|
});
|