Handle case of nested fields in embedReducerWithLink in tests

This commit is contained in:
Berislav 2018-06-12 11:27:08 +02:00
parent 84567fc3dc
commit 3253120eaf
3 changed files with 23 additions and 3 deletions

View file

@ -95,10 +95,17 @@ Authors.addReducers({
authorCached: {
name: 1,
},
metadata: {
keywords: 1
},
}
},
reduce(object) {
return _.first(object.posts).authorCached.name + ' - 2';
const firstPost = _.first(object.posts);
return {
author: firstPost.authorCached,
metadata: {...firstPost.metadata},
};
}
}
});

3
lib/query/testing/bootstrap/fixtures.js Normal file → Executable file
View file

@ -53,6 +53,9 @@ _.each(authors, (author) => {
_.each(_.range(POST_PER_USER), (idx) => {
let post = {
title: `User Post - ${idx}`,
metadata: {
keywords: _.sample(TAGS, _.random(1, 2)),
},
createdAt: new Date(),
};

View file

@ -254,8 +254,8 @@ describe('Reducers', function() {
* Both commentsReducers use Posts link on Authors collection and both use denormalized authorCached link
* inside the Posts.
*
* This necessitates the use of embedReducerWithLink() function while creating reducers
* which was failing for denormalized fields.
* This necessitates the use of embedReducerWithLink() function while creating reducers,
* which was failing for denormalized fields and also for nested fields.
*/
const query = createQuery({
authors: {
@ -265,7 +265,17 @@ describe('Reducers', function() {
});
const data = query.fetch();
assert.isTrue(data.length > 0);
data.forEach(author => {
// check if nested denormalized links are working
assert.isObject(author.commentsReducer2.author);
assert.isTrue(author.commentsReducer2.author.name.startsWith('Author'));
// check if nested fields are working
assert.isObject(author.commentsReducer2.metadata);
assert.isTrue(author.commentsReducer2.metadata.keywords.length > 0);
});
});
it('Should allow non-existent nested fields while cleaning', function() {