Skip to content

Commit

Permalink
cruft cleanup, added test for observequery updates
Browse files Browse the repository at this point in the history
  • Loading branch information
svidgen committed Jun 8, 2022
1 parent 3261cc4 commit e35d66d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 42 deletions.
63 changes: 62 additions & 1 deletion packages/datastore/__tests__/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ describe('DataStore observeQuery, with fake-indexeddb and fake sync', () => {
})();
});

test('attaches related belongsTo properties records just like query() on inserts', async done => {
test('attaches related belongsTo properties consistently with query() on INSERT', async done => {
try {
const expecteds = [5, 15];

Expand Down Expand Up @@ -590,6 +590,67 @@ describe('DataStore observeQuery, with fake-indexeddb and fake sync', () => {
done(error);
}
});

test('attaches related belongsTo properties consistently with query() on UPDATE', async done => {
try {
const expecteds = [
['old post 0', 'old post 1', 'old post 2', 'old post 3', 'old post 4'],
['new post 0', 'new post 1', 'new post 2', 'new post 3', 'new post 4'],
];

for (let i = 0; i < 5; i++) {
await DataStore.save(
new Comment({
content: `comment content ${i}`,
post: await DataStore.save(
new Post({
title: `old post ${i}`,
})
),
})
);
}

const sub = DataStore.observeQuery(Comment).subscribe(
({ items, isSynced }) => {
const expected = expecteds.shift() || [];
expect(items.length).toBe(expected.length);

for (let i = 0; i < expected.length; i++) {
expect(items[i].content).toContain(`comment content ${i}`);
expect(items[i].post.title).toEqual(expected[i]);
}

if (expecteds.length === 0) {
sub.unsubscribe();
done();
}
}
);

setTimeout(async () => {
let postIndex = 0;
const comments = await DataStore.query(Comment);
for (const comment of comments) {
// create new posts, just to ensure
const newPost = await DataStore.save(
new Post({
title: `new post ${postIndex++}`,
})
);

await DataStore.save(
Comment.copyOf(comment, draft => {
draft.content = `updated: ${comment.content}`;
draft.post = newPost;
})
);
}
}, 1);
} catch (error) {
done(error);
}
});
});

describe('DataStore tests', () => {
Expand Down
44 changes: 3 additions & 41 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,9 @@ class DataStore {
.filter(({ model }) => namespaceResolver(model) === USER)
.subscribe({
next: async item => {
// the `element` for UPDATE events isn't a fully fleshed out instance of `modelConstructor`.
// the `element` doesn't necessarily contain all item details or
// have related records attached consistently with that of a query()
// result item. for consistency, we attach them here.

let message = item;

Expand All @@ -1169,46 +1171,6 @@ class DataStore {
error: err => observer.error(err),
complete: () => observer.complete(),
});

// .map(
// (item: InternalSubscriptionMessage<T>): SubscriptionMessage<T> => {

// // the `element` for UPDATE events isn't an instance of `modelConstructor`.
// // however, `executivePredicate` expects an instance that supports lazy loaded
// // associations. customers will presumably expect the same!
// let message = item;
// if (
// isModelConstructor(modelConstructor) &&
// !(item.element instanceof modelConstructor)
// ) {
// message = {
// ...message,
// element: modelInstanceCreator(modelConstructor, item.element),
// };
// }
// if (
// !executivePredicate ||
// (await executivePredicate.matches(message.element))
// ) {
// observer.next(message as SubscriptionMessage<T>);
// }

// // The `element` returned by storage only contains updated fields.
// // Intercept the event to send the `savedElement` so that the first
// // snapshot returned to the consumer contains all fields.
// // In the event of a delete we return `element`, as `savedElement`
// // here is undefined.
// const { opType, model, condition, element, savedElement } = event;

// return {
// opType,
// element: savedElement || element,
// model,
// condition,
// };
// }
// )
// .subscribe(observer);
})();

return () => {
Expand Down

0 comments on commit e35d66d

Please sign in to comment.